BattleInterfaceClasses.cpp 24 KB

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