BattleInterfaceClasses.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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/CCreatureWindow.h"
  32. #include "../windows/CSpellWindow.h"
  33. #include "../render/CAnimation.h"
  34. #include "../adventureMap/CInGameConsole.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 <SDL_surface.h>
  48. #include <SDL_events.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 (auto const & 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 (auto const & 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. CSDL_Ext::startTextInput(pos);
  140. }
  141. else
  142. {
  143. assert(enteringText == true);
  144. CSDL_Ext::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::hover(bool on)
  229. {
  230. //TODO: BROKEN CODE
  231. if (on)
  232. CCS->curh->set(Cursor::Combat::HERO);
  233. else
  234. CCS->curh->set(Cursor::Combat::POINTER);
  235. }
  236. void BattleHero::clickLeft(tribool down, bool previousState)
  237. {
  238. if(owner.actionsController->spellcastingModeActive()) //we are casting a spell
  239. return;
  240. if(boost::logic::indeterminate(down))
  241. return;
  242. if(!hero || down || !owner.myTurn)
  243. return;
  244. if(owner.getCurrentPlayerInterface()->cb->battleCanCastSpell(hero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
  245. {
  246. BattleHex hoveredHex = owner.fieldController->getHoveredHex();
  247. //do nothing when any hex is hovered - hero's animation overlaps battlefield
  248. if ( hoveredHex != BattleHex::INVALID )
  249. return;
  250. CCS->curh->set(Cursor::Map::POINTER);
  251. GH.pushIntT<CSpellWindow>(hero, owner.getCurrentPlayerInterface());
  252. }
  253. }
  254. void BattleHero::clickRight(tribool down, bool previousState)
  255. {
  256. if(boost::logic::indeterminate(down))
  257. return;
  258. Point windowPosition;
  259. windowPosition.x = (!defender) ? owner.fieldController->pos.left() + 1 : owner.fieldController->pos.right() - 79;
  260. windowPosition.y = owner.fieldController->pos.y + 135;
  261. InfoAboutHero targetHero;
  262. if(down && (owner.myTurn || settings["session"]["spectate"].Bool()))
  263. {
  264. auto h = defender ? owner.defendingHeroInstance : owner.attackingHeroInstance;
  265. targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
  266. GH.pushIntT<HeroInfoWindow>(targetHero, &windowPosition);
  267. }
  268. }
  269. void BattleHero::switchToNextPhase()
  270. {
  271. phase = nextPhase;
  272. currentFrame = 0.f;
  273. auto copy = phaseFinishedCallback;
  274. phaseFinishedCallback.clear();
  275. copy();
  276. }
  277. BattleHero::BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender):
  278. defender(defender),
  279. hero(hero),
  280. owner(owner),
  281. phase(EHeroAnimType::HOLDING),
  282. nextPhase(EHeroAnimType::HOLDING),
  283. currentSpeed(0.f),
  284. currentFrame(0.f),
  285. flagCurrentFrame(0.f)
  286. {
  287. std::string animationPath;
  288. if(!hero->type->battleImage.empty())
  289. animationPath = hero->type->battleImage;
  290. else
  291. if(hero->sex)
  292. animationPath = hero->type->heroClass->imageBattleFemale;
  293. else
  294. animationPath = hero->type->heroClass->imageBattleMale;
  295. animation = std::make_shared<CAnimation>(animationPath);
  296. animation->preload();
  297. pos.w = 64;
  298. pos.h = 136;
  299. pos.x = owner.fieldController->pos.x + (defender ? (owner.fieldController->pos.w - pos.w) : 0);
  300. pos.y = owner.fieldController->pos.y;
  301. if(defender)
  302. animation->verticalFlip();
  303. if(defender)
  304. flagAnimation = std::make_shared<CAnimation>("CMFLAGR");
  305. else
  306. flagAnimation = std::make_shared<CAnimation>("CMFLAGL");
  307. flagAnimation->preload();
  308. flagAnimation->playerColored(hero->tempOwner);
  309. addUsedEvents(LCLICK | RCLICK | HOVER);
  310. switchToNextPhase();
  311. play();
  312. }
  313. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  314. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, "CHRPOP")
  315. {
  316. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  317. if (position != nullptr)
  318. moveTo(*position);
  319. background->colorize(hero.owner); //maybe add this functionality to base class?
  320. auto attack = hero.details->primskills[0];
  321. auto defense = hero.details->primskills[1];
  322. auto power = hero.details->primskills[2];
  323. auto knowledge = hero.details->primskills[3];
  324. auto morale = hero.details->morale;
  325. auto luck = hero.details->luck;
  326. auto currentSpellPoints = hero.details->mana;
  327. auto maxSpellPoints = hero.details->manaLimit;
  328. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 10, 6));
  329. //primary stats
  330. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  331. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  332. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":"));
  333. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":"));
  334. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack)));
  335. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense)));
  336. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power)));
  337. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge)));
  338. //morale+luck
  339. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  340. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  341. icons.push_back(std::make_shared<CAnimImage>("IMRL22", morale + 3, 0, 47, 131));
  342. icons.push_back(std::make_shared<CAnimImage>("ILCK22", luck + 3, 0, 47, 143));
  343. //spell points
  344. labels.push_back(std::make_shared<CLabel>(39, 174, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]));
  345. labels.push_back(std::make_shared<CLabel>(39, 186, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints)));
  346. }
  347. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner)
  348. : owner(_owner)
  349. {
  350. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  351. pos = CSDL_Ext::genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  352. background = std::make_shared<CPicture>("CPRESULT");
  353. background->colorize(owner.playerID);
  354. exit = std::make_shared<CButton>(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, SDLK_RETURN);
  355. exit->setBorderColor(Colors::METALLIC_GOLD);
  356. if(br.winner == 0) //attacker won
  357. {
  358. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  359. }
  360. else
  361. {
  362. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  363. }
  364. if(br.winner == 1)
  365. {
  366. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  367. }
  368. else
  369. {
  370. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  371. }
  372. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  373. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  374. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  375. std::string sideNames[2] = {"N/A", "N/A"};
  376. for(int i = 0; i < 2; i++)
  377. {
  378. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  379. const int xs[] = {21, 392};
  380. if(heroInfo.portrait >= 0) //attacking hero
  381. {
  382. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38));
  383. sideNames[i] = heroInfo.name;
  384. }
  385. else
  386. {
  387. auto stacks = owner.cb->battleGetAllStacks();
  388. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  389. {
  390. return stack->side != i || !stack->base;
  391. });
  392. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  393. {
  394. return stack->type->AIValue;
  395. });
  396. if(best != stacks.end()) //should be always but to be safe...
  397. {
  398. icons.push_back(std::make_shared<CAnimImage>("TWCRPORT", (*best)->type->getIconIndex(), 0, xs[i], 38));
  399. sideNames[i] = (*best)->type->getNamePluralTranslated();
  400. }
  401. }
  402. }
  403. //printing attacker and defender's names
  404. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  405. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  406. //printing casualties
  407. for(int step = 0; step < 2; ++step)
  408. {
  409. if(br.casualties[step].size()==0)
  410. {
  411. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  412. }
  413. else
  414. {
  415. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  416. int yPos = 344 + step * 97;
  417. for(auto & elem : br.casualties[step])
  418. {
  419. auto creature = CGI->creatures()->getByIndex(elem.first);
  420. if (creature->getId() == CreatureID::ARROW_TOWERS )
  421. continue; // do not show destroyed towers in battle results
  422. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, xPos, yPos));
  423. std::ostringstream amount;
  424. amount<<elem.second;
  425. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  426. xPos += 42;
  427. }
  428. }
  429. }
  430. //printing result description
  431. bool weAreAttacker = !(owner.cb->battleGetMySide());
  432. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  433. {
  434. int text = 304;
  435. switch(br.result)
  436. {
  437. case BattleResult::NORMAL:
  438. break;
  439. case BattleResult::ESCAPE:
  440. text = 303;
  441. break;
  442. case BattleResult::SURRENDER:
  443. text = 302;
  444. break;
  445. default:
  446. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  447. break;
  448. }
  449. CCS->musich->playMusic("Music/Win Battle", false, true);
  450. CCS->videoh->open("WIN3.BIK");
  451. std::string str = CGI->generaltexth->allTexts[text];
  452. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  453. if (ourHero)
  454. {
  455. str += CGI->generaltexth->allTexts[305];
  456. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  457. boost::algorithm::replace_first(str, "%d", boost::lexical_cast<std::string>(br.exp[weAreAttacker ? 0 : 1]));
  458. }
  459. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  460. }
  461. else // we lose
  462. {
  463. int text = 311;
  464. std::string musicName = "Music/LoseCombat";
  465. std::string videoName = "LBSTART.BIK";
  466. switch(br.result)
  467. {
  468. case BattleResult::NORMAL:
  469. break;
  470. case BattleResult::ESCAPE:
  471. musicName = "Music/Retreat Battle";
  472. videoName = "RTSTART.BIK";
  473. text = 310;
  474. break;
  475. case BattleResult::SURRENDER:
  476. musicName = "Music/Surrender Battle";
  477. videoName = "SURRENDER.BIK";
  478. text = 309;
  479. break;
  480. default:
  481. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  482. break;
  483. }
  484. CCS->musich->playMusic(musicName, false, true);
  485. CCS->videoh->open(videoName);
  486. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  487. }
  488. }
  489. void BattleResultWindow::activate()
  490. {
  491. owner.showingDialog->set(true);
  492. CIntObject::activate();
  493. }
  494. void BattleResultWindow::show(SDL_Surface * to)
  495. {
  496. CIntObject::show(to);
  497. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  498. }
  499. void BattleResultWindow::bExitf()
  500. {
  501. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  502. close();
  503. if(dynamic_cast<BattleWindow*>(GH.topInt().get()))
  504. GH.popInts(1); //pop battle interface if present
  505. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  506. //so we can be sure that there is no dialogs left on GUI stack.
  507. intTmp.showingDialog->setn(false);
  508. CCS->videoh->close();
  509. }
  510. void ClickableHex::hover(bool on)
  511. {
  512. hovered = on;
  513. //Hoverable::hover(on);
  514. if(!on && setAlterText)
  515. {
  516. GH.statusbar->clear();
  517. setAlterText = false;
  518. }
  519. }
  520. ClickableHex::ClickableHex() : setAlterText(false), myNumber(-1), strictHovered(false), myInterface(nullptr)
  521. {
  522. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  523. }
  524. void ClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  525. {
  526. strictHovered = myInterface->fieldController->isPixelInHex(Point(sEvent.x-pos.x, sEvent.y-pos.y));
  527. if(hovered && strictHovered) //print attacked creature to console
  528. {
  529. const CStack * attackedStack = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber);
  530. if( attackedStack != nullptr &&
  531. attackedStack->owner != myInterface->getCurrentPlayerInterface()->playerID &&
  532. attackedStack->alive())
  533. {
  534. MetaString text;
  535. text.addTxt(MetaString::GENERAL_TXT, 220);
  536. attackedStack->addNameReplacement(text);
  537. GH.statusbar->write(text.toString());
  538. setAlterText = true;
  539. }
  540. }
  541. else if(setAlterText)
  542. {
  543. GH.statusbar->clear();
  544. setAlterText = false;
  545. }
  546. }
  547. void ClickableHex::clickLeft(tribool down, bool previousState)
  548. {
  549. if(!down && hovered && strictHovered) //we've been really clicked!
  550. {
  551. myInterface->actionsController->handleHex(myNumber, LCLICK);
  552. }
  553. }
  554. void ClickableHex::clickRight(tribool down, bool previousState)
  555. {
  556. const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
  557. if(hovered && strictHovered && myst!=nullptr)
  558. {
  559. if(!myst->alive()) return;
  560. if(down)
  561. {
  562. GH.pushIntT<CStackWindow>(myst, true);
  563. }
  564. }
  565. }
  566. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  567. : embedded(Embedded),
  568. owner(owner)
  569. {
  570. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  571. if(embedded)
  572. {
  573. pos.w = QUEUE_SIZE * 41;
  574. pos.h = 49;
  575. pos.x += parent->pos.w/2 - pos.w/2;
  576. pos.y += 10;
  577. icons = std::make_shared<CAnimation>("CPRSMALL");
  578. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  579. }
  580. else
  581. {
  582. pos.w = 800;
  583. pos.h = 85;
  584. pos.x += 0;
  585. pos.y -= pos.h;
  586. background = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, 0, pos.w, pos.h));
  587. icons = std::make_shared<CAnimation>("TWCRPORT");
  588. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  589. //TODO: where use big icons?
  590. //stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESBIG");
  591. }
  592. stateIcons->preload();
  593. stackBoxes.resize(QUEUE_SIZE);
  594. for (int i = 0; i < stackBoxes.size(); i++)
  595. {
  596. stackBoxes[i] = std::make_shared<StackBox>(this);
  597. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  598. }
  599. }
  600. void StackQueue::show(SDL_Surface * to)
  601. {
  602. if (embedded)
  603. showAll(to);
  604. CIntObject::show(to);
  605. }
  606. void StackQueue::update()
  607. {
  608. std::vector<battle::Units> queueData;
  609. owner.getCurrentPlayerInterface()->cb->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  610. size_t boxIndex = 0;
  611. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  612. {
  613. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  614. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  615. }
  616. for(; boxIndex < stackBoxes.size(); boxIndex++)
  617. stackBoxes[boxIndex]->setUnit(nullptr);
  618. }
  619. int32_t StackQueue::getSiegeShooterIconID()
  620. {
  621. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  622. }
  623. StackQueue::StackBox::StackBox(StackQueue * owner):
  624. owner(owner)
  625. {
  626. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  627. background = std::make_shared<CPicture>(owner->embedded ? "StackQueueSmall" : "StackQueueLarge");
  628. pos.w = background->pos.w;
  629. pos.h = background->pos.h;
  630. if(owner->embedded)
  631. {
  632. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  633. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  634. }
  635. else
  636. {
  637. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  638. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  639. int icon_x = pos.w - 17;
  640. int icon_y = pos.h - 18;
  641. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  642. stateIcon->visible = false;
  643. }
  644. }
  645. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  646. {
  647. if(unit)
  648. {
  649. background->colorize(unit->unitOwner());
  650. icon->visible = true;
  651. // temporary code for mod compatibility:
  652. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  653. // second, try to switch to icon that should be provided by mod
  654. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  655. // for 1.2 release & later next line should be moved into 'else' block
  656. icon->setFrame(unit->creatureIconIndex(), 0);
  657. if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS)
  658. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  659. amount->setText(CSDL_Ext::makeNumberShort(unit->getCount(), 4));
  660. if(stateIcon)
  661. {
  662. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  663. {
  664. stateIcon->setFrame(0, 0);
  665. stateIcon->visible = true;
  666. }
  667. else if(unit->waited((int)turn))
  668. {
  669. stateIcon->setFrame(1, 0);
  670. stateIcon->visible = true;
  671. }
  672. else
  673. {
  674. stateIcon->visible = false;
  675. }
  676. }
  677. }
  678. else
  679. {
  680. background->colorize(PlayerColor::NEUTRAL);
  681. icon->visible = false;
  682. icon->setFrame(0);
  683. amount->setText("");
  684. if(stateIcon)
  685. stateIcon->visible = false;
  686. }
  687. }