CBattleInterfaceClasses.cpp 22 KB

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