BattleInterfaceClasses.cpp 28 KB

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