BattleInterfaceClasses.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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), currentVideo(BattleResultVideo::NONE)
  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. currentVideo = BattleResultVideo::WIN;
  474. switch(br.result)
  475. {
  476. case EBattleResult::NORMAL:
  477. if(owner.cb->getBattle(br.battleID)->battleGetDefendedTown() && !weAreAttacker)
  478. currentVideo = BattleResultVideo::WIN_SIEGE;
  479. break;
  480. case EBattleResult::ESCAPE:
  481. text = 303;
  482. break;
  483. case EBattleResult::SURRENDER:
  484. text = 302;
  485. break;
  486. default:
  487. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  488. break;
  489. }
  490. playVideo();
  491. std::string str = CGI->generaltexth->allTexts[text];
  492. const CGHeroInstance * ourHero = owner.cb->getBattle(br.battleID)->battleGetMyHero();
  493. if (ourHero)
  494. {
  495. str += CGI->generaltexth->allTexts[305];
  496. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  497. boost::algorithm::replace_first(str, "%d", std::to_string(br.exp[weAreAttacker ? 0 : 1]));
  498. }
  499. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  500. }
  501. else // we lose
  502. {
  503. int text = 311;
  504. currentVideo = BattleResultVideo::DEFEAT;
  505. switch(br.result)
  506. {
  507. case EBattleResult::NORMAL:
  508. if(owner.cb->getBattle(br.battleID)->battleGetDefendedTown() && !weAreAttacker)
  509. currentVideo = BattleResultVideo::DEFEAT_SIEGE;
  510. break;
  511. case EBattleResult::ESCAPE:
  512. currentVideo = BattleResultVideo::RETREAT;
  513. text = 310;
  514. break;
  515. case EBattleResult::SURRENDER:
  516. currentVideo = BattleResultVideo::SURRENDER;
  517. text = 309;
  518. break;
  519. default:
  520. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  521. break;
  522. }
  523. playVideo();
  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. {
  538. playVideo(true);
  539. });
  540. }
  541. void BattleResultWindow::playVideo(bool startLoop)
  542. {
  543. AudioPath musicName = AudioPath();
  544. VideoPath videoName = VideoPath();
  545. if(!startLoop)
  546. {
  547. switch(currentVideo)
  548. {
  549. case BattleResultVideo::WIN:
  550. musicName = AudioPath::builtin("Music/Win Battle");
  551. videoName = VideoPath::builtin("WIN3.BIK");
  552. break;
  553. case BattleResultVideo::SURRENDER:
  554. musicName = AudioPath::builtin("Music/Surrender Battle");
  555. videoName = VideoPath::builtin("SURRENDER.BIK");
  556. break;
  557. case BattleResultVideo::RETREAT:
  558. musicName = AudioPath::builtin("Music/Retreat Battle");
  559. videoName = VideoPath::builtin("RTSTART.BIK");
  560. break;
  561. case BattleResultVideo::DEFEAT:
  562. musicName = AudioPath::builtin("Music/LoseCombat");
  563. videoName = VideoPath::builtin("LBSTART.BIK");
  564. break;
  565. case BattleResultVideo::DEFEAT_SIEGE:
  566. musicName = AudioPath::builtin("Music/LoseCastle");
  567. videoName = VideoPath::builtin("LOSECSTL.BIK");
  568. break;
  569. case BattleResultVideo::WIN_SIEGE:
  570. musicName = AudioPath::builtin("Music/Defend Castle");
  571. videoName = VideoPath::builtin("DEFENDALL.BIK");
  572. break;
  573. }
  574. }
  575. else
  576. {
  577. switch(currentVideo)
  578. {
  579. case BattleResultVideo::RETREAT:
  580. currentVideo = BattleResultVideo::RETREAT_LOOP;
  581. videoName = VideoPath::builtin("RTLOOP.BIK");
  582. break;
  583. case BattleResultVideo::DEFEAT:
  584. currentVideo = BattleResultVideo::DEFEAT_LOOP;
  585. videoName = VideoPath::builtin("LBLOOP.BIK");
  586. break;
  587. case BattleResultVideo::DEFEAT_SIEGE:
  588. currentVideo = BattleResultVideo::DEFEAT_SIEGE_LOOP;
  589. videoName = VideoPath::builtin("LOSECSLP.BIK");
  590. break;
  591. case BattleResultVideo::WIN_SIEGE:
  592. currentVideo = BattleResultVideo::WIN_SIEGE_LOOP;
  593. videoName = VideoPath::builtin("DEFENDLOOP.BIK");
  594. break;
  595. }
  596. }
  597. if(musicName != AudioPath())
  598. CCS->musich->playMusic(musicName, false, true);
  599. if(videoName != VideoPath())
  600. CCS->videoh->open(videoName);
  601. }
  602. void BattleResultWindow::buttonPressed(int button)
  603. {
  604. if (resultCallback)
  605. resultCallback(button);
  606. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  607. close();
  608. if(GH.windows().topWindow<BattleWindow>())
  609. GH.windows().popWindows(1); //pop battle interface if present
  610. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  611. //so we can be sure that there is no dialogs left on GUI stack.
  612. intTmp.showingDialog->setn(false);
  613. CCS->videoh->close();
  614. }
  615. void BattleResultWindow::bExitf()
  616. {
  617. buttonPressed(0);
  618. }
  619. void BattleResultWindow::bRepeatf()
  620. {
  621. buttonPressed(1);
  622. }
  623. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  624. : embedded(Embedded),
  625. owner(owner)
  626. {
  627. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  628. if(embedded)
  629. {
  630. pos.w = QUEUE_SIZE * 41;
  631. pos.h = 49;
  632. pos.x += parent->pos.w/2 - pos.w/2;
  633. pos.y += 10;
  634. icons = GH.renderHandler().loadAnimation(AnimationPath::builtin("CPRSMALL"));
  635. stateIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("VCMI/BATTLEQUEUE/STATESSMALL"));
  636. }
  637. else
  638. {
  639. pos.w = 800;
  640. pos.h = 85;
  641. pos.x += 0;
  642. pos.y -= pos.h;
  643. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, pos.w, pos.h));
  644. icons = GH.renderHandler().loadAnimation(AnimationPath::builtin("TWCRPORT"));
  645. stateIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("VCMI/BATTLEQUEUE/STATESSMALL"));
  646. //TODO: where use big icons?
  647. //stateIcons = GH.renderHandler().loadAnimation("VCMI/BATTLEQUEUE/STATESBIG");
  648. }
  649. stateIcons->preload();
  650. stackBoxes.resize(QUEUE_SIZE);
  651. for (int i = 0; i < stackBoxes.size(); i++)
  652. {
  653. stackBoxes[i] = std::make_shared<StackBox>(this);
  654. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  655. }
  656. }
  657. void StackQueue::show(Canvas & to)
  658. {
  659. if (embedded)
  660. showAll(to);
  661. CIntObject::show(to);
  662. }
  663. void StackQueue::update()
  664. {
  665. std::vector<battle::Units> queueData;
  666. owner.getBattle()->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  667. size_t boxIndex = 0;
  668. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  669. {
  670. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  671. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  672. }
  673. for(; boxIndex < stackBoxes.size(); boxIndex++)
  674. stackBoxes[boxIndex]->setUnit(nullptr);
  675. }
  676. int32_t StackQueue::getSiegeShooterIconID()
  677. {
  678. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  679. }
  680. std::optional<uint32_t> StackQueue::getHoveredUnitIdIfAny() const
  681. {
  682. for(const auto & stackBox : stackBoxes)
  683. {
  684. if(stackBox->isHovered())
  685. {
  686. return stackBox->getBoundUnitID();
  687. }
  688. }
  689. return std::nullopt;
  690. }
  691. StackQueue::StackBox::StackBox(StackQueue * owner):
  692. CIntObject(SHOW_POPUP | HOVER), owner(owner)
  693. {
  694. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  695. background = std::make_shared<CPicture>(ImagePath::builtin(owner->embedded ? "StackQueueSmall" : "StackQueueLarge"));
  696. pos.w = background->pos.w;
  697. pos.h = background->pos.h;
  698. if(owner->embedded)
  699. {
  700. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  701. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  702. }
  703. else
  704. {
  705. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  706. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  707. int icon_x = pos.w - 17;
  708. int icon_y = pos.h - 18;
  709. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  710. stateIcon->visible = false;
  711. }
  712. }
  713. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  714. {
  715. if(unit)
  716. {
  717. boundUnitID = unit->unitId();
  718. background->colorize(unit->unitOwner());
  719. icon->visible = true;
  720. // temporary code for mod compatibility:
  721. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  722. // second, try to switch to icon that should be provided by mod
  723. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  724. // for 1.2 release & later next line should be moved into 'else' block
  725. icon->setFrame(unit->creatureIconIndex(), 0);
  726. if (unit->unitType()->getId() == CreatureID::ARROW_TOWERS)
  727. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  728. amount->setText(TextOperations::formatMetric(unit->getCount(), 4));
  729. if(stateIcon)
  730. {
  731. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  732. {
  733. stateIcon->setFrame(0, 0);
  734. stateIcon->visible = true;
  735. }
  736. else if(unit->waited((int)turn))
  737. {
  738. stateIcon->setFrame(1, 0);
  739. stateIcon->visible = true;
  740. }
  741. else
  742. {
  743. stateIcon->visible = false;
  744. }
  745. }
  746. }
  747. else
  748. {
  749. boundUnitID = std::nullopt;
  750. background->colorize(PlayerColor::NEUTRAL);
  751. icon->visible = false;
  752. icon->setFrame(0);
  753. amount->setText("");
  754. if(stateIcon)
  755. stateIcon->visible = false;
  756. }
  757. }
  758. std::optional<uint32_t> StackQueue::StackBox::getBoundUnitID() const
  759. {
  760. return boundUnitID;
  761. }
  762. bool StackQueue::StackBox::isBoundUnitHighlighted() const
  763. {
  764. auto unitIdsToHighlight = owner->owner.stacksController->getHoveredStacksUnitIds();
  765. return vstd::contains(unitIdsToHighlight, getBoundUnitID());
  766. }
  767. void StackQueue::StackBox::showAll(Canvas & to)
  768. {
  769. CIntObject::showAll(to);
  770. if(isBoundUnitHighlighted())
  771. to.drawBorder(background->pos, Colors::CYAN, 2);
  772. }
  773. void StackQueue::StackBox::show(Canvas & to)
  774. {
  775. CIntObject::show(to);
  776. if(isBoundUnitHighlighted())
  777. to.drawBorder(background->pos, Colors::CYAN, 2);
  778. }