CBattleInterfaceClasses.cpp 27 KB

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