2
0

CBattleInterfaceClasses.cpp 26 KB

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