BattleInterfaceClasses.cpp 25 KB

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