BattleInterfaceClasses.cpp 24 KB

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