BattleInterfaceClasses.cpp 24 KB

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