CBattleInterfaceClasses.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. 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. NULL,
  125. screen,
  126. &temp_rect);
  127. //animation of hero
  128. SDL_Rect rect = pos;
  129. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[currentFrame].bitmap, NULL, 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 != NULL && 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. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), myHero, myOwner->curInt);
  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(size_t i = 0; i < dh->ourImages.size(); ++i) //transforming images
  187. {
  188. if(flip)
  189. {
  190. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  191. SDL_FreeSurface(dh->ourImages[i].bitmap);
  192. dh->ourImages[i].bitmap = hlp;
  193. }
  194. CSDL_Ext::alphaTransform(dh->ourImages[i].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(size_t i = 0; i < flag->ourImages.size(); ++i)
  202. {
  203. CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  204. graphics->blueToPlayersAdv(flag->ourImages[i].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", NULL, 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", NULL, 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", NULL, 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, CBattleInterface * _owner)
  268. : owner(_owner)
  269. {
  270. OBJ_CONSTRUCTION_CAPTURING_ALL;
  271. this->pos = pos;
  272. CPicture * bg = new CPicture("CPRESULT");
  273. bg->colorize(owner->curInt->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 attackerName, defenderName;
  291. if(owner->attackingHeroInstance) //a hero attacked
  292. {
  293. new CAnimImage("PortraitsLarge", owner->attackingHeroInstance->portrait, 0, 21, 38);
  294. //setting attackerName
  295. attackerName = owner->attackingHeroInstance->name;
  296. }
  297. else //a monster attacked
  298. {
  299. int bestMonsterID = -1;
  300. ui32 bestPower = 0;
  301. for(TSlots::const_iterator it = owner->army1->Slots().begin(); it!=owner->army1->Slots().end(); ++it)
  302. {
  303. if(it->second->type->AIValue > bestPower)
  304. {
  305. bestPower = it->second->type->AIValue;
  306. bestMonsterID = it->second->type->idNumber;
  307. }
  308. }
  309. new CAnimImage("TWCRPORT", bestMonsterID+2, 0, 21, 38);
  310. //setting attackerName
  311. attackerName = CGI->creh->creatures[bestMonsterID]->namePl;
  312. }
  313. if(owner->defendingHeroInstance) //a hero defended
  314. {
  315. new CAnimImage("PortraitsLarge", owner->defendingHeroInstance->portrait, 0, 392, 38);
  316. //setting defenderName
  317. defenderName = owner->defendingHeroInstance->name;
  318. }
  319. else //a monster defended
  320. {
  321. int bestMonsterID = -1;
  322. ui32 bestPower = 0;
  323. for(TSlots::const_iterator it = owner->army2->Slots().begin(); it!=owner->army2->Slots().end(); ++it)
  324. {
  325. if( it->second->type->AIValue > bestPower)
  326. {
  327. bestPower = it->second->type->AIValue;
  328. bestMonsterID = it->second->type->idNumber;
  329. }
  330. }
  331. new CAnimImage("TWCRPORT", CGI->creh->creatures[bestMonsterID]->iconIndex, 0, 392, 38);
  332. //setting defenderName
  333. defenderName = CGI->creh->creatures[bestMonsterID]->namePl;
  334. }
  335. //printing attacker and defender's names
  336. new CLabel( 89, 37, FONT_SMALL, TOPLEFT, Colors::WHITE, attackerName);
  337. new CLabel( 381, 53, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, defenderName);
  338. //printing casualities
  339. for(int step = 0; step < 2; ++step)
  340. {
  341. if(br.casualties[step].size()==0)
  342. {
  343. new CLabel( 235, 360 + 97*step, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]);
  344. }
  345. else
  346. {
  347. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  348. int yPos = 344 + step*97;
  349. for(std::map<ui32,si32>::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  350. {
  351. new CAnimImage("CPRSMALL", CGI->creh->creatures[it->first]->iconIndex, 0, xPos, yPos);
  352. std::ostringstream amount;
  353. amount<<it->second;
  354. new CLabel( xPos+16, yPos + 42, FONT_SMALL, CENTER, Colors::WHITE, amount.str());
  355. xPos += 42;
  356. }
  357. }
  358. }
  359. //printing result description
  360. bool weAreAttacker = (owner->curInt->playerID == owner->attackingHeroInstance->tempOwner);
  361. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  362. {
  363. int text=-1;
  364. switch(br.result)
  365. {
  366. case BattleResult::NORMAL: text = 304; break;
  367. case BattleResult::ESCAPE: text = 303; break;
  368. case BattleResult::SURRENDER: text = 302; break;
  369. }
  370. CCS->musich->playMusic("Music/Win Battle", false);
  371. CCS->videoh->open("WIN3.BIK");
  372. std::string str = CGI->generaltexth->allTexts[text];
  373. const CGHeroInstance * ourHero = weAreAttacker? owner->attackingHeroInstance : owner->defendingHeroInstance;
  374. if (ourHero)
  375. {
  376. str += CGI->generaltexth->allTexts[305];
  377. boost::algorithm::replace_first(str,"%s",ourHero->name);
  378. boost::algorithm::replace_first(str,"%d",boost::lexical_cast<std::string>(br.exp[weAreAttacker?0:1]));
  379. }
  380. new CTextBox(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  381. }
  382. else // we lose
  383. {
  384. switch(br.result)
  385. {
  386. case BattleResult::NORMAL:
  387. {
  388. CCS->musich->playMusic("Music/LoseCombat", false);
  389. CCS->videoh->open("LBSTART.BIK");
  390. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[311]);
  391. break;
  392. }
  393. case BattleResult::ESCAPE: //flee
  394. {
  395. CCS->musich->playMusic("Music/Retreat Battle", false);
  396. CCS->videoh->open("RTSTART.BIK");
  397. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[310]);
  398. break;
  399. }
  400. case BattleResult::SURRENDER:
  401. {
  402. CCS->musich->playMusic("Music/Surrender Battle", false);
  403. CCS->videoh->open("SURRENDER.BIK");
  404. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[309]);
  405. break;
  406. }
  407. }
  408. }
  409. }
  410. CBattleResultWindow::~CBattleResultWindow()
  411. {
  412. }
  413. void CBattleResultWindow::activate()
  414. {
  415. owner->curInt->showingDialog->set(true);
  416. CIntObject::activate();
  417. }
  418. void CBattleResultWindow::show(SDL_Surface * to)
  419. {
  420. CIntObject::show(to);
  421. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  422. }
  423. void CBattleResultWindow::bExitf()
  424. {
  425. if(LOCPLINT->cb->getStartInfo()->mode == StartInfo::DUEL)
  426. {
  427. CGuiHandler::pushSDLEvent(SDL_QUIT);
  428. return;
  429. }
  430. CPlayerInterface * intTmp = owner->curInt;
  431. GH.popInts(2); //first - we; second - battle interface
  432. intTmp->showingDialog->setn(false);
  433. CCS->videoh->close();
  434. }
  435. Point CClickableHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi)
  436. {
  437. Point ret(-500, -500); //returned value
  438. if(stack && stack->position < 0) //creatures in turrets
  439. {
  440. switch(stack->position)
  441. {
  442. case -2: //keep
  443. ret = cbi->siegeH->town->town->clientInfo.siegePositions[18];
  444. break;
  445. case -3: //lower turret
  446. ret = cbi->siegeH->town->town->clientInfo.siegePositions[19];
  447. break;
  448. case -4: //upper turret
  449. ret = cbi->siegeH->town->town->clientInfo.siegePositions[20];
  450. break;
  451. }
  452. }
  453. else
  454. {
  455. ret.y = -139 + 42 * (hexNum/GameConstants::BFIELD_WIDTH); //counting y
  456. //counting x
  457. if(attacker)
  458. {
  459. ret.x = -160 + 22 * ( ((hexNum/GameConstants::BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % GameConstants::BFIELD_WIDTH);
  460. }
  461. else
  462. {
  463. ret.x = -219 + 22 * ( ((hexNum/GameConstants::BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % GameConstants::BFIELD_WIDTH);
  464. }
  465. //shifting position for double - hex creatures
  466. if(stack && stack->doubleWide())
  467. {
  468. if(attacker)
  469. {
  470. ret.x -= 44;
  471. }
  472. else
  473. {
  474. ret.x += 45;
  475. }
  476. }
  477. }
  478. //returning
  479. return ret +CPlayerInterface::battleInt->pos;
  480. }
  481. void CClickableHex::hover(bool on)
  482. {
  483. hovered = on;
  484. //Hoverable::hover(on);
  485. if(!on && setAlterText)
  486. {
  487. myInterface->console->alterTxt = std::string();
  488. setAlterText = false;
  489. }
  490. }
  491. CClickableHex::CClickableHex() : setAlterText(false), myNumber(-1), accessible(true), hovered(false), strictHovered(false), myInterface(NULL)
  492. {
  493. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  494. }
  495. void CClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  496. {
  497. if(myInterface->cellShade)
  498. {
  499. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  500. {
  501. strictHovered = false;
  502. }
  503. else //hovered pixel is inside hex
  504. {
  505. strictHovered = true;
  506. }
  507. }
  508. if(hovered && strictHovered) //print attacked creature to console
  509. {
  510. const CStack * attackedStack = myInterface->curInt->cb->battleGetStackByPos(myNumber);
  511. if(myInterface->console->alterTxt.size() == 0 &&attackedStack != NULL &&
  512. attackedStack->owner != myInterface->curInt->playerID &&
  513. attackedStack->alive())
  514. {
  515. char tabh[160];
  516. const std::string & attackedName = attackedStack->count == 1 ? attackedStack->getCreature()->nameSing : attackedStack->getCreature()->namePl;
  517. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  518. myInterface->console->alterTxt = std::string(tabh);
  519. setAlterText = true;
  520. }
  521. }
  522. else if(setAlterText)
  523. {
  524. myInterface->console->alterTxt = std::string();
  525. setAlterText = false;
  526. }
  527. }
  528. void CClickableHex::clickLeft(tribool down, bool previousState)
  529. {
  530. if(!down && hovered && strictHovered) //we've been really clicked!
  531. {
  532. myInterface->hexLclicked(myNumber);
  533. }
  534. }
  535. void CClickableHex::clickRight(tribool down, bool previousState)
  536. {
  537. const CStack * myst = myInterface->curInt->cb->battleGetStackByPos(myNumber); //stack info
  538. if(hovered && strictHovered && myst!=NULL)
  539. {
  540. if(!myst->alive()) return;
  541. if(down)
  542. {
  543. GH.pushInt(createCreWindow(myst));
  544. }
  545. }
  546. }
  547. void CStackQueue::update()
  548. {
  549. stacksSorted.clear();
  550. owner->curInt->cb->battleGetStackQueue(stacksSorted, stackBoxes.size());
  551. if(stacksSorted.size())
  552. {
  553. for (int i = 0; i < stackBoxes.size() ; i++)
  554. {
  555. stackBoxes[i]->setStack(stacksSorted[i]);
  556. }
  557. }
  558. else
  559. {
  560. //no stacks on battlefield... what to do with queue?
  561. }
  562. }
  563. CStackQueue::CStackQueue(bool Embedded, CBattleInterface * _owner)
  564. :embedded(Embedded), owner(_owner)
  565. {
  566. OBJ_CONSTRUCTION_CAPTURING_ALL;
  567. if(embedded)
  568. {
  569. bg = NULL;
  570. pos.w = QUEUE_SIZE * 37;
  571. pos.h = 46;
  572. pos.x = screen->w/2 - pos.w/2;
  573. pos.y = (screen->h - 600)/2 + 10;
  574. }
  575. else
  576. {
  577. bg = BitmapHandler::loadBitmap("DIBOXBCK");
  578. pos.w = 800;
  579. pos.h = 85;
  580. }
  581. stackBoxes.resize(QUEUE_SIZE);
  582. for (int i = 0; i < stackBoxes.size(); i++)
  583. {
  584. stackBoxes[i] = new StackBox(embedded);
  585. stackBoxes[i]->moveBy(Point(1 + (embedded ? 36 : 80)*i, 0));
  586. }
  587. }
  588. CStackQueue::~CStackQueue()
  589. {
  590. SDL_FreeSurface(bg);
  591. }
  592. void CStackQueue::showAll(SDL_Surface * to)
  593. {
  594. blitBg(to);
  595. CIntObject::showAll(to);
  596. }
  597. void CStackQueue::blitBg( SDL_Surface * to )
  598. {
  599. if(bg)
  600. {
  601. SDL_SetClipRect(to, &pos);
  602. CSDL_Ext::fillTexture(to, bg);
  603. SDL_SetClipRect(to, nullptr);
  604. }
  605. }
  606. void CStackQueue::StackBox::showAll(SDL_Surface * to)
  607. {
  608. assert(stack);
  609. bg->colorize(stack->owner);
  610. CIntObject::showAll(to);
  611. if(small)
  612. printAtMiddleLoc(makeNumberShort(stack->count), pos.w/2, pos.h - 7, FONT_SMALL, Colors::WHITE, to);
  613. else
  614. printAtMiddleLoc(makeNumberShort(stack->count), pos.w/2, pos.h - 8, FONT_MEDIUM, Colors::WHITE, to);
  615. }
  616. void CStackQueue::StackBox::setStack( const CStack *stack )
  617. {
  618. this->stack = stack;
  619. assert(stack);
  620. icon->setFrame(stack->getCreature()->iconIndex);
  621. }
  622. CStackQueue::StackBox::StackBox(bool small):
  623. stack(nullptr),
  624. small(small)
  625. {
  626. OBJ_CONSTRUCTION_CAPTURING_ALL;
  627. bg = new CPicture(small ? "StackQueueBgSmall" : "StackQueueBgBig" );
  628. if (small)
  629. {
  630. icon = new CAnimImage("CPRSMALL", 0, 0, 5, 2);
  631. }
  632. else
  633. icon = new CAnimImage("TWCRPORT", 0, 0, 9, 1);
  634. pos.w = bg->pos.w;
  635. pos.h = bg->pos.h;
  636. }