CBattleInterfaceClasses.cpp 22 KB

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