BattleInterfaceClasses.cpp 33 KB

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