BattleInterfaceClasses.cpp 27 KB

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