CBattleInterface.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include "CBattleInterface.h"
  2. #include "CGameInfo.h"
  3. #include "hch\CLodHandler.h"
  4. #include "SDL_Extensions.h"
  5. #include "CAdvmapInterface.h"
  6. #include "AdventureMapButton.h"
  7. #include "hch\CHeroHandler.h"
  8. #include "hch\CDefHandler.h"
  9. #include "CCallback.h"
  10. #include "CGameState.h"
  11. extern SDL_Surface * screen;
  12. SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
  13. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  14. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0)
  15. {
  16. //initializing armies
  17. this->army1 = army1;
  18. this->army2 = army2;
  19. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  20. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  21. {
  22. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  23. creAnims[b->second.ID]->setType(2);
  24. }
  25. //preparing menu background and terrain
  26. std::vector< std::string > & backref = CGI->mh->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  27. background = CGI->bitmaph->loadBitmap(backref[ rand() % backref.size()] );
  28. menu = CGI->bitmaph->loadBitmap("CBAR.BMP");
  29. CSDL_Ext::blueToPlayersAdv(menu, hero1->tempOwner);
  30. //blitting menu background and terrain
  31. blitAt(background, 0, 0);
  32. blitAt(menu, 0, 556);
  33. CSDL_Ext::update();
  34. //preparing buttons
  35. bOptions = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bOptionsf, 3, 561, "icm003.def", this, false, NULL, false);
  36. bSurrender = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bSurrenderf, 54, 561, "icm001.def", this, false, NULL, false);
  37. bFlee = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bFleef, 105, 561, "icm002.def", this, false, NULL, false);
  38. bAutofight = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bAutofightf, 157, 561, "icm004.def", this, false, NULL, false);
  39. bSpell = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bSpellf, 645, 561, "icm005.def", this, false, NULL, false);
  40. bWait = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bWaitf, 696, 561, "icm006.def", this, false, NULL, false);
  41. bDefence = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bDefencef, 747, 561, "icm007.def", this, false, NULL, false);
  42. bConsoleUp = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bConsoleUpf, 624, 561, "ComSlide.def", this, false, NULL, false);
  43. bConsoleDown = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bConsoleDownf, 624, 580, "ComSlide.def", this, false, NULL, false);
  44. bConsoleDown->bitmapOffset = 2;
  45. //loading hero animations
  46. if(hero1) // attacking hero
  47. {
  48. attackingHero = new CBattleHero(CGI->mh->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  49. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  50. }
  51. else
  52. {
  53. attackingHero = NULL;
  54. }
  55. if(hero2) // defending hero
  56. {
  57. defendingHero = new CBattleHero(CGI->mh->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  58. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  59. }
  60. else
  61. {
  62. defendingHero = NULL;
  63. }
  64. //preparing cells and hexes
  65. cellBorder = CGI->bitmaph->loadBitmap("CCELLGRD.BMP");
  66. cellBorder = CSDL_Ext::alphaTransform(cellBorder);
  67. cellShade = CGI->bitmaph->loadBitmap("CCELLSHD.BMP");
  68. cellShade = CSDL_Ext::alphaTransform(cellShade);
  69. for(int h=0; h<187; ++h)
  70. {
  71. bfield[h].myNumber = h;
  72. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  73. int y = 86 + 42 * (h/17);
  74. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  75. }
  76. }
  77. CBattleInterface::~CBattleInterface()
  78. {
  79. SDL_FreeSurface(background);
  80. SDL_FreeSurface(menu);
  81. delete bOptions;
  82. delete bSurrender;
  83. delete bFlee;
  84. delete bAutofight;
  85. delete bSpell;
  86. delete bWait;
  87. delete bDefence;
  88. delete bConsoleUp;
  89. delete bConsoleDown;
  90. delete attackingHero;
  91. delete defendingHero;
  92. SDL_FreeSurface(cellBorder);
  93. SDL_FreeSurface(cellShade);
  94. for(int g=0; g<creAnims.size(); ++g)
  95. delete creAnims[g];
  96. }
  97. void CBattleInterface::activate()
  98. {
  99. bOptions->activate();
  100. bSurrender->activate();
  101. bFlee->activate();
  102. bAutofight->activate();
  103. bSpell->activate();
  104. bWait->activate();
  105. bDefence->activate();
  106. bConsoleUp->activate();
  107. bConsoleDown->activate();
  108. for(int b=0; b<187; ++b)
  109. {
  110. bfield[b].activate();
  111. }
  112. }
  113. void CBattleInterface::deactivate()
  114. {
  115. bOptions->deactivate();
  116. bSurrender->deactivate();
  117. bFlee->deactivate();
  118. bAutofight->deactivate();
  119. bSpell->deactivate();
  120. bWait->deactivate();
  121. bDefence->deactivate();
  122. bConsoleUp->deactivate();
  123. bConsoleDown->deactivate();
  124. for(int b=0; b<187; ++b)
  125. {
  126. bfield[b].deactivate();
  127. }
  128. }
  129. void CBattleInterface::show(SDL_Surface * to)
  130. {
  131. ++animCount;
  132. if(!to) //"evaluating" to
  133. to = screen;
  134. //showing background
  135. blitAt(background, 0, 0, to);
  136. if(printCellBorders) //printing cell borders
  137. {
  138. for(int i=0; i<11; ++i) //rows
  139. {
  140. for(int j=0; j<15; ++j) //columns
  141. {
  142. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  143. int y = 86 + 42 * i;
  144. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorder, NULL, to, &genRect(cellBorder->h, cellBorder->w, x, y));
  145. }
  146. }
  147. }
  148. //printing hovered cell
  149. for(int b=0; b<187; ++b)
  150. {
  151. if(bfield[b].strictHovered && bfield[b].hovered)
  152. {
  153. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  154. int y = 86 + 42 * (b/17);
  155. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  156. }
  157. }
  158. //showing menu background
  159. blitAt(menu, 0, 556, to);
  160. //showing buttons
  161. bOptions->show(to);
  162. bSurrender->show(to);
  163. bFlee->show(to);
  164. bAutofight->show(to);
  165. bSpell->show(to);
  166. bWait->show(to);
  167. bDefence->show(to);
  168. bConsoleUp->show(to);
  169. bConsoleDown->show(to);
  170. //showing hero animations
  171. if(attackingHero)
  172. attackingHero->show(to);
  173. if(defendingHero)
  174. defendingHero->show(to);
  175. //showing units //a lot of work...
  176. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  177. for(std::map<int, CCreatureAnimation*>::iterator j=creAnims.begin(); j!=creAnims.end(); ++j)
  178. {
  179. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(stacks[j->first].position, stacks[j->first].owner == attackingHeroInstance->tempOwner);
  180. j->second->nextFrame(to, coords.first, coords.second, stacks[j->first].owner == attackingHeroInstance->tempOwner, animCount%2==0);
  181. }
  182. //units shown
  183. CSDL_Ext::update();
  184. }
  185. void CBattleInterface::bOptionsf()
  186. {
  187. }
  188. void CBattleInterface::bSurrenderf()
  189. {
  190. }
  191. void CBattleInterface::bFleef()
  192. {
  193. for(int i=0; i<LOCPLINT->objsToBlit.size(); ++i)
  194. {
  195. if( dynamic_cast<CBattleInterface*>( LOCPLINT->objsToBlit[i] ) )
  196. {
  197. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+i);
  198. }
  199. }
  200. deactivate();
  201. LOCPLINT->adventureInt->activate();
  202. delete this;
  203. }
  204. void CBattleInterface::bAutofightf()
  205. {
  206. }
  207. void CBattleInterface::bSpellf()
  208. {
  209. }
  210. void CBattleInterface::bWaitf()
  211. {
  212. }
  213. void CBattleInterface::bDefencef()
  214. {
  215. }
  216. void CBattleInterface::bConsoleUpf()
  217. {
  218. }
  219. void CBattleInterface::bConsoleDownf()
  220. {
  221. }
  222. void CBattleInterface::newStack(CStack stack)
  223. {
  224. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  225. creAnims[stack.ID]->setType(2);
  226. }
  227. void CBattleInterface::stackRemoved(CStack stack)
  228. {
  229. delete creAnims[stack.ID];
  230. creAnims.erase(stack.ID);
  231. }
  232. void CBattleHero::show(SDL_Surface *to)
  233. {
  234. int tick=-1;
  235. for(int i=0; i<dh->ourImages.size(); ++i)
  236. {
  237. if(dh->ourImages[i].groupNumber==phase)
  238. ++tick;
  239. if(tick==image)
  240. {
  241. SDL_Rect posb = pos;
  242. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  243. ++image;
  244. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  245. {
  246. image = 0;
  247. }
  248. break;
  249. }
  250. }
  251. if(flip)
  252. {
  253. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  254. }
  255. else
  256. {
  257. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  258. }
  259. {
  260. ++flagAnim;
  261. flagAnim %= flag->ourImages.size();
  262. }
  263. }
  264. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  265. {
  266. dh = CGI->spriteh->giveDef( defName );
  267. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  268. {
  269. if(flip)
  270. dh->ourImages[i].bitmap = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  271. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  272. }
  273. dh->alphaTransformed = true;
  274. if(flip)
  275. flag = CGI->spriteh->giveDef("CMFLAGR.DEF");
  276. else
  277. flag = CGI->spriteh->giveDef("CMFLAGL.DEF");
  278. //coloring flag and adding transparency
  279. for(int i=0; i<flag->ourImages.size(); ++i)
  280. {
  281. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  282. CSDL_Ext::blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  283. }
  284. }
  285. CBattleHero::~CBattleHero()
  286. {
  287. delete dh;
  288. delete flag;
  289. }
  290. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker)
  291. {
  292. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  293. ret.second = -139 + 42 * (hexNum/17); //counting y
  294. //counting x
  295. if(attacker)
  296. {
  297. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  298. }
  299. else
  300. {
  301. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  302. }
  303. //returning
  304. return ret;
  305. }
  306. void CBattleHex::activate()
  307. {
  308. Hoverable::activate();
  309. MotionInterested::activate();
  310. }
  311. void CBattleHex::deactivate()
  312. {
  313. Hoverable::deactivate();
  314. MotionInterested::deactivate();
  315. }
  316. void CBattleHex::hover(bool on)
  317. {
  318. hovered = on;
  319. Hoverable::hover(on);
  320. }
  321. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false)
  322. {
  323. }
  324. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  325. {
  326. if(CBattleInterface::cellShade)
  327. {
  328. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  329. {
  330. strictHovered = false;
  331. }
  332. else //hovered pixel is inside hex
  333. {
  334. strictHovered = true;
  335. }
  336. }
  337. }