CBattleInterfaceClasses.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * CBattleInterfaceClasses.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 "CBattleInterfaceClasses.h"
  12. #include "CBattleInterface.h"
  13. #include "../CBitmapHandler.h"
  14. #include "../CDefHandler.h"
  15. #include "../CGameInfo.h"
  16. #include "../CMessage.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CVideoHandler.h"
  20. #include "../Graphics.h"
  21. #include "../gui/CCursorHandler.h"
  22. #include "../gui/CGuiHandler.h"
  23. #include "../gui/SDL_Extensions.h"
  24. #include "../widgets/Buttons.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../windows/CCreatureWindow.h"
  27. #include "../windows/CSpellWindow.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CStack.h"
  30. #include "../../lib/CConfigHandler.h"
  31. #include "../../lib/CCreatureHandler.h"
  32. #include "../../lib/CGameState.h"
  33. #include "../../lib/CGeneralTextHandler.h"
  34. #include "../../lib/CTownHandler.h"
  35. #include "../../lib/NetPacks.h"
  36. #include "../../lib/StartInfo.h"
  37. #include "../../lib/CondSh.h"
  38. #include "../../lib/mapObjects/CGTownInstance.h"
  39. void CBattleConsole::showAll(SDL_Surface * to)
  40. {
  41. Point textPos(pos.x + pos.w / 2, pos.y + 17);
  42. if(ingcAlter.size())
  43. {
  44. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(ingcAlter, pos.w, FONT_SMALL), Colors::WHITE, textPos);
  45. }
  46. else if(alterTxt.size())
  47. {
  48. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(alterTxt, pos.w, FONT_SMALL), Colors::WHITE, textPos);
  49. }
  50. else if(texts.size())
  51. {
  52. if(texts.size() == 1)
  53. {
  54. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(texts[0], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  55. }
  56. else
  57. {
  58. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(texts[lastShown - 1], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  59. textPos.y += 16;
  60. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(texts[lastShown], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  61. }
  62. }
  63. }
  64. bool CBattleConsole::addText(const std::string & text)
  65. {
  66. logGlobal->traceStream() << "CBattleConsole message: " << text;
  67. if(text.size() > 70)
  68. return false; //text too long!
  69. int firstInToken = 0;
  70. for(size_t i = 0; i < text.size(); ++i) //tokenize
  71. {
  72. if(text[i] == 10)
  73. {
  74. texts.push_back(text.substr(firstInToken, i - firstInToken));
  75. firstInToken = i + 1;
  76. }
  77. }
  78. texts.push_back(text.substr(firstInToken, text.size()));
  79. lastShown = texts.size() - 1;
  80. return true;
  81. }
  82. void CBattleConsole::alterText(const std::string & text)
  83. {
  84. //char buf[500];
  85. //sprintf(buf, text.c_str());
  86. //alterTxt = buf;
  87. alterTxt = text;
  88. }
  89. void CBattleConsole::eraseText(ui32 pos)
  90. {
  91. if(pos < texts.size())
  92. {
  93. texts.erase(texts.begin() + pos);
  94. if(lastShown == texts.size())
  95. --lastShown;
  96. }
  97. }
  98. void CBattleConsole::changeTextAt(const std::string & text, ui32 pos)
  99. {
  100. if(pos >= texts.size()) //no such pos
  101. return;
  102. texts[pos] = text;
  103. }
  104. void CBattleConsole::scrollUp(ui32 by)
  105. {
  106. if(lastShown > static_cast<int>(by))
  107. lastShown -= by;
  108. }
  109. void CBattleConsole::scrollDown(ui32 by)
  110. {
  111. if(lastShown + by < texts.size())
  112. lastShown += by;
  113. }
  114. CBattleConsole::CBattleConsole()
  115. : lastShown(-1), alterTxt(""), whoSetAlter(0)
  116. {}
  117. void CBattleHero::show(SDL_Surface * to)
  118. {
  119. //animation of flag
  120. SDL_Rect temp_rect;
  121. if(flip)
  122. {
  123. temp_rect = genRect(
  124. flag->ourImages[flagAnim].bitmap->h,
  125. flag->ourImages[flagAnim].bitmap->w,
  126. pos.x + 61,
  127. pos.y + 39);
  128. }
  129. else
  130. {
  131. temp_rect = genRect(
  132. flag->ourImages[flagAnim].bitmap->h,
  133. flag->ourImages[flagAnim].bitmap->w,
  134. pos.x + 72,
  135. pos.y + 39);
  136. }
  137. CSDL_Ext::blit8bppAlphaTo24bpp(
  138. flag->ourImages[flagAnim].bitmap,
  139. nullptr,
  140. screen,
  141. &temp_rect);
  142. //animation of hero
  143. SDL_Rect rect = pos;
  144. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[currentFrame].bitmap, nullptr, to, &rect);
  145. if(++animCount == 4)
  146. {
  147. animCount = 0;
  148. if(++flagAnim >= flag->ourImages.size())
  149. flagAnim = 0;
  150. if(++currentFrame >= lastFrame)
  151. switchToNextPhase();
  152. }
  153. }
  154. void CBattleHero::setPhase(int newPhase)
  155. {
  156. nextPhase = newPhase;
  157. switchToNextPhase(); //immediately switch to next phase and then restore idling phase
  158. nextPhase = 0;
  159. }
  160. void CBattleHero::hover(bool on)
  161. {
  162. //TODO: Make lines below work properly
  163. if(on)
  164. CCS->curh->changeGraphic(ECursor::COMBAT, 5);
  165. else
  166. CCS->curh->changeGraphic(ECursor::COMBAT, 0);
  167. }
  168. void CBattleHero::clickLeft(tribool down, bool previousState)
  169. {
  170. if(myOwner->spellDestSelectMode) //we are casting a spell
  171. return;
  172. if(myHero != nullptr && !down && myOwner->myTurn && myOwner->getCurrentPlayerInterface()->cb->battleCanCastSpell(myHero, ECastingMode::HERO_CASTING) == ESpellCastProblem::OK) //check conditions
  173. {
  174. for(int it = 0; it < GameConstants::BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  175. {
  176. if(myOwner->bfield[it]->hovered && myOwner->bfield[it]->strictHovered)
  177. return;
  178. }
  179. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  180. GH.pushInt(new CSpellWindow(myHero, myOwner->getCurrentPlayerInterface()));
  181. }
  182. }
  183. void CBattleHero::clickRight(tribool down, bool previousState)
  184. {
  185. Point windowPosition;
  186. windowPosition.x = (!flip) ? myOwner->pos.topLeft().x + 1 : myOwner->pos.topRight().x - 79;
  187. windowPosition.y = myOwner->pos.y + 135;
  188. InfoAboutHero targetHero;
  189. if(down && (myOwner->myTurn || settings["session"]["spectate"].Bool()))
  190. {
  191. auto h = flip ? myOwner->defendingHeroInstance : myOwner->attackingHeroInstance;
  192. targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
  193. GH.pushInt(new CHeroInfoWindow(targetHero, &windowPosition));
  194. }
  195. }
  196. void CBattleHero::switchToNextPhase()
  197. {
  198. if(phase != nextPhase)
  199. {
  200. phase = nextPhase;
  201. //find first and last frames of our animation
  202. for(firstFrame = 0;
  203. firstFrame < dh->ourImages.size() && dh->ourImages[firstFrame].groupNumber != phase;
  204. firstFrame++)
  205. ;
  206. for(lastFrame = firstFrame;
  207. lastFrame < dh->ourImages.size() && dh->ourImages[lastFrame].groupNumber == phase;
  208. lastFrame++)
  209. ;
  210. }
  211. currentFrame = firstFrame;
  212. }
  213. CBattleHero::CBattleHero(const std::string & defName, bool flipG, PlayerColor player, const CGHeroInstance * hero, const CBattleInterface * owner)
  214. : flip(flipG), myHero(hero), myOwner(owner), phase(1), nextPhase(0), flagAnim(0), animCount(0)
  215. {
  216. dh = CDefHandler::giveDef(defName);
  217. for(auto & elem : dh->ourImages) //transforming images
  218. {
  219. if(flip)
  220. {
  221. SDL_Surface * hlp = CSDL_Ext::verticalFlip(elem.bitmap);
  222. SDL_FreeSurface(elem.bitmap);
  223. elem.bitmap = hlp;
  224. }
  225. CSDL_Ext::alphaTransform(elem.bitmap);
  226. }
  227. if(flip)
  228. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  229. else
  230. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  231. //coloring flag and adding transparency
  232. for(auto & elem : flag->ourImages)
  233. {
  234. CSDL_Ext::alphaTransform(elem.bitmap);
  235. graphics->blueToPlayersAdv(elem.bitmap, player);
  236. }
  237. addUsedEvents(LCLICK | RCLICK | HOVER);
  238. switchToNextPhase();
  239. }
  240. CBattleHero::~CBattleHero()
  241. {
  242. delete dh;
  243. delete flag;
  244. }
  245. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface * owner)
  246. {
  247. OBJ_CONSTRUCTION_CAPTURING_ALL;
  248. pos = position;
  249. background = new CPicture("comopbck.bmp");
  250. background->colorize(owner->getCurrentPlayerInterface()->playerID);
  251. viewGrid = new CToggleButton(Point(25, 56), "sysopchk.def", CGI->generaltexth->zelp[427], [=](bool on){owner->setPrintCellBorders(on);});
  252. viewGrid->setSelected(settings["battle"]["cellBorders"].Bool());
  253. movementShadow = new CToggleButton(Point(25, 89), "sysopchk.def", CGI->generaltexth->zelp[428], [=](bool on){owner->setPrintStackRange(on);});
  254. movementShadow->setSelected(settings["battle"]["stackRange"].Bool());
  255. mouseShadow = new CToggleButton(Point(25, 122), "sysopchk.def", CGI->generaltexth->zelp[429], [=](bool on){owner->setPrintMouseShadow(on);});
  256. mouseShadow->setSelected(settings["battle"]["mouseShadow"].Bool());
  257. animSpeeds = new CToggleGroup([=](int value){ owner->setAnimSpeed(value);});
  258. animSpeeds->addToggle(40, new CToggleButton(Point(28, 225), "sysopb9.def", CGI->generaltexth->zelp[422]));
  259. animSpeeds->addToggle(63, new CToggleButton(Point(92, 225), "sysob10.def", CGI->generaltexth->zelp[423]));
  260. animSpeeds->addToggle(100, new CToggleButton(Point(156, 225), "sysob11.def", CGI->generaltexth->zelp[424]));
  261. animSpeeds->setSelected(owner->getAnimSpeed());
  262. setToDefault = new CButton(Point(246, 359), "codefaul.def", CGI->generaltexth->zelp[393], [&](){ bDefaultf(); });
  263. setToDefault->setImageOrder(1, 0, 2, 3);
  264. exit = new CButton(Point(357, 359), "soretrn.def", CGI->generaltexth->zelp[392], [&](){ bExitf();}, SDLK_RETURN);
  265. exit->setImageOrder(1, 0, 2, 3);
  266. //creating labels
  267. labels.push_back(new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[392])); //window title
  268. labels.push_back(new CLabel(122, 214, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[393])); //animation speed
  269. labels.push_back(new CLabel(122, 293, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[394])); //music volume
  270. labels.push_back(new CLabel(122, 359, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[395])); //effects' volume
  271. labels.push_back(new CLabel(353, 66, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[396])); //auto - combat options
  272. labels.push_back(new CLabel(353, 265, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[397])); //creature info
  273. //auto - combat options
  274. labels.push_back(new CLabel(283, 86, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[398])); //creatures
  275. labels.push_back(new CLabel(283, 116, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[399])); //spells
  276. labels.push_back(new CLabel(283, 146, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[400])); //catapult
  277. labels.push_back(new CLabel(283, 176, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[151])); //ballista
  278. labels.push_back(new CLabel(283, 206, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[401])); //first aid tent
  279. //creature info
  280. labels.push_back(new CLabel(283, 285, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[402])); //all stats
  281. labels.push_back(new CLabel(283, 315, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[403])); //spells only
  282. //general options
  283. labels.push_back(new CLabel(61, 57, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[404]));
  284. labels.push_back(new CLabel(61, 90, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[405]));
  285. labels.push_back(new CLabel(61, 123, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[406]));
  286. labels.push_back(new CLabel(61, 156, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[407]));
  287. }
  288. void CBattleOptionsWindow::bDefaultf()
  289. {
  290. //TODO: implement
  291. }
  292. void CBattleOptionsWindow::bExitf()
  293. {
  294. GH.popIntTotally(this);
  295. }
  296. CBattleResultWindow::CBattleResultWindow(const BattleResult & br, const SDL_Rect & pos, CPlayerInterface & _owner)
  297. : owner(_owner)
  298. {
  299. OBJ_CONSTRUCTION_CAPTURING_ALL;
  300. this->pos = pos;
  301. CPicture * bg = new CPicture("CPRESULT");
  302. bg->colorize(owner.playerID);
  303. exit = new CButton(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, SDLK_RETURN);
  304. exit->borderColor = Colors::METALLIC_GOLD;
  305. if(br.winner == 0) //attacker won
  306. {
  307. new CLabel(59, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]);
  308. new CLabel(408, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]);
  309. }
  310. else //if(br.winner==1)
  311. {
  312. new CLabel(59, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]);
  313. new CLabel(412, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]);
  314. }
  315. new CLabel(232, 302, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]);
  316. new CLabel(232, 332, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]);
  317. new CLabel(232, 428, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]);
  318. std::string sideNames[2] = {"N/A", "N/A"};
  319. for(int i = 0; i < 2; i++)
  320. {
  321. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  322. const int xs[] = {21, 392};
  323. if(heroInfo.portrait >= 0) //attacking hero
  324. {
  325. new CAnimImage("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38);
  326. sideNames[i] = heroInfo.name;
  327. }
  328. else
  329. {
  330. auto stacks = owner.cb->battleGetAllStacks();
  331. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  332. { return stack->side != i || !stack->base; });
  333. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack){ return stack->type->AIValue; });
  334. if(best != stacks.end()) //should be always but to be safe...
  335. {
  336. new CAnimImage("TWCRPORT", (*best)->type->idNumber + 2, 0, xs[i], 38);
  337. sideNames[i] = CGI->creh->creatures[(*best)->type->idNumber]->namePl;
  338. }
  339. }
  340. }
  341. //printing attacker and defender's names
  342. new CLabel(89, 37, FONT_SMALL, TOPLEFT, Colors::WHITE, sideNames[0]);
  343. new CLabel(381, 53, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, sideNames[1]);
  344. //printing casualties
  345. for(int step = 0; step < 2; ++step)
  346. {
  347. if(br.casualties[step].size() == 0)
  348. {
  349. new CLabel(235, 360 + 97 * step, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]);
  350. }
  351. else
  352. {
  353. int xPos = 235 - (br.casualties[step].size() * 32 + (br.casualties[step].size() - 1) * 10) / 2; //increment by 42 with each picture
  354. int yPos = 344 + step * 97;
  355. for(auto & elem : br.casualties[step])
  356. {
  357. new CAnimImage("CPRSMALL", CGI->creh->creatures[elem.first]->iconIndex, 0, xPos, yPos);
  358. std::ostringstream amount;
  359. amount << elem.second;
  360. new CLabel(xPos + 16, yPos + 42, FONT_SMALL, CENTER, Colors::WHITE, amount.str());
  361. xPos += 42;
  362. }
  363. }
  364. }
  365. //printing result description
  366. bool weAreAttacker = !(owner.cb->battleGetMySide());
  367. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  368. {
  369. int text = -1;
  370. switch(br.result)
  371. {
  372. case BattleResult::NORMAL:
  373. text = 304;
  374. break;
  375. case BattleResult::ESCAPE:
  376. text = 303;
  377. break;
  378. case BattleResult::SURRENDER:
  379. text = 302;
  380. break;
  381. }
  382. CCS->musich->playMusic("Music/Win Battle", false);
  383. CCS->videoh->open("WIN3.BIK");
  384. std::string str = CGI->generaltexth->allTexts[text];
  385. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  386. if(ourHero)
  387. {
  388. str += CGI->generaltexth->allTexts[305];
  389. boost::algorithm::replace_first(str, "%s", ourHero->name);
  390. boost::algorithm::replace_first(str, "%d", boost::lexical_cast<std::string>(br.exp[weAreAttacker ? 0 : 1]));
  391. }
  392. new CTextBox(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  393. }
  394. else // we lose
  395. {
  396. switch(br.result)
  397. {
  398. case BattleResult::NORMAL:
  399. {
  400. CCS->musich->playMusic("Music/LoseCombat", false);
  401. CCS->videoh->open("LBSTART.BIK");
  402. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[311]);
  403. break;
  404. }
  405. case BattleResult::ESCAPE: //flee
  406. {
  407. CCS->musich->playMusic("Music/Retreat Battle", false);
  408. CCS->videoh->open("RTSTART.BIK");
  409. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[310]);
  410. break;
  411. }
  412. case BattleResult::SURRENDER:
  413. {
  414. CCS->musich->playMusic("Music/Surrender Battle", false);
  415. CCS->videoh->open("SURRENDER.BIK");
  416. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[309]);
  417. break;
  418. }
  419. }
  420. }
  421. }
  422. CBattleResultWindow::~CBattleResultWindow()
  423. {
  424. }
  425. void CBattleResultWindow::activate()
  426. {
  427. owner.showingDialog->set(true);
  428. CIntObject::activate();
  429. }
  430. void CBattleResultWindow::show(SDL_Surface * to)
  431. {
  432. CIntObject::show(to);
  433. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  434. }
  435. void CBattleResultWindow::bExitf()
  436. {
  437. CPlayerInterface & intTmp = owner; //copy reference because "this" will be destructed soon
  438. GH.popIntTotally(this);
  439. if(dynamic_cast<CBattleInterface *>(GH.topInt()))
  440. GH.popInts(1); //pop battle interface if present
  441. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  442. //so we can be sure that there is no dialogs left on GUI stack.
  443. intTmp.showingDialog->setn(false);
  444. CCS->videoh->close();
  445. }
  446. Point CClickableHex::getXYUnitAnim(BattleHex hexNum, const CStack * stack, CBattleInterface * cbi)
  447. {
  448. assert(cbi);
  449. Point ret(-500, -500); //returned value
  450. if(stack && stack->position < 0) //creatures in turrets
  451. {
  452. switch(stack->position)
  453. {
  454. case -2: //keep
  455. ret = cbi->siegeH->town->town->clientInfo.siegePositions[18];
  456. break;
  457. case -3: //lower turret
  458. ret = cbi->siegeH->town->town->clientInfo.siegePositions[19];
  459. break;
  460. case -4: //upper turret
  461. ret = cbi->siegeH->town->town->clientInfo.siegePositions[20];
  462. break;
  463. }
  464. }
  465. else
  466. {
  467. static const Point basePos(-190, -139); // position of creature in topleft corner
  468. static const int imageShiftX = 30; // X offset to base pos for facing right stacks, negative for facing left
  469. ret.x = basePos.x + 22 * ((hexNum.getY() + 1) % 2) + 44 * hexNum.getX();
  470. ret.y = basePos.y + 42 * hexNum.getY();
  471. if(stack)
  472. {
  473. if(cbi->creDir[stack->ID])
  474. ret.x += imageShiftX;
  475. else
  476. ret.x -= imageShiftX;
  477. //shifting position for double - hex creatures
  478. if(stack->doubleWide())
  479. {
  480. if(stack->side == BattleSide::ATTACKER)
  481. {
  482. if(cbi->creDir[stack->ID])
  483. ret.x -= 44;
  484. }
  485. else
  486. {
  487. if(!cbi->creDir[stack->ID])
  488. ret.x += 44;
  489. }
  490. }
  491. }
  492. }
  493. //returning
  494. return ret + CPlayerInterface::battleInt->pos;
  495. }
  496. void CClickableHex::hover(bool on)
  497. {
  498. hovered = on;
  499. //Hoverable::hover(on);
  500. if(!on && setAlterText)
  501. {
  502. myInterface->console->alterTxt = std::string();
  503. setAlterText = false;
  504. }
  505. }
  506. CClickableHex::CClickableHex()
  507. : setAlterText(false), myNumber(-1), accessible(true), hovered(false), strictHovered(false), myInterface(nullptr)
  508. {
  509. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  510. }
  511. void CClickableHex::mouseMoved(const SDL_MouseMotionEvent & sEvent)
  512. {
  513. if(myInterface->cellShade)
  514. {
  515. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x - pos.x, sEvent.y - pos.y) == 0) //hovered pixel is outside hex
  516. {
  517. strictHovered = false;
  518. }
  519. else //hovered pixel is inside hex
  520. {
  521. strictHovered = true;
  522. }
  523. }
  524. if(hovered && strictHovered) //print attacked creature to console
  525. {
  526. const CStack * attackedStack = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber);
  527. if(myInterface->console->alterTxt.size() == 0 && attackedStack != nullptr &&
  528. attackedStack->owner != myInterface->getCurrentPlayerInterface()->playerID &&
  529. attackedStack->alive())
  530. {
  531. MetaString text;
  532. text.addTxt(MetaString::GENERAL_TXT, 220);
  533. attackedStack->addNameReplacement(text);
  534. myInterface->console->alterTxt = text.toString();
  535. setAlterText = true;
  536. }
  537. }
  538. else if(setAlterText)
  539. {
  540. myInterface->console->alterTxt = std::string();
  541. setAlterText = false;
  542. }
  543. }
  544. void CClickableHex::clickLeft(tribool down, bool previousState)
  545. {
  546. if(!down && hovered && strictHovered) //we've been really clicked!
  547. {
  548. myInterface->hexLclicked(myNumber);
  549. }
  550. }
  551. void CClickableHex::clickRight(tribool down, bool previousState)
  552. {
  553. const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
  554. if(hovered && strictHovered && myst != nullptr)
  555. {
  556. if(!myst->alive())
  557. return;
  558. if(down)
  559. {
  560. GH.pushInt(new CStackWindow(myst, true));
  561. }
  562. }
  563. }
  564. CHeroInfoWindow::CHeroInfoWindow(const InfoAboutHero & hero, Point * position)
  565. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, "CHRPOP")
  566. {
  567. OBJ_CONSTRUCTION_CAPTURING_ALL;
  568. if(position != nullptr)
  569. moveTo(*position);
  570. background->colorize(hero.owner); //maybe add this functionality to base class?
  571. int attack = hero.details->primskills[0];
  572. int defense = hero.details->primskills[1];
  573. int power = hero.details->primskills[2];
  574. int knowledge = hero.details->primskills[3];
  575. int morale = hero.details->morale;
  576. int luck = hero.details->luck;
  577. int currentSpellPoints = hero.details->mana;
  578. int maxSpellPoints = hero.details->manaLimit;
  579. new CAnimImage("PortraitsLarge", hero.portrait, 0, 10, 6);
  580. //primary stats
  581. new CLabel(9, 75, EFonts::FONT_TINY, EAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":");
  582. new CLabel(9, 87, EFonts::FONT_TINY, EAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":");
  583. new CLabel(9, 99, EFonts::FONT_TINY, EAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":");
  584. new CLabel(9, 111, EFonts::FONT_TINY, EAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":");
  585. new CLabel(69, 87, EFonts::FONT_TINY, EAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack));
  586. new CLabel(69, 99, EFonts::FONT_TINY, EAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense));
  587. new CLabel(69, 111, EFonts::FONT_TINY, EAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power));
  588. new CLabel(69, 123, EFonts::FONT_TINY, EAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge));
  589. //morale+luck
  590. new CLabel(9, 131, EFonts::FONT_TINY, EAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":");
  591. new CLabel(9, 143, EFonts::FONT_TINY, EAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":");
  592. new CAnimImage("IMRL22", morale + 3, 0, 47, 131);
  593. new CAnimImage("ILCK22", luck + 3, 0, 47, 143);
  594. //spell points
  595. new CLabel(39, 174, EFonts::FONT_TINY, EAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]);
  596. new CLabel(39, 186, EFonts::FONT_TINY, EAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints));
  597. }
  598. void CStackQueue::update()
  599. {
  600. stacksSorted.clear();
  601. owner->getCurrentPlayerInterface()->cb->battleGetStackQueue(stacksSorted, stackBoxes.size());
  602. if(stacksSorted.size())
  603. {
  604. for(int i = 0; i < stackBoxes.size(); i++)
  605. {
  606. stackBoxes[i]->setStack(stacksSorted[i]);
  607. }
  608. }
  609. else
  610. {
  611. //no stacks on battlefield... what to do with queue?
  612. }
  613. }
  614. CStackQueue::CStackQueue(bool Embedded, CBattleInterface * _owner)
  615. : embedded(Embedded), owner(_owner)
  616. {
  617. OBJ_CONSTRUCTION_CAPTURING_ALL;
  618. if(embedded)
  619. {
  620. bg = nullptr;
  621. pos.w = QUEUE_SIZE * 37;
  622. pos.h = 46;
  623. pos.x = screen->w / 2 - pos.w / 2;
  624. pos.y = (screen->h - 600) / 2 + 10;
  625. }
  626. else
  627. {
  628. bg = BitmapHandler::loadBitmap("DIBOXBCK");
  629. pos.w = 800;
  630. pos.h = 85;
  631. }
  632. stackBoxes.resize(QUEUE_SIZE);
  633. for(int i = 0; i < stackBoxes.size(); i++)
  634. {
  635. stackBoxes[i] = new StackBox(embedded);
  636. stackBoxes[i]->moveBy(Point(1 + (embedded ? 36 : 80) * i, 0));
  637. }
  638. }
  639. CStackQueue::~CStackQueue()
  640. {
  641. SDL_FreeSurface(bg);
  642. }
  643. void CStackQueue::showAll(SDL_Surface * to)
  644. {
  645. blitBg(to);
  646. CIntObject::showAll(to);
  647. }
  648. void CStackQueue::blitBg(SDL_Surface * to)
  649. {
  650. if(bg)
  651. {
  652. SDL_SetClipRect(to, &pos);
  653. CSDL_Ext::fillTexture(to, bg);
  654. SDL_SetClipRect(to, nullptr);
  655. }
  656. }
  657. void CStackQueue::StackBox::showAll(SDL_Surface * to)
  658. {
  659. assert(stack);
  660. bg->colorize(stack->owner);
  661. CIntObject::showAll(to);
  662. if(small)
  663. printAtMiddleLoc(makeNumberShort(stack->getCount()), pos.w / 2, pos.h - 7, FONT_SMALL, Colors::WHITE, to);
  664. else
  665. printAtMiddleLoc(makeNumberShort(stack->getCount()), pos.w / 2, pos.h - 8, FONT_MEDIUM, Colors::WHITE, to);
  666. }
  667. void CStackQueue::StackBox::setStack(const CStack * stack)
  668. {
  669. this->stack = stack;
  670. assert(stack);
  671. icon->setFrame(stack->getCreature()->iconIndex);
  672. }
  673. CStackQueue::StackBox::StackBox(bool small)
  674. : stack(nullptr), small(small)
  675. {
  676. OBJ_CONSTRUCTION_CAPTURING_ALL;
  677. bg = new CPicture(small ? "StackQueueSmall" : "StackQueueLarge");
  678. if(small)
  679. {
  680. icon = new CAnimImage("CPRSMALL", 0, 0, 5, 2);
  681. }
  682. else
  683. icon = new CAnimImage("TWCRPORT", 0, 0, 9, 1);
  684. pos.w = bg->pos.w;
  685. pos.h = bg->pos.h;
  686. }