CBattleInterfaceClasses.cpp 23 KB

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