2
0

CBattleInterfaceClasses.cpp 22 KB

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