BattleInterfaceClasses.cpp 29 KB

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