BattleInterfaceClasses.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * BattleInterfaceClasses.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 "BattleInterfaceClasses.h"
  12. #include "BattleInterface.h"
  13. #include "BattleActionsController.h"
  14. #include "BattleSiegeController.h"
  15. #include "BattleFieldController.h"
  16. #include "BattleStacksController.h"
  17. #include "BattleControlPanel.h"
  18. #include "../CGameInfo.h"
  19. #include "../CMessage.h"
  20. #include "../CMusicHandler.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../CVideoHandler.h"
  23. #include "../Graphics.h"
  24. #include "../gui/CAnimation.h"
  25. #include "../gui/Canvas.h"
  26. #include "../gui/CCursorHandler.h"
  27. #include "../gui/CGuiHandler.h"
  28. #include "../widgets/AdventureMapClasses.h"
  29. #include "../widgets/Buttons.h"
  30. #include "../widgets/Images.h"
  31. #include "../widgets/TextControls.h"
  32. #include "../windows/CCreatureWindow.h"
  33. #include "../windows/CSpellWindow.h"
  34. #include "../../CCallback.h"
  35. #include "../../lib/CStack.h"
  36. #include "../../lib/CConfigHandler.h"
  37. #include "../../lib/CCreatureHandler.h"
  38. #include "../../lib/CGameState.h"
  39. #include "../../lib/CGeneralTextHandler.h"
  40. #include "../../lib/CTownHandler.h"
  41. #include "../../lib/NetPacks.h"
  42. #include "../../lib/StartInfo.h"
  43. #include "../../lib/CondSh.h"
  44. #include "../../lib/mapObjects/CGTownInstance.h"
  45. void BattleConsole::showAll(SDL_Surface * to)
  46. {
  47. Point consolePos(pos.x + 10, pos.y + 17);
  48. Point textPos (pos.x + pos.w/2, pos.y + 17);
  49. if (!consoleText.empty())
  50. {
  51. graphics->fonts[FONT_SMALL]->renderTextLinesLeft(to, CMessage::breakText(consoleText, pos.w, FONT_SMALL), Colors::WHITE, consolePos);
  52. }
  53. else if(!hoverText.empty())
  54. {
  55. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(hoverText, pos.w, FONT_SMALL), Colors::WHITE, textPos);
  56. }
  57. else if(logEntries.size())
  58. {
  59. if(logEntries.size()==1)
  60. {
  61. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(logEntries[0], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  62. }
  63. else
  64. {
  65. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(logEntries[scrollPosition - 1], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  66. textPos.y += 16;
  67. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(logEntries[scrollPosition], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  68. }
  69. }
  70. }
  71. bool BattleConsole::addText(const std::string & text)
  72. {
  73. logGlobal->trace("CBattleConsole message: %s", text);
  74. if(text.size()>70)
  75. return false; //text too long!
  76. int firstInToken = 0;
  77. for(size_t i = 0; i < text.size(); ++i) //tokenize
  78. {
  79. if(text[i] == 10)
  80. {
  81. logEntries.push_back( text.substr(firstInToken, i-firstInToken) );
  82. firstInToken = (int)i+1;
  83. }
  84. }
  85. logEntries.push_back( text.substr(firstInToken, text.size()) );
  86. scrollPosition = (int)logEntries.size()-1;
  87. return true;
  88. }
  89. void BattleConsole::scrollUp(ui32 by)
  90. {
  91. if(scrollPosition > static_cast<int>(by))
  92. scrollPosition -= by;
  93. }
  94. void BattleConsole::scrollDown(ui32 by)
  95. {
  96. if(scrollPosition + by < logEntries.size())
  97. scrollPosition += by;
  98. }
  99. BattleConsole::BattleConsole(const Rect & position)
  100. : scrollPosition(-1)
  101. , enteringText(false)
  102. {
  103. pos += position.topLeft();
  104. pos.w = position.w;
  105. pos.h = position.h;
  106. }
  107. void BattleConsole::deactivate()
  108. {
  109. if (enteringText)
  110. LOCPLINT->cingconsole->endEnteringText(false);
  111. CIntObject::deactivate();
  112. }
  113. void BattleConsole::setEnteringMode(bool on)
  114. {
  115. consoleText.clear();
  116. if (on)
  117. {
  118. assert(enteringText == false);
  119. CSDL_Ext::startTextInput(&pos);
  120. }
  121. else
  122. {
  123. assert(enteringText == true);
  124. CSDL_Ext::stopTextInput();
  125. }
  126. enteringText = on;
  127. }
  128. void BattleConsole::setEnteredText(const std::string & text)
  129. {
  130. assert(enteringText == true);
  131. consoleText = text;
  132. }
  133. void BattleConsole::write(const std::string & Text)
  134. {
  135. hoverText = Text;
  136. }
  137. void BattleConsole::clearIfMatching(const std::string & Text)
  138. {
  139. if (hoverText == Text)
  140. clear();
  141. }
  142. void BattleConsole::clear()
  143. {
  144. write({});
  145. }
  146. void BattleHero::render(Canvas & canvas)
  147. {
  148. auto flagFrame = flagAnimation->getImage(flagAnim, 0, true);
  149. if(!flagFrame)
  150. return;
  151. //animation of flag
  152. Point flagPosition = pos.topLeft();
  153. if(flip)
  154. flagPosition += Point(61, 39);
  155. else
  156. flagPosition += Point(72, 39);
  157. auto heroFrame = animation->getImage(currentFrame, phase, true);
  158. canvas.draw(flagFrame, flagPosition);
  159. canvas.draw(heroFrame, pos.topLeft());
  160. if(++animCount >= 4)
  161. {
  162. animCount = 0;
  163. if(++flagAnim >= flagAnimation->size(0))
  164. flagAnim = 0;
  165. if(++currentFrame >= lastFrame)
  166. switchToNextPhase();
  167. }
  168. }
  169. void BattleHero::setPhase(int newPhase)
  170. {
  171. nextPhase = newPhase;
  172. switchToNextPhase(); //immediately switch to next phase and then restore idling phase
  173. nextPhase = 0;
  174. }
  175. void BattleHero::hover(bool on)
  176. {
  177. //TODO: Make lines below work properly
  178. if (on)
  179. CCS->curh->changeGraphic(ECursor::COMBAT, 5);
  180. else
  181. CCS->curh->changeGraphic(ECursor::COMBAT, 0);
  182. }
  183. void BattleHero::clickLeft(tribool down, bool previousState)
  184. {
  185. if(myOwner->actionsController->spellcastingModeActive()) //we are casting a spell
  186. return;
  187. if(boost::logic::indeterminate(down))
  188. return;
  189. if(!myHero || down || !myOwner->myTurn)
  190. return;
  191. if(myOwner->getCurrentPlayerInterface()->cb->battleCanCastSpell(myHero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
  192. {
  193. BattleHex hoveredHex = myOwner->fieldController->getHoveredHex();
  194. //do nothing when any hex is hovered - hero's animation overlaps battlefield
  195. if ( hoveredHex != BattleHex::INVALID )
  196. return;
  197. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  198. GH.pushIntT<CSpellWindow>(myHero, myOwner->getCurrentPlayerInterface());
  199. }
  200. }
  201. void BattleHero::clickRight(tribool down, bool previousState)
  202. {
  203. if(boost::logic::indeterminate(down))
  204. return;
  205. Point windowPosition;
  206. windowPosition.x = (!flip) ? myOwner->pos.topLeft().x + 1 : myOwner->pos.topRight().x - 79;
  207. windowPosition.y = myOwner->pos.y + 135;
  208. InfoAboutHero targetHero;
  209. if(down && (myOwner->myTurn || settings["session"]["spectate"].Bool()))
  210. {
  211. auto h = flip ? myOwner->defendingHeroInstance : myOwner->attackingHeroInstance;
  212. targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
  213. GH.pushIntT<HeroInfoWindow>(targetHero, &windowPosition);
  214. }
  215. }
  216. void BattleHero::switchToNextPhase()
  217. {
  218. if(phase != nextPhase)
  219. {
  220. phase = nextPhase;
  221. firstFrame = 0;
  222. lastFrame = static_cast<int>(animation->size(phase));
  223. }
  224. currentFrame = firstFrame;
  225. }
  226. BattleHero::BattleHero(const std::string & animationPath, bool flipG, PlayerColor player, const CGHeroInstance * hero, const BattleInterface & owner):
  227. flip(flipG),
  228. myHero(hero),
  229. myOwner(&owner),
  230. phase(1),
  231. nextPhase(0),
  232. flagAnim(0),
  233. animCount(0)
  234. {
  235. animation = std::make_shared<CAnimation>(animationPath);
  236. animation->preload();
  237. if(flipG)
  238. animation->verticalFlip();
  239. if(flip)
  240. flagAnimation = std::make_shared<CAnimation>("CMFLAGR");
  241. else
  242. flagAnimation = std::make_shared<CAnimation>("CMFLAGL");
  243. flagAnimation->preload();
  244. flagAnimation->playerColored(player);
  245. addUsedEvents(LCLICK | RCLICK | HOVER);
  246. switchToNextPhase();
  247. }
  248. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  249. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, "CHRPOP")
  250. {
  251. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  252. if (position != nullptr)
  253. moveTo(*position);
  254. background->colorize(hero.owner); //maybe add this functionality to base class?
  255. auto attack = hero.details->primskills[0];
  256. auto defense = hero.details->primskills[1];
  257. auto power = hero.details->primskills[2];
  258. auto knowledge = hero.details->primskills[3];
  259. auto morale = hero.details->morale;
  260. auto luck = hero.details->luck;
  261. auto currentSpellPoints = hero.details->mana;
  262. auto maxSpellPoints = hero.details->manaLimit;
  263. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 10, 6));
  264. //primary stats
  265. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  266. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  267. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":"));
  268. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":"));
  269. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack)));
  270. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense)));
  271. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power)));
  272. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge)));
  273. //morale+luck
  274. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  275. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  276. icons.push_back(std::make_shared<CAnimImage>("IMRL22", morale + 3, 0, 47, 131));
  277. icons.push_back(std::make_shared<CAnimImage>("ILCK22", luck + 3, 0, 47, 143));
  278. //spell points
  279. labels.push_back(std::make_shared<CLabel>(39, 174, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]));
  280. labels.push_back(std::make_shared<CLabel>(39, 186, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints)));
  281. }
  282. BattleOptionsWindow::BattleOptionsWindow(BattleInterface & owner):
  283. CWindowObject(PLAYER_COLORED, "comopbck.bmp")
  284. {
  285. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  286. auto viewGrid = std::make_shared<CToggleButton>(Point(25, 56), "sysopchk.def", CGI->generaltexth->zelp[427], [&](bool on){owner.setPrintCellBorders(on);} );
  287. viewGrid->setSelected(settings["battle"]["cellBorders"].Bool());
  288. toggles.push_back(viewGrid);
  289. auto movementShadow = std::make_shared<CToggleButton>(Point(25, 89), "sysopchk.def", CGI->generaltexth->zelp[428], [&](bool on){owner.setPrintStackRange(on);});
  290. movementShadow->setSelected(settings["battle"]["stackRange"].Bool());
  291. toggles.push_back(movementShadow);
  292. auto mouseShadow = std::make_shared<CToggleButton>(Point(25, 122), "sysopchk.def", CGI->generaltexth->zelp[429], [&](bool on){owner.setPrintMouseShadow(on);});
  293. mouseShadow->setSelected(settings["battle"]["mouseShadow"].Bool());
  294. toggles.push_back(mouseShadow);
  295. animSpeeds = std::make_shared<CToggleGroup>([&](int value){ owner.setAnimSpeed(value);});
  296. std::shared_ptr<CToggleButton> toggle;
  297. toggle = std::make_shared<CToggleButton>(Point( 28, 225), "sysopb9.def", CGI->generaltexth->zelp[422]);
  298. animSpeeds->addToggle(40, toggle);
  299. toggle = std::make_shared<CToggleButton>(Point( 92, 225), "sysob10.def", CGI->generaltexth->zelp[423]);
  300. animSpeeds->addToggle(63, toggle);
  301. toggle = std::make_shared<CToggleButton>(Point(156, 225), "sysob11.def", CGI->generaltexth->zelp[424]);
  302. animSpeeds->addToggle(100, toggle);
  303. animSpeeds->setSelected(owner.getAnimSpeed());
  304. setToDefault = std::make_shared<CButton>(Point(246, 359), "codefaul.def", CGI->generaltexth->zelp[393], [&](){ bDefaultf(); });
  305. setToDefault->setImageOrder(1, 0, 2, 3);
  306. exit = std::make_shared<CButton>(Point(357, 359), "soretrn.def", CGI->generaltexth->zelp[392], [&](){ bExitf();}, SDLK_RETURN);
  307. exit->setImageOrder(1, 0, 2, 3);
  308. //creating labels
  309. labels.push_back(std::make_shared<CLabel>(242, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[392]));//window title
  310. labels.push_back(std::make_shared<CLabel>(122, 214, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[393]));//animation speed
  311. labels.push_back(std::make_shared<CLabel>(122, 293, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[394]));//music volume
  312. labels.push_back(std::make_shared<CLabel>(122, 359, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[395]));//effects' volume
  313. labels.push_back(std::make_shared<CLabel>(353, 66, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[396]));//auto - combat options
  314. labels.push_back(std::make_shared<CLabel>(353, 265, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[397]));//creature info
  315. //auto - combat options
  316. labels.push_back(std::make_shared<CLabel>(283, 86, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[398]));//creatures
  317. labels.push_back(std::make_shared<CLabel>(283, 116, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[399]));//spells
  318. labels.push_back(std::make_shared<CLabel>(283, 146, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[400]));//catapult
  319. labels.push_back(std::make_shared<CLabel>(283, 176, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[151]));//ballista
  320. labels.push_back(std::make_shared<CLabel>(283, 206, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[401]));//first aid tent
  321. //creature info
  322. labels.push_back(std::make_shared<CLabel>(283, 285, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[402]));//all stats
  323. labels.push_back(std::make_shared<CLabel>(283, 315, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[403]));//spells only
  324. //general options
  325. labels.push_back(std::make_shared<CLabel>(61, 57, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[404]));
  326. labels.push_back(std::make_shared<CLabel>(61, 90, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[405]));
  327. labels.push_back(std::make_shared<CLabel>(61, 123, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[406]));
  328. labels.push_back(std::make_shared<CLabel>(61, 156, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[407]));
  329. }
  330. void BattleOptionsWindow::bDefaultf()
  331. {
  332. //TODO: implement
  333. }
  334. void BattleOptionsWindow::bExitf()
  335. {
  336. close();
  337. }
  338. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner)
  339. : owner(_owner)
  340. {
  341. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  342. pos = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  343. background = std::make_shared<CPicture>("CPRESULT");
  344. background->colorize(owner.playerID);
  345. exit = std::make_shared<CButton>(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, SDLK_RETURN);
  346. exit->setBorderColor(Colors::METALLIC_GOLD);
  347. if(br.winner == 0) //attacker won
  348. {
  349. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  350. }
  351. else
  352. {
  353. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  354. }
  355. if(br.winner == 1)
  356. {
  357. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  358. }
  359. else
  360. {
  361. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  362. }
  363. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  364. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  365. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  366. std::string sideNames[2] = {"N/A", "N/A"};
  367. for(int i = 0; i < 2; i++)
  368. {
  369. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  370. const int xs[] = {21, 392};
  371. if(heroInfo.portrait >= 0) //attacking hero
  372. {
  373. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38));
  374. sideNames[i] = heroInfo.name;
  375. }
  376. else
  377. {
  378. auto stacks = owner.cb->battleGetAllStacks();
  379. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  380. {
  381. return stack->side != i || !stack->base;
  382. });
  383. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  384. {
  385. return stack->type->AIValue;
  386. });
  387. if(best != stacks.end()) //should be always but to be safe...
  388. {
  389. icons.push_back(std::make_shared<CAnimImage>("TWCRPORT", (*best)->type->getIconIndex(), 0, xs[i], 38));
  390. sideNames[i] = (*best)->type->getPluralName();
  391. }
  392. }
  393. }
  394. //printing attacker and defender's names
  395. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  396. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  397. //printing casualties
  398. for(int step = 0; step < 2; ++step)
  399. {
  400. if(br.casualties[step].size()==0)
  401. {
  402. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  403. }
  404. else
  405. {
  406. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  407. int yPos = 344 + step * 97;
  408. for(auto & elem : br.casualties[step])
  409. {
  410. auto creature = CGI->creatures()->getByIndex(elem.first);
  411. if (creature->getId() == CreatureID::ARROW_TOWERS )
  412. continue; // do not show destroyed towers in battle results
  413. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, xPos, yPos));
  414. std::ostringstream amount;
  415. amount<<elem.second;
  416. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  417. xPos += 42;
  418. }
  419. }
  420. }
  421. //printing result description
  422. bool weAreAttacker = !(owner.cb->battleGetMySide());
  423. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  424. {
  425. int text = 304;
  426. switch(br.result)
  427. {
  428. case BattleResult::NORMAL:
  429. break;
  430. case BattleResult::ESCAPE:
  431. text = 303;
  432. break;
  433. case BattleResult::SURRENDER:
  434. text = 302;
  435. break;
  436. default:
  437. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  438. break;
  439. }
  440. CCS->musich->playMusic("Music/Win Battle", false, true);
  441. CCS->videoh->open("WIN3.BIK");
  442. std::string str = CGI->generaltexth->allTexts[text];
  443. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  444. if (ourHero)
  445. {
  446. str += CGI->generaltexth->allTexts[305];
  447. boost::algorithm::replace_first(str, "%s", ourHero->name);
  448. boost::algorithm::replace_first(str, "%d", boost::lexical_cast<std::string>(br.exp[weAreAttacker ? 0 : 1]));
  449. }
  450. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  451. }
  452. else // we lose
  453. {
  454. int text = 311;
  455. std::string musicName = "Music/LoseCombat";
  456. std::string videoName = "LBSTART.BIK";
  457. switch(br.result)
  458. {
  459. case BattleResult::NORMAL:
  460. break;
  461. case BattleResult::ESCAPE:
  462. musicName = "Music/Retreat Battle";
  463. videoName = "RTSTART.BIK";
  464. text = 310;
  465. break;
  466. case BattleResult::SURRENDER:
  467. musicName = "Music/Surrender Battle";
  468. videoName = "SURRENDER.BIK";
  469. text = 309;
  470. break;
  471. default:
  472. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  473. break;
  474. }
  475. CCS->musich->playMusic(musicName, false, true);
  476. CCS->videoh->open(videoName);
  477. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  478. }
  479. }
  480. void BattleResultWindow::activate()
  481. {
  482. owner.showingDialog->set(true);
  483. CIntObject::activate();
  484. }
  485. void BattleResultWindow::show(SDL_Surface * to)
  486. {
  487. CIntObject::show(to);
  488. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  489. }
  490. void BattleResultWindow::bExitf()
  491. {
  492. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  493. close();
  494. if(dynamic_cast<BattleInterface*>(GH.topInt().get()))
  495. GH.popInts(1); //pop battle interface if present
  496. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  497. //so we can be sure that there is no dialogs left on GUI stack.
  498. intTmp.showingDialog->setn(false);
  499. CCS->videoh->close();
  500. }
  501. void ClickableHex::hover(bool on)
  502. {
  503. hovered = on;
  504. //Hoverable::hover(on);
  505. if(!on && setAlterText)
  506. {
  507. myInterface->controlPanel->console->clear();
  508. setAlterText = false;
  509. }
  510. }
  511. ClickableHex::ClickableHex() : setAlterText(false), myNumber(-1), strictHovered(false), myInterface(nullptr)
  512. {
  513. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  514. }
  515. void ClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  516. {
  517. strictHovered = myInterface->fieldController->isPixelInHex(Point(sEvent.x-pos.x, sEvent.y-pos.y));
  518. if(hovered && strictHovered) //print attacked creature to console
  519. {
  520. const CStack * attackedStack = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber);
  521. if( attackedStack != nullptr &&
  522. attackedStack->owner != myInterface->getCurrentPlayerInterface()->playerID &&
  523. attackedStack->alive())
  524. {
  525. MetaString text;
  526. text.addTxt(MetaString::GENERAL_TXT, 220);
  527. attackedStack->addNameReplacement(text);
  528. myInterface->controlPanel->console->write(text.toString());
  529. setAlterText = true;
  530. }
  531. }
  532. else if(setAlterText)
  533. {
  534. myInterface->controlPanel->console->clear();
  535. setAlterText = false;
  536. }
  537. }
  538. void ClickableHex::clickLeft(tribool down, bool previousState)
  539. {
  540. if(!down && hovered && strictHovered) //we've been really clicked!
  541. {
  542. myInterface->hexLclicked(myNumber);
  543. }
  544. }
  545. void ClickableHex::clickRight(tribool down, bool previousState)
  546. {
  547. const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
  548. if(hovered && strictHovered && myst!=nullptr)
  549. {
  550. if(!myst->alive()) return;
  551. if(down)
  552. {
  553. GH.pushIntT<CStackWindow>(myst, true);
  554. }
  555. }
  556. }
  557. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  558. : embedded(Embedded),
  559. owner(owner)
  560. {
  561. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  562. if(embedded)
  563. {
  564. pos.w = QUEUE_SIZE * 37;
  565. pos.h = 46;
  566. pos.x = screen->w/2 - pos.w/2;
  567. pos.y = (screen->h - 600)/2 + 10;
  568. icons = std::make_shared<CAnimation>("CPRSMALL");
  569. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  570. }
  571. else
  572. {
  573. pos.w = 800;
  574. pos.h = 85;
  575. background = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, 0, pos.w, pos.h));
  576. icons = std::make_shared<CAnimation>("TWCRPORT");
  577. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  578. //TODO: where use big icons?
  579. //stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESBIG");
  580. }
  581. stateIcons->preload();
  582. stackBoxes.resize(QUEUE_SIZE);
  583. for (int i = 0; i < stackBoxes.size(); i++)
  584. {
  585. stackBoxes[i] = std::make_shared<StackBox>(this);
  586. stackBoxes[i]->moveBy(Point(1 + (embedded ? 36 : 80) * i, 0));
  587. }
  588. }
  589. void StackQueue::update()
  590. {
  591. std::vector<battle::Units> queueData;
  592. owner.getCurrentPlayerInterface()->cb->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  593. size_t boxIndex = 0;
  594. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  595. {
  596. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  597. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  598. }
  599. for(; boxIndex < stackBoxes.size(); boxIndex++)
  600. stackBoxes[boxIndex]->setUnit(nullptr);
  601. }
  602. int32_t StackQueue::getSiegeShooterIconID()
  603. {
  604. return owner.siegeController->getSiegedTown()->town->faction->index;
  605. }
  606. StackQueue::StackBox::StackBox(StackQueue * owner):
  607. owner(owner)
  608. {
  609. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  610. background = std::make_shared<CPicture>(owner->embedded ? "StackQueueSmall" : "StackQueueLarge");
  611. pos.w = background->pos.w;
  612. pos.h = background->pos.h;
  613. if(owner->embedded)
  614. {
  615. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  616. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  617. }
  618. else
  619. {
  620. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  621. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  622. int icon_x = pos.w - 17;
  623. int icon_y = pos.h - 18;
  624. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  625. stateIcon->visible = false;
  626. }
  627. }
  628. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  629. {
  630. if(unit)
  631. {
  632. background->colorize(unit->unitOwner());
  633. icon->visible = true;
  634. // temporary code for mod compatibility:
  635. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  636. // second, try to switch to icon that should be provided by mod
  637. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  638. // for 1.2 release & later next line should be moved into 'else' block
  639. icon->setFrame(unit->creatureIconIndex(), 0);
  640. if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS)
  641. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  642. amount->setText(makeNumberShort(unit->getCount()));
  643. if(stateIcon)
  644. {
  645. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  646. {
  647. stateIcon->setFrame(0, 0);
  648. stateIcon->visible = true;
  649. }
  650. else if(unit->waited((int)turn))
  651. {
  652. stateIcon->setFrame(1, 0);
  653. stateIcon->visible = true;
  654. }
  655. else
  656. {
  657. stateIcon->visible = false;
  658. }
  659. }
  660. }
  661. else
  662. {
  663. background->colorize(PlayerColor::NEUTRAL);
  664. icon->visible = false;
  665. icon->setFrame(0);
  666. amount->setText("");
  667. if(stateIcon)
  668. stateIcon->visible = false;
  669. }
  670. }