BattleInterfaceClasses.cpp 26 KB

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