CBattleInterfaceClasses.cpp 26 KB

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