BattleInterfaceClasses.cpp 28 KB

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