CBattleInterfaceClasses.cpp 26 KB

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