2
0

BattleInterfaceClasses.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. auto attack = hero.details->primskills[0];
  325. auto defense = hero.details->primskills[1];
  326. auto power = hero.details->primskills[2];
  327. auto knowledge = hero.details->primskills[3];
  328. auto morale = hero.details->morale;
  329. auto luck = hero.details->luck;
  330. auto currentSpellPoints = hero.details->mana;
  331. auto maxSpellPoints = hero.details->manaLimit;
  332. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 10, 6));
  333. //primary stats
  334. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  335. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  336. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":"));
  337. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":"));
  338. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack)));
  339. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense)));
  340. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power)));
  341. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge)));
  342. //morale+luck
  343. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  344. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  345. icons.push_back(std::make_shared<CAnimImage>("IMRL22", morale + 3, 0, 47, 131));
  346. icons.push_back(std::make_shared<CAnimImage>("ILCK22", luck + 3, 0, 47, 143));
  347. //spell points
  348. labels.push_back(std::make_shared<CLabel>(39, 174, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]));
  349. labels.push_back(std::make_shared<CLabel>(39, 186, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints)));
  350. }
  351. void HeroInfoBasicPanel::show(Canvas & to)
  352. {
  353. showAll(to);
  354. CIntObject::show(to);
  355. }
  356. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  357. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, "CHRPOP")
  358. {
  359. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  360. if (position != nullptr)
  361. moveTo(*position);
  362. background->colorize(hero.owner); //maybe add this functionality to base class?
  363. content = std::make_shared<HeroInfoBasicPanel>(hero, nullptr, false);
  364. }
  365. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay)
  366. : owner(_owner)
  367. {
  368. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  369. background = std::make_shared<CPicture>("CPRESULT");
  370. background->colorize(owner.playerID);
  371. pos = center(background->pos);
  372. exit = std::make_shared<CButton>(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, EShortcut::GLOBAL_ACCEPT);
  373. exit->setBorderColor(Colors::METALLIC_GOLD);
  374. if(allowReplay)
  375. {
  376. repeat = std::make_shared<CButton>(Point(24, 505), "icn6432.def", std::make_pair("", ""), [&](){ bRepeatf();}, EShortcut::GLOBAL_CANCEL);
  377. repeat->setBorderColor(Colors::METALLIC_GOLD);
  378. labels.push_back(std::make_shared<CLabel>(232, 520, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("vcmi.battleResultsWindow.applyResultsLabel")));
  379. }
  380. if(br.winner == 0) //attacker won
  381. {
  382. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  383. }
  384. else
  385. {
  386. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  387. }
  388. if(br.winner == 1)
  389. {
  390. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  391. }
  392. else
  393. {
  394. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  395. }
  396. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  397. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  398. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  399. std::string sideNames[2] = {"N/A", "N/A"};
  400. for(int i = 0; i < 2; i++)
  401. {
  402. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  403. const int xs[] = {21, 392};
  404. if(heroInfo.portrait >= 0) //attacking hero
  405. {
  406. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38));
  407. sideNames[i] = heroInfo.name;
  408. }
  409. else
  410. {
  411. auto stacks = owner.cb->battleGetAllStacks();
  412. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  413. {
  414. return stack->unitSide() != i || !stack->base;
  415. });
  416. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  417. {
  418. return stack->unitType()->getAIValue();
  419. });
  420. if(best != stacks.end()) //should be always but to be safe...
  421. {
  422. icons.push_back(std::make_shared<CAnimImage>("TWCRPORT", (*best)->unitType()->getIconIndex(), 0, xs[i], 38));
  423. sideNames[i] = (*best)->unitType()->getNamePluralTranslated();
  424. }
  425. }
  426. }
  427. //printing attacker and defender's names
  428. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  429. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  430. //printing casualties
  431. for(int step = 0; step < 2; ++step)
  432. {
  433. if(br.casualties[step].size()==0)
  434. {
  435. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  436. }
  437. else
  438. {
  439. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  440. int yPos = 344 + step * 97;
  441. for(auto & elem : br.casualties[step])
  442. {
  443. auto creature = CGI->creatures()->getByIndex(elem.first);
  444. if (creature->getId() == CreatureID::ARROW_TOWERS )
  445. continue; // do not show destroyed towers in battle results
  446. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, xPos, yPos));
  447. std::ostringstream amount;
  448. amount<<elem.second;
  449. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  450. xPos += 42;
  451. }
  452. }
  453. }
  454. //printing result description
  455. bool weAreAttacker = !(owner.cb->battleGetMySide());
  456. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  457. {
  458. int text = 304;
  459. switch(br.result)
  460. {
  461. case BattleResult::NORMAL:
  462. break;
  463. case BattleResult::ESCAPE:
  464. text = 303;
  465. break;
  466. case BattleResult::SURRENDER:
  467. text = 302;
  468. break;
  469. default:
  470. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  471. break;
  472. }
  473. CCS->musich->playMusic("Music/Win Battle", false, true);
  474. CCS->videoh->open("WIN3.BIK");
  475. std::string str = CGI->generaltexth->allTexts[text];
  476. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  477. if (ourHero)
  478. {
  479. str += CGI->generaltexth->allTexts[305];
  480. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  481. boost::algorithm::replace_first(str, "%d", std::to_string(br.exp[weAreAttacker ? 0 : 1]));
  482. }
  483. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  484. }
  485. else // we lose
  486. {
  487. int text = 311;
  488. std::string musicName = "Music/LoseCombat";
  489. std::string videoName = "LBSTART.BIK";
  490. switch(br.result)
  491. {
  492. case BattleResult::NORMAL:
  493. break;
  494. case BattleResult::ESCAPE:
  495. musicName = "Music/Retreat Battle";
  496. videoName = "RTSTART.BIK";
  497. text = 310;
  498. break;
  499. case BattleResult::SURRENDER:
  500. musicName = "Music/Surrender Battle";
  501. videoName = "SURRENDER.BIK";
  502. text = 309;
  503. break;
  504. default:
  505. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  506. break;
  507. }
  508. CCS->musich->playMusic(musicName, false, true);
  509. CCS->videoh->open(videoName);
  510. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  511. }
  512. }
  513. void BattleResultWindow::activate()
  514. {
  515. owner.showingDialog->set(true);
  516. CIntObject::activate();
  517. }
  518. void BattleResultWindow::show(Canvas & to)
  519. {
  520. CIntObject::show(to);
  521. CCS->videoh->update(pos.x + 107, pos.y + 70, to.getInternalSurface(), true, false);
  522. }
  523. void BattleResultWindow::buttonPressed(int button)
  524. {
  525. resultCallback(button);
  526. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  527. close();
  528. if(GH.windows().topWindow<BattleWindow>())
  529. GH.windows().popWindows(1); //pop battle interface if present
  530. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  531. //so we can be sure that there is no dialogs left on GUI stack.
  532. intTmp.showingDialog->setn(false);
  533. CCS->videoh->close();
  534. }
  535. void BattleResultWindow::bExitf()
  536. {
  537. buttonPressed(0);
  538. }
  539. void BattleResultWindow::bRepeatf()
  540. {
  541. buttonPressed(1);
  542. }
  543. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  544. : embedded(Embedded),
  545. owner(owner)
  546. {
  547. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  548. if(embedded)
  549. {
  550. pos.w = QUEUE_SIZE * 41;
  551. pos.h = 49;
  552. pos.x += parent->pos.w/2 - pos.w/2;
  553. pos.y += 10;
  554. icons = std::make_shared<CAnimation>("CPRSMALL");
  555. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  556. }
  557. else
  558. {
  559. pos.w = 800;
  560. pos.h = 85;
  561. pos.x += 0;
  562. pos.y -= pos.h;
  563. background = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, 0, pos.w, pos.h));
  564. icons = std::make_shared<CAnimation>("TWCRPORT");
  565. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  566. //TODO: where use big icons?
  567. //stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESBIG");
  568. }
  569. stateIcons->preload();
  570. stackBoxes.resize(QUEUE_SIZE);
  571. for (int i = 0; i < stackBoxes.size(); i++)
  572. {
  573. stackBoxes[i] = std::make_shared<StackBox>(this);
  574. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  575. }
  576. }
  577. void StackQueue::show(Canvas & to)
  578. {
  579. if (embedded)
  580. showAll(to);
  581. CIntObject::show(to);
  582. }
  583. void StackQueue::update()
  584. {
  585. std::vector<battle::Units> queueData;
  586. owner.getCurrentPlayerInterface()->cb->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  587. size_t boxIndex = 0;
  588. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  589. {
  590. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  591. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  592. }
  593. for(; boxIndex < stackBoxes.size(); boxIndex++)
  594. stackBoxes[boxIndex]->setUnit(nullptr);
  595. }
  596. int32_t StackQueue::getSiegeShooterIconID()
  597. {
  598. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  599. }
  600. std::optional<uint32_t> StackQueue::getHoveredUnitIdIfAny() const
  601. {
  602. for(const auto & stackBox : stackBoxes)
  603. {
  604. if(stackBox->isHovered())
  605. {
  606. return stackBox->getBoundUnitID();
  607. }
  608. }
  609. return std::nullopt;
  610. }
  611. StackQueue::StackBox::StackBox(StackQueue * owner):
  612. CIntObject(SHOW_POPUP | HOVER), owner(owner)
  613. {
  614. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  615. background = std::make_shared<CPicture>(owner->embedded ? "StackQueueSmall" : "StackQueueLarge");
  616. pos.w = background->pos.w;
  617. pos.h = background->pos.h;
  618. if(owner->embedded)
  619. {
  620. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  621. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  622. }
  623. else
  624. {
  625. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  626. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  627. int icon_x = pos.w - 17;
  628. int icon_y = pos.h - 18;
  629. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  630. stateIcon->visible = false;
  631. }
  632. }
  633. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  634. {
  635. if(unit)
  636. {
  637. boundUnitID = unit->unitId();
  638. background->colorize(unit->unitOwner());
  639. icon->visible = true;
  640. // temporary code for mod compatibility:
  641. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  642. // second, try to switch to icon that should be provided by mod
  643. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  644. // for 1.2 release & later next line should be moved into 'else' block
  645. icon->setFrame(unit->creatureIconIndex(), 0);
  646. if (unit->unitType()->getId() == CreatureID::ARROW_TOWERS)
  647. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  648. amount->setText(TextOperations::formatMetric(unit->getCount(), 4));
  649. if(stateIcon)
  650. {
  651. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  652. {
  653. stateIcon->setFrame(0, 0);
  654. stateIcon->visible = true;
  655. }
  656. else if(unit->waited((int)turn))
  657. {
  658. stateIcon->setFrame(1, 0);
  659. stateIcon->visible = true;
  660. }
  661. else
  662. {
  663. stateIcon->visible = false;
  664. }
  665. }
  666. }
  667. else
  668. {
  669. boundUnitID = std::nullopt;
  670. background->colorize(PlayerColor::NEUTRAL);
  671. icon->visible = false;
  672. icon->setFrame(0);
  673. amount->setText("");
  674. if(stateIcon)
  675. stateIcon->visible = false;
  676. }
  677. }
  678. std::optional<uint32_t> StackQueue::StackBox::getBoundUnitID() const
  679. {
  680. return boundUnitID;
  681. }
  682. bool StackQueue::StackBox::isBoundUnitHighlighted() const
  683. {
  684. auto unitIdsToHighlight = owner->owner.stacksController->getHoveredStacksUnitIds();
  685. return vstd::contains(unitIdsToHighlight, getBoundUnitID());
  686. }
  687. void StackQueue::StackBox::showAll(Canvas & to)
  688. {
  689. CIntObject::showAll(to);
  690. if(isBoundUnitHighlighted())
  691. to.drawBorder(background->pos, Colors::CYAN, 2);
  692. }
  693. void StackQueue::StackBox::show(Canvas & to)
  694. {
  695. CIntObject::show(to);
  696. if(isBoundUnitHighlighted())
  697. to.drawBorder(background->pos, Colors::CYAN, 2);
  698. }