CBattleInterfaceClasses.cpp 22 KB

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