BattleInterfaceClasses.cpp 25 KB

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