CBattleInterfaceClasses.cpp 22 KB

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