CBattleInterfaceClasses.cpp 23 KB

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