BattleInterfaceClasses.cpp 24 KB

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