CBattleInterfaceClasses.cpp 21 KB

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