BattleInterfaceClasses.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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.makingTurn())
  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.makingTurn() || 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. BattleOptionsWindow::BattleOptionsWindow(BattleInterface & owner):
  348. CWindowObject(PLAYER_COLORED, "comopbck.bmp")
  349. {
  350. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  351. auto viewGrid = std::make_shared<CToggleButton>(Point(25, 56), "sysopchk.def", CGI->generaltexth->zelp[427], [&](bool on){owner.setPrintCellBorders(on);} );
  352. viewGrid->setSelected(settings["battle"]["cellBorders"].Bool());
  353. toggles.push_back(viewGrid);
  354. auto movementShadow = std::make_shared<CToggleButton>(Point(25, 89), "sysopchk.def", CGI->generaltexth->zelp[428], [&](bool on){owner.setPrintStackRange(on);});
  355. movementShadow->setSelected(settings["battle"]["stackRange"].Bool());
  356. toggles.push_back(movementShadow);
  357. auto mouseShadow = std::make_shared<CToggleButton>(Point(25, 122), "sysopchk.def", CGI->generaltexth->zelp[429], [&](bool on){owner.setPrintMouseShadow(on);});
  358. mouseShadow->setSelected(settings["battle"]["mouseShadow"].Bool());
  359. toggles.push_back(mouseShadow);
  360. animSpeeds = std::make_shared<CToggleGroup>([&](int value){ owner.setAnimSpeed(value);});
  361. std::shared_ptr<CToggleButton> toggle;
  362. toggle = std::make_shared<CToggleButton>(Point( 28, 225), "sysopb9.def", CGI->generaltexth->zelp[422]);
  363. animSpeeds->addToggle(1, toggle);
  364. toggle = std::make_shared<CToggleButton>(Point( 92, 225), "sysob10.def", CGI->generaltexth->zelp[423]);
  365. animSpeeds->addToggle(2, toggle);
  366. toggle = std::make_shared<CToggleButton>(Point(156, 225), "sysob11.def", CGI->generaltexth->zelp[424]);
  367. animSpeeds->addToggle(3, toggle);
  368. animSpeeds->setSelected(owner.getAnimSpeed());
  369. setToDefault = std::make_shared<CButton>(Point(246, 359), "codefaul.def", CGI->generaltexth->zelp[393], [&](){ bDefaultf(); });
  370. setToDefault->setImageOrder(1, 0, 2, 3);
  371. exit = std::make_shared<CButton>(Point(357, 359), "soretrn.def", CGI->generaltexth->zelp[392], [&](){ bExitf();}, SDLK_RETURN);
  372. exit->setImageOrder(1, 0, 2, 3);
  373. //creating labels
  374. labels.push_back(std::make_shared<CLabel>(242, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[392]));//window title
  375. labels.push_back(std::make_shared<CLabel>(122, 214, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[393]));//animation speed
  376. labels.push_back(std::make_shared<CLabel>(122, 293, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[394]));//music volume
  377. labels.push_back(std::make_shared<CLabel>(122, 359, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[395]));//effects' volume
  378. labels.push_back(std::make_shared<CLabel>(353, 66, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[396]));//auto - combat options
  379. labels.push_back(std::make_shared<CLabel>(353, 265, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[397]));//creature info
  380. //auto - combat options
  381. labels.push_back(std::make_shared<CLabel>(283, 86, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[398]));//creatures
  382. labels.push_back(std::make_shared<CLabel>(283, 116, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[399]));//spells
  383. labels.push_back(std::make_shared<CLabel>(283, 146, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[400]));//catapult
  384. labels.push_back(std::make_shared<CLabel>(283, 176, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[151]));//ballista
  385. labels.push_back(std::make_shared<CLabel>(283, 206, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[401]));//first aid tent
  386. //creature info
  387. labels.push_back(std::make_shared<CLabel>(283, 285, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[402]));//all stats
  388. labels.push_back(std::make_shared<CLabel>(283, 315, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[403]));//spells only
  389. //general options
  390. labels.push_back(std::make_shared<CLabel>(61, 57, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[404]));
  391. labels.push_back(std::make_shared<CLabel>(61, 90, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[405]));
  392. labels.push_back(std::make_shared<CLabel>(61, 123, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[406]));
  393. labels.push_back(std::make_shared<CLabel>(61, 156, FONT_MEDIUM, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[407]));
  394. }
  395. void BattleOptionsWindow::bDefaultf()
  396. {
  397. //TODO: implement
  398. }
  399. void BattleOptionsWindow::bExitf()
  400. {
  401. close();
  402. }
  403. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner)
  404. : owner(_owner)
  405. {
  406. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  407. pos = CSDL_Ext::genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  408. background = std::make_shared<CPicture>("CPRESULT");
  409. background->colorize(owner.playerID);
  410. exit = std::make_shared<CButton>(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, SDLK_RETURN);
  411. exit->setBorderColor(Colors::METALLIC_GOLD);
  412. if(br.winner == 0) //attacker won
  413. {
  414. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  415. }
  416. else
  417. {
  418. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  419. }
  420. if(br.winner == 1)
  421. {
  422. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  423. }
  424. else
  425. {
  426. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  427. }
  428. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  429. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  430. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  431. std::string sideNames[2] = {"N/A", "N/A"};
  432. for(int i = 0; i < 2; i++)
  433. {
  434. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  435. const int xs[] = {21, 392};
  436. if(heroInfo.portrait >= 0) //attacking hero
  437. {
  438. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38));
  439. sideNames[i] = heroInfo.name;
  440. }
  441. else
  442. {
  443. auto stacks = owner.cb->battleGetAllStacks();
  444. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  445. {
  446. return stack->side != i || !stack->base;
  447. });
  448. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  449. {
  450. return stack->type->AIValue;
  451. });
  452. if(best != stacks.end()) //should be always but to be safe...
  453. {
  454. icons.push_back(std::make_shared<CAnimImage>("TWCRPORT", (*best)->type->getIconIndex(), 0, xs[i], 38));
  455. sideNames[i] = (*best)->type->getNamePluralTranslated();
  456. }
  457. }
  458. }
  459. //printing attacker and defender's names
  460. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  461. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  462. //printing casualties
  463. for(int step = 0; step < 2; ++step)
  464. {
  465. if(br.casualties[step].size()==0)
  466. {
  467. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  468. }
  469. else
  470. {
  471. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  472. int yPos = 344 + step * 97;
  473. for(auto & elem : br.casualties[step])
  474. {
  475. auto creature = CGI->creatures()->getByIndex(elem.first);
  476. if (creature->getId() == CreatureID::ARROW_TOWERS )
  477. continue; // do not show destroyed towers in battle results
  478. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, xPos, yPos));
  479. std::ostringstream amount;
  480. amount<<elem.second;
  481. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  482. xPos += 42;
  483. }
  484. }
  485. }
  486. //printing result description
  487. bool weAreAttacker = !(owner.cb->battleGetMySide());
  488. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  489. {
  490. int text = 304;
  491. switch(br.result)
  492. {
  493. case BattleResult::NORMAL:
  494. break;
  495. case BattleResult::ESCAPE:
  496. text = 303;
  497. break;
  498. case BattleResult::SURRENDER:
  499. text = 302;
  500. break;
  501. default:
  502. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  503. break;
  504. }
  505. CCS->musich->playMusic("Music/Win Battle", false, true);
  506. CCS->videoh->open("WIN3.BIK");
  507. std::string str = CGI->generaltexth->allTexts[text];
  508. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  509. if (ourHero)
  510. {
  511. str += CGI->generaltexth->allTexts[305];
  512. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  513. boost::algorithm::replace_first(str, "%d", boost::lexical_cast<std::string>(br.exp[weAreAttacker ? 0 : 1]));
  514. }
  515. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  516. }
  517. else // we lose
  518. {
  519. int text = 311;
  520. std::string musicName = "Music/LoseCombat";
  521. std::string videoName = "LBSTART.BIK";
  522. switch(br.result)
  523. {
  524. case BattleResult::NORMAL:
  525. break;
  526. case BattleResult::ESCAPE:
  527. musicName = "Music/Retreat Battle";
  528. videoName = "RTSTART.BIK";
  529. text = 310;
  530. break;
  531. case BattleResult::SURRENDER:
  532. musicName = "Music/Surrender Battle";
  533. videoName = "SURRENDER.BIK";
  534. text = 309;
  535. break;
  536. default:
  537. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  538. break;
  539. }
  540. CCS->musich->playMusic(musicName, false, true);
  541. CCS->videoh->open(videoName);
  542. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  543. }
  544. }
  545. void BattleResultWindow::activate()
  546. {
  547. owner.showingDialog->set(true);
  548. CIntObject::activate();
  549. }
  550. void BattleResultWindow::show(SDL_Surface * to)
  551. {
  552. CIntObject::show(to);
  553. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  554. }
  555. void BattleResultWindow::bExitf()
  556. {
  557. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  558. close();
  559. if(dynamic_cast<BattleWindow*>(GH.topInt().get()))
  560. GH.popInts(1); //pop battle interface if present
  561. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  562. //so we can be sure that there is no dialogs left on GUI stack.
  563. intTmp.showingDialog->setn(false);
  564. CCS->videoh->close();
  565. }
  566. void ClickableHex::hover(bool on)
  567. {
  568. hovered = on;
  569. //Hoverable::hover(on);
  570. }
  571. ClickableHex::ClickableHex()
  572. : myNumber(-1)
  573. , strictHovered(false)
  574. , myInterface(nullptr)
  575. {
  576. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  577. }
  578. void ClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  579. {
  580. strictHovered = myInterface->fieldController->isPixelInHex(Point(sEvent.x-pos.x, sEvent.y-pos.y));
  581. }
  582. void ClickableHex::clickLeft(tribool down, bool previousState)
  583. {
  584. if(!down && hovered && strictHovered) //we've been really clicked!
  585. {
  586. myInterface->actionsController->onHexClicked(myNumber);
  587. }
  588. }
  589. void ClickableHex::clickRight(tribool down, bool previousState)
  590. {
  591. const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
  592. if(hovered && strictHovered && myst!=nullptr)
  593. {
  594. if(!myst->alive()) return;
  595. if(down)
  596. {
  597. GH.pushIntT<CStackWindow>(myst, true);
  598. }
  599. }
  600. }
  601. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  602. : embedded(Embedded),
  603. owner(owner)
  604. {
  605. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  606. if(embedded)
  607. {
  608. pos.w = QUEUE_SIZE * 41;
  609. pos.h = 49;
  610. pos.x += parent->pos.w/2 - pos.w/2;
  611. pos.y += 10;
  612. icons = std::make_shared<CAnimation>("CPRSMALL");
  613. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  614. }
  615. else
  616. {
  617. pos.w = 800;
  618. pos.h = 85;
  619. pos.x += 0;
  620. pos.y -= pos.h;
  621. background = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, 0, pos.w, pos.h));
  622. icons = std::make_shared<CAnimation>("TWCRPORT");
  623. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  624. //TODO: where use big icons?
  625. //stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESBIG");
  626. }
  627. stateIcons->preload();
  628. stackBoxes.resize(QUEUE_SIZE);
  629. for (int i = 0; i < stackBoxes.size(); i++)
  630. {
  631. stackBoxes[i] = std::make_shared<StackBox>(this);
  632. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  633. }
  634. }
  635. void StackQueue::show(SDL_Surface * to)
  636. {
  637. if (embedded)
  638. showAll(to);
  639. CIntObject::show(to);
  640. }
  641. void StackQueue::update()
  642. {
  643. std::vector<battle::Units> queueData;
  644. owner.getCurrentPlayerInterface()->cb->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  645. size_t boxIndex = 0;
  646. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  647. {
  648. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  649. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  650. }
  651. for(; boxIndex < stackBoxes.size(); boxIndex++)
  652. stackBoxes[boxIndex]->setUnit(nullptr);
  653. }
  654. int32_t StackQueue::getSiegeShooterIconID()
  655. {
  656. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  657. }
  658. StackQueue::StackBox::StackBox(StackQueue * owner):
  659. owner(owner)
  660. {
  661. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  662. background = std::make_shared<CPicture>(owner->embedded ? "StackQueueSmall" : "StackQueueLarge");
  663. pos.w = background->pos.w;
  664. pos.h = background->pos.h;
  665. if(owner->embedded)
  666. {
  667. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  668. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  669. }
  670. else
  671. {
  672. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  673. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  674. int icon_x = pos.w - 17;
  675. int icon_y = pos.h - 18;
  676. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  677. stateIcon->visible = false;
  678. }
  679. }
  680. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  681. {
  682. if(unit)
  683. {
  684. background->colorize(unit->unitOwner());
  685. icon->visible = true;
  686. // temporary code for mod compatibility:
  687. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  688. // second, try to switch to icon that should be provided by mod
  689. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  690. // for 1.2 release & later next line should be moved into 'else' block
  691. icon->setFrame(unit->creatureIconIndex(), 0);
  692. if (unit->unitType()->idNumber == CreatureID::ARROW_TOWERS)
  693. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  694. amount->setText(CSDL_Ext::makeNumberShort(unit->getCount(), 4));
  695. if(stateIcon)
  696. {
  697. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  698. {
  699. stateIcon->setFrame(0, 0);
  700. stateIcon->visible = true;
  701. }
  702. else if(unit->waited((int)turn))
  703. {
  704. stateIcon->setFrame(1, 0);
  705. stateIcon->visible = true;
  706. }
  707. else
  708. {
  709. stateIcon->visible = false;
  710. }
  711. }
  712. }
  713. else
  714. {
  715. background->colorize(PlayerColor::NEUTRAL);
  716. icon->visible = false;
  717. icon->setFrame(0);
  718. amount->setText("");
  719. if(stateIcon)
  720. stateIcon->visible = false;
  721. }
  722. }