CBattleInterfaceClasses.cpp 22 KB

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