BattleInterfaceClasses.cpp 27 KB

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