CBattleInterfaceClasses.cpp 22 KB

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