2
0

BattleInterfaceClasses.cpp 33 KB

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