CBattleResultWindow.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "StdInc.h"
  2. #include "CBattleResultWindow.h"
  3. #include "CBattleInterface.h"
  4. #include "../AdventureMapButton.h"
  5. #include "../CGameInfo.h"
  6. #include "../../lib/CObjectHandler.h"
  7. #include "../../lib/NetPacks.h"
  8. #include "../../lib/CCreatureHandler.h"
  9. #include "../../lib/CGeneralTextHandler.h"
  10. #include "../CMusicHandler.h"
  11. #include "../CPlayerInterface.h"
  12. #include "../Graphics.h"
  13. #include "../../CCallback.h"
  14. #include "../CVideoHandler.h"
  15. #include "../SDL_Extensions.h"
  16. #include "../CBitmapHandler.h"
  17. #include "../UIFramework/CGuiHandler.h"
  18. CBattleResultWindow::CBattleResultWindow(const BattleResult &br, const SDL_Rect & pos, CBattleInterface * _owner)
  19. : owner(_owner)
  20. {
  21. this->pos = pos;
  22. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  23. graphics->blueToPlayersAdv(background, owner->curInt->playerID);
  24. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  25. SDL_FreeSurface(background);
  26. background = pom;
  27. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleResultWindow::bExitf,this), 384 + pos.x, 505 + pos.y, "iok6432.def", SDLK_RETURN);
  28. exit->borderColor = Colors::MetallicGold;
  29. exit->borderEnabled = true;
  30. if(br.winner==0) //attacker won
  31. {
  32. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 59, 124, FONT_SMALL, zwykly, background);
  33. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 408, 124, FONT_SMALL, zwykly, background);
  34. }
  35. else //if(br.winner==1)
  36. {
  37. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 59, 124, FONT_SMALL, zwykly, background);
  38. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 412, 124, FONT_SMALL, zwykly, background);
  39. }
  40. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 232, 302, FONT_BIG, tytulowy, background);
  41. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 232, 332, FONT_BIG, zwykly, background);
  42. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 237, 428, FONT_BIG, zwykly, background);
  43. std::string attackerName, defenderName;
  44. if(owner->attackingHeroInstance) //a hero attacked
  45. {
  46. SDL_Rect temp_rect = genRect(64, 58, 21, 38);
  47. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &temp_rect);
  48. //setting attackerName
  49. attackerName = owner->attackingHeroInstance->name;
  50. }
  51. else //a monster attacked
  52. {
  53. int bestMonsterID = -1;
  54. ui32 bestPower = 0;
  55. for(TSlots::const_iterator it = owner->army1->Slots().begin(); it!=owner->army1->Slots().end(); ++it)
  56. {
  57. if(it->second->type->AIValue > bestPower)
  58. {
  59. bestPower = it->second->type->AIValue;
  60. bestMonsterID = it->second->type->idNumber;
  61. }
  62. }
  63. SDL_Rect temp_rect = genRect(64, 58, 21, 38);
  64. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &temp_rect);
  65. //setting attackerName
  66. attackerName = CGI->creh->creatures[bestMonsterID]->namePl;
  67. }
  68. if(owner->defendingHeroInstance) //a hero defended
  69. {
  70. SDL_Rect temp_rect = genRect(64, 58, 392, 38);
  71. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &temp_rect);
  72. //setting defenderName
  73. defenderName = owner->defendingHeroInstance->name;
  74. }
  75. else //a monster defended
  76. {
  77. int bestMonsterID = -1;
  78. ui32 bestPower = 0;
  79. for(TSlots::const_iterator it = owner->army2->Slots().begin(); it!=owner->army2->Slots().end(); ++it)
  80. {
  81. if( it->second->type->AIValue > bestPower)
  82. {
  83. bestPower = it->second->type->AIValue;
  84. bestMonsterID = it->second->type->idNumber;
  85. }
  86. }
  87. SDL_Rect temp_rect = genRect(64, 58, 392, 38);
  88. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &temp_rect);
  89. //setting defenderName
  90. defenderName = CGI->creh->creatures[bestMonsterID]->namePl;
  91. }
  92. //printing attacker and defender's names
  93. CSDL_Ext::printAt(attackerName, 89, 37, FONT_SMALL, zwykly, background);
  94. CSDL_Ext::printTo(defenderName, 381, 53, FONT_SMALL, zwykly, background);
  95. //printing casualities
  96. for(int step = 0; step < 2; ++step)
  97. {
  98. if(br.casualties[step].size()==0)
  99. {
  100. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, FONT_SMALL, zwykly, background);
  101. }
  102. else
  103. {
  104. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  105. int yPos = 344 + step*97;
  106. for(std::map<ui32,si32>::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  107. {
  108. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  109. std::ostringstream amount;
  110. amount<<it->second;
  111. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, FONT_SMALL, zwykly, background);
  112. xPos += 42;
  113. }
  114. }
  115. }
  116. //printing result description
  117. bool weAreAttacker = (owner->curInt->playerID == owner->attackingHeroInstance->tempOwner);
  118. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  119. {
  120. int text=-1;
  121. switch(br.result)
  122. {
  123. case 0: text = 304; break;
  124. case 1: text = 303; break;
  125. case 2: text = 302; break;
  126. }
  127. CCS->musich->playMusic(musicBase::winBattle);
  128. CCS->videoh->open(VIDEO_WIN);
  129. std::string str = CGI->generaltexth->allTexts[text];
  130. const CGHeroInstance * ourHero = weAreAttacker? owner->attackingHeroInstance : owner->defendingHeroInstance;
  131. if (ourHero)
  132. {
  133. str += CGI->generaltexth->allTexts[305];
  134. boost::algorithm::replace_first(str,"%s",ourHero->name);
  135. boost::algorithm::replace_first(str,"%d",boost::lexical_cast<std::string>(br.exp[weAreAttacker?0:1]));
  136. }
  137. CSDL_Ext::printAtMiddleWB(str, 235, 235, FONT_SMALL, 55, zwykly, background);
  138. }
  139. else // we lose
  140. {
  141. switch(br.result)
  142. {
  143. case 0: //normal victory
  144. {
  145. CCS->musich->playMusic(musicBase::loseCombat);
  146. CCS->videoh->open(VIDEO_LOSE_BATTLE_START);
  147. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, FONT_SMALL, zwykly, background);
  148. break;
  149. }
  150. case 1: //flee
  151. {
  152. CCS->musich->playMusic(musicBase::retreatBattle);
  153. CCS->videoh->open(VIDEO_RETREAT_START);
  154. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, FONT_SMALL, zwykly, background);
  155. break;
  156. }
  157. case 2: //surrender
  158. {
  159. CCS->musich->playMusic(musicBase::surrenderBattle);
  160. CCS->videoh->open(VIDEO_SURRENDER);
  161. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 220, FONT_SMALL, zwykly, background);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. CBattleResultWindow::~CBattleResultWindow()
  168. {
  169. SDL_FreeSurface(background);
  170. }
  171. void CBattleResultWindow::activate()
  172. {
  173. owner->curInt->showingDialog->set(true);
  174. exit->activate();
  175. }
  176. void CBattleResultWindow::deactivate()
  177. {
  178. exit->deactivate();
  179. }
  180. void CBattleResultWindow::show(SDL_Surface *to)
  181. {
  182. //evaluating to
  183. if(!to)
  184. to = screen;
  185. CCS->videoh->update(107, 70, background, false, true);
  186. SDL_BlitSurface(background, NULL, to, &pos);
  187. exit->showAll(to);
  188. }
  189. void CBattleResultWindow::bExitf()
  190. {
  191. if(LOCPLINT->cb->getStartInfo()->mode == StartInfo::DUEL)
  192. {
  193. std::exit(0);
  194. }
  195. CPlayerInterface * intTmp = owner->curInt;
  196. GH.popInts(2); //first - we; second - battle interface
  197. intTmp->showingDialog->setn(false);
  198. CCS->videoh->close();
  199. }