BattleInterfaceClasses.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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/StartInfo.h"
  49. #include "../../lib/CondSh.h"
  50. #include "../../lib/mapObjects/CGTownInstance.h"
  51. #include "../../lib/networkPacks/PacksForClientBattle.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. StackInfoBasicPanel::StackInfoBasicPanel(const CStack * stack, Point * position, bool initializeBackground)
  371. : CIntObject(0)
  372. {
  373. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  374. if (position != nullptr)
  375. moveTo(*position);
  376. if(initializeBackground)
  377. {
  378. background = std::make_shared<CPicture>(ImagePath::builtin("CCRPOP"));
  379. background->pos.y += 37;
  380. background->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  381. background->colorize(stack->getOwner());
  382. background2 = std::make_shared<CPicture>(ImagePath::builtin("CHRPOP"));
  383. background2->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  384. background2->colorize(stack->getOwner());
  385. }
  386. initializeData(stack);
  387. }
  388. void StackInfoBasicPanel::initializeData(const CStack * stack)
  389. {
  390. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  391. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), stack->creatureId() + 2, 0, 10, 6));
  392. labels.push_back(std::make_shared<CLabel>(10 + 58, 6 + 64, FONT_MEDIUM, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, TextOperations::formatMetric(stack->getCount(), 4)));
  393. auto attack = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getAttack(stack->isShooter())) + "(" + std::to_string(stack->getAttack(stack->isShooter())) + ")";
  394. auto defense = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getDefense(stack->isShooter())) + "(" + std::to_string(stack->getDefense(stack->isShooter())) + ")";
  395. auto damage = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getMinDamage(stack->isShooter())) + "-" + std::to_string(stack->getMaxDamage(stack->isShooter()));
  396. auto health = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getMaxHealth());
  397. auto morale = stack->moraleVal();
  398. auto luck = stack->luckVal();
  399. auto killed = stack->getKilled();
  400. auto healthRemaining = TextOperations::formatMetric(stack->getAvailableHealth(), 4);
  401. //primary stats*/
  402. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  403. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  404. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[386] + ":"));
  405. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[389] + ":"));
  406. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, attack));
  407. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, defense));
  408. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, damage));
  409. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, health));
  410. //morale+luck
  411. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  412. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  413. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("IMRL22"), morale + 3, 0, 47, 131));
  414. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ILCK22"), luck + 3, 0, 47, 143));
  415. //extra information
  416. labels.push_back(std::make_shared<CLabel>(9, 168, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, std::string("Killed") + ":"));
  417. labels.push_back(std::make_shared<CLabel>(9, 180, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, std::string("Rem He") + ":"));
  418. labels.push_back(std::make_shared<CLabel>(69, 180, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(killed)));
  419. labels.push_back(std::make_shared<CLabel>(69, 192, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, healthRemaining));
  420. }
  421. void StackInfoBasicPanel::update(const CStack * updatedInfo)
  422. {
  423. icons.clear();
  424. labels.clear();
  425. initializeData(updatedInfo);
  426. }
  427. void StackInfoBasicPanel::show(Canvas & to)
  428. {
  429. showAll(to);
  430. CIntObject::show(to);
  431. }
  432. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  433. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, ImagePath::builtin("CHRPOP"))
  434. {
  435. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  436. if (position != nullptr)
  437. moveTo(*position);
  438. background->colorize(hero.owner); //maybe add this functionality to base class?
  439. content = std::make_shared<HeroInfoBasicPanel>(hero, nullptr, false);
  440. }
  441. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay)
  442. : owner(_owner), currentVideo(BattleResultVideo::NONE)
  443. {
  444. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  445. background = std::make_shared<CPicture>(ImagePath::builtin("CPRESULT"));
  446. background->colorize(owner.playerID);
  447. pos = center(background->pos);
  448. exit = std::make_shared<CButton>(Point(384, 505), AnimationPath::builtin("iok6432.def"), std::make_pair("", ""), [&](){ bExitf();}, EShortcut::GLOBAL_ACCEPT);
  449. exit->setBorderColor(Colors::METALLIC_GOLD);
  450. if(allowReplay)
  451. {
  452. repeat = std::make_shared<CButton>(Point(24, 505), AnimationPath::builtin("icn6432.def"), std::make_pair("", ""), [&](){ bRepeatf();}, EShortcut::GLOBAL_CANCEL);
  453. repeat->setBorderColor(Colors::METALLIC_GOLD);
  454. labels.push_back(std::make_shared<CLabel>(232, 520, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("vcmi.battleResultsWindow.applyResultsLabel")));
  455. }
  456. if(br.winner == 0) //attacker won
  457. {
  458. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  459. }
  460. else
  461. {
  462. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  463. }
  464. if(br.winner == 1)
  465. {
  466. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  467. }
  468. else
  469. {
  470. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  471. }
  472. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  473. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  474. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  475. std::string sideNames[2] = {"N/A", "N/A"};
  476. for(int i = 0; i < 2; i++)
  477. {
  478. auto heroInfo = owner.cb->getBattle(br.battleID)->battleGetHeroInfo(i);
  479. const int xs[] = {21, 392};
  480. if(heroInfo.portraitSource.isValid()) //attacking hero
  481. {
  482. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), heroInfo.getIconIndex(), 0, xs[i], 38));
  483. sideNames[i] = heroInfo.name;
  484. }
  485. else
  486. {
  487. auto stacks = owner.cb->getBattle(br.battleID)->battleGetAllStacks();
  488. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  489. {
  490. return stack->unitSide() != i || !stack->base;
  491. });
  492. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  493. {
  494. return stack->unitType()->getAIValue();
  495. });
  496. if(best != stacks.end()) //should be always but to be safe...
  497. {
  498. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), (*best)->unitType()->getIconIndex(), 0, xs[i], 38));
  499. sideNames[i] = (*best)->unitType()->getNamePluralTranslated();
  500. }
  501. }
  502. }
  503. //printing attacker and defender's names
  504. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  505. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  506. //printing casualties
  507. for(int step = 0; step < 2; ++step)
  508. {
  509. if(br.casualties[step].size()==0)
  510. {
  511. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  512. }
  513. else
  514. {
  515. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  516. int yPos = 344 + step * 97;
  517. for(auto & elem : br.casualties[step])
  518. {
  519. auto creature = CGI->creatures()->getByIndex(elem.first);
  520. if (creature->getId() == CreatureID::ARROW_TOWERS )
  521. continue; // do not show destroyed towers in battle results
  522. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), creature->getIconIndex(), 0, xPos, yPos));
  523. std::ostringstream amount;
  524. amount<<elem.second;
  525. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  526. xPos += 42;
  527. }
  528. }
  529. }
  530. //printing result description
  531. bool weAreAttacker = !(owner.cb->getBattle(br.battleID)->battleGetMySide());
  532. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  533. {
  534. int text = 304;
  535. currentVideo = BattleResultVideo::WIN;
  536. switch(br.result)
  537. {
  538. case EBattleResult::NORMAL:
  539. if(owner.cb->getBattle(br.battleID)->battleGetDefendedTown() && !weAreAttacker)
  540. currentVideo = BattleResultVideo::WIN_SIEGE;
  541. break;
  542. case EBattleResult::ESCAPE:
  543. text = 303;
  544. break;
  545. case EBattleResult::SURRENDER:
  546. text = 302;
  547. break;
  548. default:
  549. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  550. break;
  551. }
  552. playVideo();
  553. std::string str = CGI->generaltexth->allTexts[text];
  554. const CGHeroInstance * ourHero = owner.cb->getBattle(br.battleID)->battleGetMyHero();
  555. if (ourHero)
  556. {
  557. str += CGI->generaltexth->allTexts[305];
  558. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  559. boost::algorithm::replace_first(str, "%d", std::to_string(br.exp[weAreAttacker ? 0 : 1]));
  560. }
  561. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  562. }
  563. else // we lose
  564. {
  565. int text = 311;
  566. currentVideo = BattleResultVideo::DEFEAT;
  567. switch(br.result)
  568. {
  569. case EBattleResult::NORMAL:
  570. if(owner.cb->getBattle(br.battleID)->battleGetDefendedTown() && !weAreAttacker)
  571. currentVideo = BattleResultVideo::DEFEAT_SIEGE;
  572. break;
  573. case EBattleResult::ESCAPE:
  574. currentVideo = BattleResultVideo::RETREAT;
  575. text = 310;
  576. break;
  577. case EBattleResult::SURRENDER:
  578. currentVideo = BattleResultVideo::SURRENDER;
  579. text = 309;
  580. break;
  581. default:
  582. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  583. break;
  584. }
  585. playVideo();
  586. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  587. }
  588. }
  589. void BattleResultWindow::activate()
  590. {
  591. owner.showingDialog->set(true);
  592. CIntObject::activate();
  593. }
  594. void BattleResultWindow::show(Canvas & to)
  595. {
  596. CIntObject::show(to);
  597. CCS->videoh->update(pos.x + 107, pos.y + 70, to.getInternalSurface(), true, false,
  598. [&]()
  599. {
  600. playVideo(true);
  601. });
  602. }
  603. void BattleResultWindow::playVideo(bool startLoop)
  604. {
  605. AudioPath musicName = AudioPath();
  606. VideoPath videoName = VideoPath();
  607. if(!startLoop)
  608. {
  609. switch(currentVideo)
  610. {
  611. case BattleResultVideo::WIN:
  612. musicName = AudioPath::builtin("Music/Win Battle");
  613. videoName = VideoPath::builtin("WIN3.BIK");
  614. break;
  615. case BattleResultVideo::SURRENDER:
  616. musicName = AudioPath::builtin("Music/Surrender Battle");
  617. videoName = VideoPath::builtin("SURRENDER.BIK");
  618. break;
  619. case BattleResultVideo::RETREAT:
  620. musicName = AudioPath::builtin("Music/Retreat Battle");
  621. videoName = VideoPath::builtin("RTSTART.BIK");
  622. break;
  623. case BattleResultVideo::DEFEAT:
  624. musicName = AudioPath::builtin("Music/LoseCombat");
  625. videoName = VideoPath::builtin("LBSTART.BIK");
  626. break;
  627. case BattleResultVideo::DEFEAT_SIEGE:
  628. musicName = AudioPath::builtin("Music/LoseCastle");
  629. videoName = VideoPath::builtin("LOSECSTL.BIK");
  630. break;
  631. case BattleResultVideo::WIN_SIEGE:
  632. musicName = AudioPath::builtin("Music/Defend Castle");
  633. videoName = VideoPath::builtin("DEFENDALL.BIK");
  634. break;
  635. }
  636. }
  637. else
  638. {
  639. switch(currentVideo)
  640. {
  641. case BattleResultVideo::RETREAT:
  642. currentVideo = BattleResultVideo::RETREAT_LOOP;
  643. videoName = VideoPath::builtin("RTLOOP.BIK");
  644. break;
  645. case BattleResultVideo::DEFEAT:
  646. currentVideo = BattleResultVideo::DEFEAT_LOOP;
  647. videoName = VideoPath::builtin("LBLOOP.BIK");
  648. break;
  649. case BattleResultVideo::DEFEAT_SIEGE:
  650. currentVideo = BattleResultVideo::DEFEAT_SIEGE_LOOP;
  651. videoName = VideoPath::builtin("LOSECSLP.BIK");
  652. break;
  653. case BattleResultVideo::WIN_SIEGE:
  654. currentVideo = BattleResultVideo::WIN_SIEGE_LOOP;
  655. videoName = VideoPath::builtin("DEFENDLOOP.BIK");
  656. break;
  657. }
  658. }
  659. if(musicName != AudioPath())
  660. CCS->musich->playMusic(musicName, false, true);
  661. if(videoName != VideoPath())
  662. CCS->videoh->open(videoName);
  663. }
  664. void BattleResultWindow::buttonPressed(int button)
  665. {
  666. if (resultCallback)
  667. resultCallback(button);
  668. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  669. close();
  670. if(GH.windows().topWindow<BattleWindow>())
  671. GH.windows().popWindows(1); //pop battle interface if present
  672. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  673. //so we can be sure that there is no dialogs left on GUI stack.
  674. intTmp.showingDialog->setn(false);
  675. CCS->videoh->close();
  676. }
  677. void BattleResultWindow::bExitf()
  678. {
  679. buttonPressed(0);
  680. }
  681. void BattleResultWindow::bRepeatf()
  682. {
  683. buttonPressed(1);
  684. }
  685. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  686. : embedded(Embedded),
  687. owner(owner)
  688. {
  689. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  690. if(embedded)
  691. {
  692. pos.w = QUEUE_SIZE * 41;
  693. pos.h = 49;
  694. pos.x += parent->pos.w/2 - pos.w/2;
  695. pos.y += 10;
  696. icons = GH.renderHandler().loadAnimation(AnimationPath::builtin("CPRSMALL"));
  697. stateIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("VCMI/BATTLEQUEUE/STATESSMALL"));
  698. }
  699. else
  700. {
  701. pos.w = 800;
  702. pos.h = 85;
  703. pos.x += 0;
  704. pos.y -= pos.h;
  705. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, pos.w, pos.h));
  706. icons = GH.renderHandler().loadAnimation(AnimationPath::builtin("TWCRPORT"));
  707. stateIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("VCMI/BATTLEQUEUE/STATESSMALL"));
  708. //TODO: where use big icons?
  709. //stateIcons = GH.renderHandler().loadAnimation("VCMI/BATTLEQUEUE/STATESBIG");
  710. }
  711. stateIcons->preload();
  712. stackBoxes.resize(QUEUE_SIZE);
  713. for (int i = 0; i < stackBoxes.size(); i++)
  714. {
  715. stackBoxes[i] = std::make_shared<StackBox>(this);
  716. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  717. }
  718. }
  719. void StackQueue::show(Canvas & to)
  720. {
  721. if (embedded)
  722. showAll(to);
  723. CIntObject::show(to);
  724. }
  725. void StackQueue::update()
  726. {
  727. std::vector<battle::Units> queueData;
  728. owner.getBattle()->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  729. size_t boxIndex = 0;
  730. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  731. {
  732. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  733. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  734. }
  735. for(; boxIndex < stackBoxes.size(); boxIndex++)
  736. stackBoxes[boxIndex]->setUnit(nullptr);
  737. }
  738. int32_t StackQueue::getSiegeShooterIconID()
  739. {
  740. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  741. }
  742. std::optional<uint32_t> StackQueue::getHoveredUnitIdIfAny() const
  743. {
  744. for(const auto & stackBox : stackBoxes)
  745. {
  746. if(stackBox->isHovered())
  747. {
  748. return stackBox->getBoundUnitID();
  749. }
  750. }
  751. return std::nullopt;
  752. }
  753. StackQueue::StackBox::StackBox(StackQueue * owner):
  754. CIntObject(SHOW_POPUP | HOVER), owner(owner)
  755. {
  756. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  757. background = std::make_shared<CPicture>(ImagePath::builtin(owner->embedded ? "StackQueueSmall" : "StackQueueLarge"));
  758. pos.w = background->pos.w;
  759. pos.h = background->pos.h;
  760. if(owner->embedded)
  761. {
  762. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  763. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  764. }
  765. else
  766. {
  767. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  768. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  769. int icon_x = pos.w - 17;
  770. int icon_y = pos.h - 18;
  771. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  772. stateIcon->visible = false;
  773. }
  774. }
  775. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  776. {
  777. if(unit)
  778. {
  779. boundUnitID = unit->unitId();
  780. background->colorize(unit->unitOwner());
  781. icon->visible = true;
  782. // temporary code for mod compatibility:
  783. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  784. // second, try to switch to icon that should be provided by mod
  785. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  786. // for 1.2 release & later next line should be moved into 'else' block
  787. icon->setFrame(unit->creatureIconIndex(), 0);
  788. if (unit->unitType()->getId() == CreatureID::ARROW_TOWERS)
  789. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  790. amount->setText(TextOperations::formatMetric(unit->getCount(), 4));
  791. if(stateIcon)
  792. {
  793. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  794. {
  795. stateIcon->setFrame(0, 0);
  796. stateIcon->visible = true;
  797. }
  798. else if(unit->waited((int)turn))
  799. {
  800. stateIcon->setFrame(1, 0);
  801. stateIcon->visible = true;
  802. }
  803. else
  804. {
  805. stateIcon->visible = false;
  806. }
  807. }
  808. }
  809. else
  810. {
  811. boundUnitID = std::nullopt;
  812. background->colorize(PlayerColor::NEUTRAL);
  813. icon->visible = false;
  814. icon->setFrame(0);
  815. amount->setText("");
  816. if(stateIcon)
  817. stateIcon->visible = false;
  818. }
  819. }
  820. std::optional<uint32_t> StackQueue::StackBox::getBoundUnitID() const
  821. {
  822. return boundUnitID;
  823. }
  824. bool StackQueue::StackBox::isBoundUnitHighlighted() const
  825. {
  826. auto unitIdsToHighlight = owner->owner.stacksController->getHoveredStacksUnitIds();
  827. return vstd::contains(unitIdsToHighlight, getBoundUnitID());
  828. }
  829. void StackQueue::StackBox::showAll(Canvas & to)
  830. {
  831. CIntObject::showAll(to);
  832. if(isBoundUnitHighlighted())
  833. to.drawBorder(background->pos, Colors::CYAN, 2);
  834. }
  835. void StackQueue::StackBox::show(Canvas & to)
  836. {
  837. CIntObject::show(to);
  838. if(isBoundUnitHighlighted())
  839. to.drawBorder(background->pos, Colors::CYAN, 2);
  840. }