BattleInterfaceClasses.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 "../CMessage.h"
  21. #include "../CMusicHandler.h"
  22. #include "../CPlayerInterface.h"
  23. #include "../CVideoHandler.h"
  24. #include "../Graphics.h"
  25. #include "../gui/CAnimation.h"
  26. #include "../gui/Canvas.h"
  27. #include "../gui/CCursorHandler.h"
  28. #include "../gui/CGuiHandler.h"
  29. #include "../widgets/AdventureMapClasses.h"
  30. #include "../widgets/Buttons.h"
  31. #include "../widgets/Images.h"
  32. #include "../widgets/TextControls.h"
  33. #include "../windows/CCreatureWindow.h"
  34. #include "../windows/CSpellWindow.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. void BattleConsole::showAll(SDL_Surface * to)
  48. {
  49. CIntObject::showAll(to);
  50. Point consolePos(pos.x + 10, pos.y + 19);
  51. Point textPos (pos.x + pos.w/2, pos.y + 19);
  52. if (!consoleText.empty())
  53. {
  54. graphics->fonts[FONT_SMALL]->renderTextLinesLeft(to, CMessage::breakText(consoleText, pos.w, FONT_SMALL), Colors::WHITE, consolePos);
  55. }
  56. else if(!hoverText.empty())
  57. {
  58. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(hoverText, pos.w, FONT_SMALL), Colors::WHITE, textPos);
  59. }
  60. else if(logEntries.size())
  61. {
  62. if(logEntries.size()==1)
  63. {
  64. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(logEntries[0], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  65. }
  66. else
  67. {
  68. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(logEntries[scrollPosition - 1], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  69. textPos.y += 16;
  70. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(logEntries[scrollPosition], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  71. }
  72. }
  73. }
  74. bool BattleConsole::addText(const std::string & text)
  75. {
  76. logGlobal->trace("CBattleConsole message: %s", text);
  77. if(text.size()>70)
  78. return false; //text too long!
  79. int firstInToken = 0;
  80. for(size_t i = 0; i < text.size(); ++i) //tokenize
  81. {
  82. if(text[i] == 10)
  83. {
  84. logEntries.push_back( text.substr(firstInToken, i-firstInToken) );
  85. firstInToken = (int)i+1;
  86. }
  87. }
  88. logEntries.push_back( text.substr(firstInToken, text.size()) );
  89. scrollPosition = (int)logEntries.size()-1;
  90. redraw();
  91. return true;
  92. }
  93. void BattleConsole::scrollUp(ui32 by)
  94. {
  95. if(scrollPosition > static_cast<int>(by))
  96. scrollPosition -= by;
  97. redraw();
  98. }
  99. void BattleConsole::scrollDown(ui32 by)
  100. {
  101. if(scrollPosition + by < logEntries.size())
  102. scrollPosition += by;
  103. redraw();
  104. }
  105. BattleConsole::BattleConsole(std::shared_ptr<CPicture> backgroundSource, const Point & objectPos, const Point & imagePos, const Point &size)
  106. : scrollPosition(-1)
  107. , enteringText(false)
  108. {
  109. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  110. pos += objectPos;
  111. pos.w = size.x;
  112. pos.h = size.y;
  113. background = std::make_shared<CPicture>(backgroundSource->getSurface(), Rect(imagePos, size), 0, 0 );
  114. }
  115. void BattleConsole::deactivate()
  116. {
  117. if (enteringText)
  118. LOCPLINT->cingconsole->endEnteringText(false);
  119. CIntObject::deactivate();
  120. }
  121. void BattleConsole::setEnteringMode(bool on)
  122. {
  123. consoleText.clear();
  124. if (on)
  125. {
  126. assert(enteringText == false);
  127. CSDL_Ext::startTextInput(&pos);
  128. }
  129. else
  130. {
  131. assert(enteringText == true);
  132. CSDL_Ext::stopTextInput();
  133. }
  134. enteringText = on;
  135. }
  136. void BattleConsole::setEnteredText(const std::string & text)
  137. {
  138. assert(enteringText == true);
  139. consoleText = text;
  140. redraw();
  141. }
  142. void BattleConsole::write(const std::string & Text)
  143. {
  144. hoverText = Text;
  145. redraw();
  146. }
  147. void BattleConsole::clearIfMatching(const std::string & Text)
  148. {
  149. if (hoverText == Text)
  150. clear();
  151. }
  152. void BattleConsole::clear()
  153. {
  154. write({});
  155. }
  156. const CGHeroInstance * BattleHero::instance()
  157. {
  158. return hero;
  159. }
  160. void BattleHero::render(Canvas & canvas)
  161. {
  162. size_t groupIndex = static_cast<size_t>(phase);
  163. auto flagFrame = flagAnimation->getImage(flagCurrentFrame, 0, true);
  164. auto heroFrame = animation->getImage(currentFrame, groupIndex, true);
  165. Point heroPosition = pos.center() - heroFrame->dimensions() / 2;
  166. Point flagPosition = pos.center() - flagFrame->dimensions() / 2;
  167. if(defender)
  168. flagPosition += Point(-4, -41);
  169. else
  170. flagPosition += Point(4, -41);
  171. canvas.draw(flagFrame, flagPosition);
  172. canvas.draw(heroFrame, heroPosition);
  173. flagCurrentFrame += currentSpeed;
  174. currentFrame += currentSpeed;
  175. if(flagCurrentFrame >= flagAnimation->size(0))
  176. flagCurrentFrame -= flagAnimation->size(0);
  177. if(currentFrame >= animation->size(groupIndex))
  178. {
  179. currentFrame -= animation->size(groupIndex);
  180. switchToNextPhase();
  181. }
  182. }
  183. void BattleHero::pause()
  184. {
  185. currentSpeed = 0.f;
  186. }
  187. void BattleHero::play()
  188. {
  189. //FIXME: un-hardcode speed
  190. currentSpeed = 0.25f;
  191. }
  192. float BattleHero::getFrame() const
  193. {
  194. return currentFrame;
  195. }
  196. void BattleHero::collectRenderableObjects(BattleRenderer & renderer)
  197. {
  198. auto hex = defender ? BattleHex(GameConstants::BFIELD_WIDTH-1) : BattleHex(0);
  199. renderer.insert(EBattleFieldLayer::HEROES, hex, [this](BattleRenderer::RendererRef canvas)
  200. {
  201. render(canvas);
  202. });
  203. }
  204. void BattleHero::onPhaseFinished(const std::function<void()> & callback)
  205. {
  206. phaseFinishedCallback = callback;
  207. }
  208. void BattleHero::setPhase(EHeroAnimType newPhase)
  209. {
  210. nextPhase = newPhase;
  211. switchToNextPhase(); //immediately switch to next phase and then restore idling phase
  212. nextPhase = EHeroAnimType::HOLDING;
  213. }
  214. void BattleHero::hover(bool on)
  215. {
  216. //TODO: BROKEN CODE
  217. if (on)
  218. CCS->curh->set(Cursor::Combat::HERO);
  219. else
  220. CCS->curh->set(Cursor::Combat::POINTER);
  221. }
  222. void BattleHero::clickLeft(tribool down, bool previousState)
  223. {
  224. if(owner.actionsController->spellcastingModeActive()) //we are casting a spell
  225. return;
  226. if(boost::logic::indeterminate(down))
  227. return;
  228. if(!hero || down || !owner.myTurn)
  229. return;
  230. if(owner.getCurrentPlayerInterface()->cb->battleCanCastSpell(hero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
  231. {
  232. BattleHex hoveredHex = owner.fieldController->getHoveredHex();
  233. //do nothing when any hex is hovered - hero's animation overlaps battlefield
  234. if ( hoveredHex != BattleHex::INVALID )
  235. return;
  236. CCS->curh->set(Cursor::Map::POINTER);
  237. GH.pushIntT<CSpellWindow>(hero, owner.getCurrentPlayerInterface());
  238. }
  239. }
  240. void BattleHero::clickRight(tribool down, bool previousState)
  241. {
  242. if(boost::logic::indeterminate(down))
  243. return;
  244. Point windowPosition;
  245. windowPosition.x = (!defender) ? owner.fieldController->pos.topLeft().x + 1 : owner.fieldController->pos.topRight().x - 79;
  246. windowPosition.y = owner.fieldController->pos.y + 135;
  247. InfoAboutHero targetHero;
  248. if(down && (owner.myTurn || settings["session"]["spectate"].Bool()))
  249. {
  250. auto h = defender ? owner.defendingHeroInstance : owner.attackingHeroInstance;
  251. targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
  252. GH.pushIntT<HeroInfoWindow>(targetHero, &windowPosition);
  253. }
  254. }
  255. void BattleHero::switchToNextPhase()
  256. {
  257. phase = nextPhase;
  258. currentFrame = 0.f;
  259. auto copy = phaseFinishedCallback;
  260. phaseFinishedCallback.clear();
  261. copy();
  262. }
  263. BattleHero::BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender):
  264. defender(defender),
  265. hero(hero),
  266. owner(owner),
  267. phase(EHeroAnimType::HOLDING),
  268. nextPhase(EHeroAnimType::HOLDING),
  269. currentSpeed(0.f),
  270. currentFrame(0.f),
  271. flagCurrentFrame(0.f)
  272. {
  273. std::string animationPath;
  274. if(!hero->type->battleImage.empty())
  275. animationPath = hero->type->battleImage;
  276. else
  277. if(hero->sex)
  278. animationPath = hero->type->heroClass->imageBattleFemale;
  279. else
  280. animationPath = hero->type->heroClass->imageBattleMale;
  281. animation = std::make_shared<CAnimation>(animationPath);
  282. animation->preload();
  283. pos.w = 64;
  284. pos.h = 136;
  285. pos.x = owner.fieldController->pos.x + (defender ? (owner.fieldController->pos.w - pos.w) : 0);
  286. pos.y = owner.fieldController->pos.y;
  287. if(defender)
  288. animation->verticalFlip();
  289. if(defender)
  290. flagAnimation = std::make_shared<CAnimation>("CMFLAGR");
  291. else
  292. flagAnimation = std::make_shared<CAnimation>("CMFLAGL");
  293. flagAnimation->preload();
  294. flagAnimation->playerColored(hero->tempOwner);
  295. addUsedEvents(LCLICK | RCLICK | HOVER);
  296. switchToNextPhase();
  297. play();
  298. }
  299. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  300. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, "CHRPOP")
  301. {
  302. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  303. if (position != nullptr)
  304. moveTo(*position);
  305. background->colorize(hero.owner); //maybe add this functionality to base class?
  306. auto attack = hero.details->primskills[0];
  307. auto defense = hero.details->primskills[1];
  308. auto power = hero.details->primskills[2];
  309. auto knowledge = hero.details->primskills[3];
  310. auto morale = hero.details->morale;
  311. auto luck = hero.details->luck;
  312. auto currentSpellPoints = hero.details->mana;
  313. auto maxSpellPoints = hero.details->manaLimit;
  314. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 10, 6));
  315. //primary stats
  316. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  317. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  318. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":"));
  319. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":"));
  320. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack)));
  321. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense)));
  322. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power)));
  323. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge)));
  324. //morale+luck
  325. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  326. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  327. icons.push_back(std::make_shared<CAnimImage>("IMRL22", morale + 3, 0, 47, 131));
  328. icons.push_back(std::make_shared<CAnimImage>("ILCK22", luck + 3, 0, 47, 143));
  329. //spell points
  330. labels.push_back(std::make_shared<CLabel>(39, 174, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]));
  331. labels.push_back(std::make_shared<CLabel>(39, 186, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints)));
  332. }
  333. BattleOptionsWindow::BattleOptionsWindow(BattleInterface & owner):
  334. CWindowObject(PLAYER_COLORED, "comopbck.bmp")
  335. {
  336. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  337. auto viewGrid = std::make_shared<CToggleButton>(Point(25, 56), "sysopchk.def", CGI->generaltexth->zelp[427], [&](bool on){owner.setPrintCellBorders(on);} );
  338. viewGrid->setSelected(settings["battle"]["cellBorders"].Bool());
  339. toggles.push_back(viewGrid);
  340. auto movementShadow = std::make_shared<CToggleButton>(Point(25, 89), "sysopchk.def", CGI->generaltexth->zelp[428], [&](bool on){owner.setPrintStackRange(on);});
  341. movementShadow->setSelected(settings["battle"]["stackRange"].Bool());
  342. toggles.push_back(movementShadow);
  343. auto mouseShadow = std::make_shared<CToggleButton>(Point(25, 122), "sysopchk.def", CGI->generaltexth->zelp[429], [&](bool on){owner.setPrintMouseShadow(on);});
  344. mouseShadow->setSelected(settings["battle"]["mouseShadow"].Bool());
  345. toggles.push_back(mouseShadow);
  346. animSpeeds = std::make_shared<CToggleGroup>([&](int value){ owner.setAnimSpeed(value);});
  347. std::shared_ptr<CToggleButton> toggle;
  348. toggle = std::make_shared<CToggleButton>(Point( 28, 225), "sysopb9.def", CGI->generaltexth->zelp[422]);
  349. animSpeeds->addToggle(40, toggle);
  350. toggle = std::make_shared<CToggleButton>(Point( 92, 225), "sysob10.def", CGI->generaltexth->zelp[423]);
  351. animSpeeds->addToggle(63, toggle);
  352. toggle = std::make_shared<CToggleButton>(Point(156, 225), "sysob11.def", CGI->generaltexth->zelp[424]);
  353. animSpeeds->addToggle(100, toggle);
  354. animSpeeds->setSelected(owner.getAnimSpeed());
  355. setToDefault = std::make_shared<CButton>(Point(246, 359), "codefaul.def", CGI->generaltexth->zelp[393], [&](){ bDefaultf(); });
  356. setToDefault->setImageOrder(1, 0, 2, 3);
  357. exit = std::make_shared<CButton>(Point(357, 359), "soretrn.def", CGI->generaltexth->zelp[392], [&](){ bExitf();}, SDLK_RETURN);
  358. exit->setImageOrder(1, 0, 2, 3);
  359. //creating labels
  360. labels.push_back(std::make_shared<CLabel>(242, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[392]));//window title
  361. labels.push_back(std::make_shared<CLabel>(122, 214, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[393]));//animation speed
  362. labels.push_back(std::make_shared<CLabel>(122, 293, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[394]));//music volume
  363. labels.push_back(std::make_shared<CLabel>(122, 359, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[395]));//effects' volume
  364. labels.push_back(std::make_shared<CLabel>(353, 66, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[396]));//auto - combat options
  365. labels.push_back(std::make_shared<CLabel>(353, 265, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[397]));//creature info
  366. //auto - combat options
  367. labels.push_back(std::make_shared<CLabel>(283, 86, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[398]));//creatures
  368. labels.push_back(std::make_shared<CLabel>(283, 116, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[399]));//spells
  369. labels.push_back(std::make_shared<CLabel>(283, 146, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[400]));//catapult
  370. labels.push_back(std::make_shared<CLabel>(283, 176, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[151]));//ballista
  371. labels.push_back(std::make_shared<CLabel>(283, 206, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[401]));//first aid tent
  372. //creature info
  373. labels.push_back(std::make_shared<CLabel>(283, 285, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[402]));//all stats
  374. labels.push_back(std::make_shared<CLabel>(283, 315, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[403]));//spells only
  375. //general options
  376. labels.push_back(std::make_shared<CLabel>(61, 57, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[404]));
  377. labels.push_back(std::make_shared<CLabel>(61, 90, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[405]));
  378. labels.push_back(std::make_shared<CLabel>(61, 123, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[406]));
  379. labels.push_back(std::make_shared<CLabel>(61, 156, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[407]));
  380. }
  381. void BattleOptionsWindow::bDefaultf()
  382. {
  383. //TODO: implement
  384. }
  385. void BattleOptionsWindow::bExitf()
  386. {
  387. close();
  388. }
  389. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner)
  390. : owner(_owner)
  391. {
  392. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  393. pos = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  394. background = std::make_shared<CPicture>("CPRESULT");
  395. background->colorize(owner.playerID);
  396. exit = std::make_shared<CButton>(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, SDLK_RETURN);
  397. exit->setBorderColor(Colors::METALLIC_GOLD);
  398. if(br.winner == 0) //attacker won
  399. {
  400. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  401. }
  402. else
  403. {
  404. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  405. }
  406. if(br.winner == 1)
  407. {
  408. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  409. }
  410. else
  411. {
  412. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  413. }
  414. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  415. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  416. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  417. std::string sideNames[2] = {"N/A", "N/A"};
  418. for(int i = 0; i < 2; i++)
  419. {
  420. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  421. const int xs[] = {21, 392};
  422. if(heroInfo.portrait >= 0) //attacking hero
  423. {
  424. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38));
  425. sideNames[i] = heroInfo.name;
  426. }
  427. else
  428. {
  429. auto stacks = owner.cb->battleGetAllStacks();
  430. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  431. {
  432. return stack->side != i || !stack->base;
  433. });
  434. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  435. {
  436. return stack->type->AIValue;
  437. });
  438. if(best != stacks.end()) //should be always but to be safe...
  439. {
  440. icons.push_back(std::make_shared<CAnimImage>("TWCRPORT", (*best)->type->getIconIndex(), 0, xs[i], 38));
  441. sideNames[i] = (*best)->type->getPluralName();
  442. }
  443. }
  444. }
  445. //printing attacker and defender's names
  446. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  447. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  448. //printing casualties
  449. for(int step = 0; step < 2; ++step)
  450. {
  451. if(br.casualties[step].size()==0)
  452. {
  453. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  454. }
  455. else
  456. {
  457. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  458. int yPos = 344 + step * 97;
  459. for(auto & elem : br.casualties[step])
  460. {
  461. auto creature = CGI->creatures()->getByIndex(elem.first);
  462. if (creature->getId() == CreatureID::ARROW_TOWERS )
  463. continue; // do not show destroyed towers in battle results
  464. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, xPos, yPos));
  465. std::ostringstream amount;
  466. amount<<elem.second;
  467. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  468. xPos += 42;
  469. }
  470. }
  471. }
  472. //printing result description
  473. bool weAreAttacker = !(owner.cb->battleGetMySide());
  474. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  475. {
  476. int text = 304;
  477. switch(br.result)
  478. {
  479. case BattleResult::NORMAL:
  480. break;
  481. case BattleResult::ESCAPE:
  482. text = 303;
  483. break;
  484. case BattleResult::SURRENDER:
  485. text = 302;
  486. break;
  487. default:
  488. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  489. break;
  490. }
  491. CCS->musich->playMusic("Music/Win Battle", false, true);
  492. CCS->videoh->open("WIN3.BIK");
  493. std::string str = CGI->generaltexth->allTexts[text];
  494. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  495. if (ourHero)
  496. {
  497. str += CGI->generaltexth->allTexts[305];
  498. boost::algorithm::replace_first(str, "%s", ourHero->name);
  499. boost::algorithm::replace_first(str, "%d", boost::lexical_cast<std::string>(br.exp[weAreAttacker ? 0 : 1]));
  500. }
  501. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  502. }
  503. else // we lose
  504. {
  505. int text = 311;
  506. std::string musicName = "Music/LoseCombat";
  507. std::string videoName = "LBSTART.BIK";
  508. switch(br.result)
  509. {
  510. case BattleResult::NORMAL:
  511. break;
  512. case BattleResult::ESCAPE:
  513. musicName = "Music/Retreat Battle";
  514. videoName = "RTSTART.BIK";
  515. text = 310;
  516. break;
  517. case BattleResult::SURRENDER:
  518. musicName = "Music/Surrender Battle";
  519. videoName = "SURRENDER.BIK";
  520. text = 309;
  521. break;
  522. default:
  523. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  524. break;
  525. }
  526. CCS->musich->playMusic(musicName, false, true);
  527. CCS->videoh->open(videoName);
  528. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  529. }
  530. }
  531. void BattleResultWindow::activate()
  532. {
  533. owner.showingDialog->set(true);
  534. CIntObject::activate();
  535. }
  536. void BattleResultWindow::show(SDL_Surface * to)
  537. {
  538. CIntObject::show(to);
  539. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  540. }
  541. void BattleResultWindow::bExitf()
  542. {
  543. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  544. close();
  545. if(dynamic_cast<BattleWindow*>(GH.topInt().get()))
  546. GH.popInts(1); //pop battle interface if present
  547. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  548. //so we can be sure that there is no dialogs left on GUI stack.
  549. intTmp.showingDialog->setn(false);
  550. CCS->videoh->close();
  551. }
  552. void ClickableHex::hover(bool on)
  553. {
  554. hovered = on;
  555. //Hoverable::hover(on);
  556. if(!on && setAlterText)
  557. {
  558. GH.statusbar->clear();
  559. setAlterText = false;
  560. }
  561. }
  562. ClickableHex::ClickableHex() : setAlterText(false), myNumber(-1), strictHovered(false), myInterface(nullptr)
  563. {
  564. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  565. }
  566. void ClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  567. {
  568. strictHovered = myInterface->fieldController->isPixelInHex(Point(sEvent.x-pos.x, sEvent.y-pos.y));
  569. if(hovered && strictHovered) //print attacked creature to console
  570. {
  571. const CStack * attackedStack = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber);
  572. if( attackedStack != nullptr &&
  573. attackedStack->owner != myInterface->getCurrentPlayerInterface()->playerID &&
  574. attackedStack->alive())
  575. {
  576. MetaString text;
  577. text.addTxt(MetaString::GENERAL_TXT, 220);
  578. attackedStack->addNameReplacement(text);
  579. GH.statusbar->write(text.toString());
  580. setAlterText = true;
  581. }
  582. }
  583. else if(setAlterText)
  584. {
  585. GH.statusbar->clear();
  586. setAlterText = false;
  587. }
  588. }
  589. void ClickableHex::clickLeft(tribool down, bool previousState)
  590. {
  591. if(!down && hovered && strictHovered) //we've been really clicked!
  592. {
  593. myInterface->actionsController->handleHex(myNumber, LCLICK);
  594. }
  595. }
  596. void ClickableHex::clickRight(tribool down, bool previousState)
  597. {
  598. const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
  599. if(hovered && strictHovered && myst!=nullptr)
  600. {
  601. if(!myst->alive()) return;
  602. if(down)
  603. {
  604. GH.pushIntT<CStackWindow>(myst, true);
  605. }
  606. }
  607. }
  608. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  609. : embedded(Embedded),
  610. owner(owner)
  611. {
  612. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  613. if(embedded)
  614. {
  615. pos.w = QUEUE_SIZE * 41;
  616. pos.h = 49;
  617. pos.x += parent->pos.w/2 - pos.w/2;
  618. pos.y += 10;
  619. icons = std::make_shared<CAnimation>("CPRSMALL");
  620. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  621. }
  622. else
  623. {
  624. pos.w = 800;
  625. pos.h = 85;
  626. pos.x += 0;
  627. pos.y -= pos.h;
  628. background = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, 0, pos.w, pos.h));
  629. icons = std::make_shared<CAnimation>("TWCRPORT");
  630. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  631. //TODO: where use big icons?
  632. //stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESBIG");
  633. }
  634. stateIcons->preload();
  635. stackBoxes.resize(QUEUE_SIZE);
  636. for (int i = 0; i < stackBoxes.size(); i++)
  637. {
  638. stackBoxes[i] = std::make_shared<StackBox>(this);
  639. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  640. }
  641. }
  642. void StackQueue::update()
  643. {
  644. std::vector<battle::Units> queueData;
  645. owner.getCurrentPlayerInterface()->cb->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  646. size_t boxIndex = 0;
  647. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  648. {
  649. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  650. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  651. }
  652. for(; boxIndex < stackBoxes.size(); boxIndex++)
  653. stackBoxes[boxIndex]->setUnit(nullptr);
  654. }
  655. int32_t StackQueue::getSiegeShooterIconID()
  656. {
  657. return owner.siegeController->getSiegedTown()->town->faction->index;
  658. }
  659. StackQueue::StackBox::StackBox(StackQueue * owner):
  660. owner(owner)
  661. {
  662. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  663. background = std::make_shared<CPicture>(owner->embedded ? "StackQueueSmall" : "StackQueueLarge");
  664. pos.w = background->pos.w;
  665. pos.h = background->pos.h;
  666. if(owner->embedded)
  667. {
  668. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  669. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  670. }
  671. else
  672. {
  673. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  674. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  675. int icon_x = pos.w - 17;
  676. int icon_y = pos.h - 18;
  677. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  678. stateIcon->visible = false;
  679. }
  680. }
  681. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  682. {
  683. if(unit)
  684. {
  685. background->colorize(unit->unitOwner());
  686. icon->visible = true;
  687. // temporary code for mod compatibility:
  688. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  689. // second, try to switch to icon that should be provided by mod
  690. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  691. // for 1.2 release & later next line should be moved into 'else' block
  692. icon->setFrame(unit->creatureIconIndex(), 0);
  693. if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS)
  694. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  695. amount->setText(makeNumberShort(unit->getCount()));
  696. if(stateIcon)
  697. {
  698. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  699. {
  700. stateIcon->setFrame(0, 0);
  701. stateIcon->visible = true;
  702. }
  703. else if(unit->waited((int)turn))
  704. {
  705. stateIcon->setFrame(1, 0);
  706. stateIcon->visible = true;
  707. }
  708. else
  709. {
  710. stateIcon->visible = false;
  711. }
  712. }
  713. }
  714. else
  715. {
  716. background->colorize(PlayerColor::NEUTRAL);
  717. icon->visible = false;
  718. icon->setFrame(0);
  719. amount->setText("");
  720. if(stateIcon)
  721. stateIcon->visible = false;
  722. }
  723. }