CBattleInterface.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. #include "hch\CGeneralTextHandler.h"
  12. #include <queue>
  13. #include <sstream>
  14. extern SDL_Surface * screen;
  15. extern TTF_Font * GEOR13;
  16. extern SDL_Color zwykly;
  17. SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
  18. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  19. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL)
  20. {
  21. //initializing armies
  22. this->army1 = army1;
  23. this->army2 = army2;
  24. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  25. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  26. {
  27. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  28. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  29. creAnims[b->second.ID]->setType(2);
  30. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  31. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  32. }
  33. //preparing menu background and terrain
  34. std::vector< std::string > & backref = CGI->mh->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  35. background = CGI->bitmaph->loadBitmap(backref[ rand() % backref.size()] );
  36. menu = CGI->bitmaph->loadBitmap("CBAR.BMP");
  37. CSDL_Ext::blueToPlayersAdv(menu, hero1->tempOwner);
  38. //preparing graphics for displaying amounts of creatures
  39. amountBasic = CGI->bitmaph->loadBitmap("CMNUMWIN.BMP");
  40. amountNormal = CGI->bitmaph->loadBitmap("CMNUMWIN.BMP");
  41. CSDL_Ext::alphaTransform(amountNormal);
  42. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  43. {
  44. if((amountNormal->format->palette->colors+g)->b != 132 &&
  45. (amountNormal->format->palette->colors+g)->g != 231 &&
  46. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  47. {
  48. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  49. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  50. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  51. }
  52. }
  53. //blitting menu background and terrain
  54. blitAt(background, 0, 0);
  55. blitAt(menu, 0, 556);
  56. CSDL_Ext::update();
  57. //preparing buttons and console
  58. bOptions = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bOptionsf, 3, 561, "icm003.def", this, false, NULL, false);
  59. bSurrender = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bSurrenderf, 54, 561, "icm001.def", this, false, NULL, false);
  60. bFlee = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bFleef, 105, 561, "icm002.def", this, false, NULL, false);
  61. bAutofight = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bAutofightf, 157, 561, "icm004.def", this, false, NULL, false);
  62. bSpell = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bSpellf, 645, 561, "icm005.def", this, false, NULL, false);
  63. bWait = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bWaitf, 696, 561, "icm006.def", this, false, NULL, false);
  64. bDefence = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bDefencef, 747, 561, "icm007.def", this, false, NULL, false);
  65. bConsoleUp = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bConsoleUpf, 624, 561, "ComSlide.def", this, false, NULL, false);
  66. bConsoleDown = new AdventureMapButton<CBattleInterface> (std::string(), std::string(), &CBattleInterface::bConsoleDownf, 624, 580, "ComSlide.def", this, false, NULL, false);
  67. bConsoleDown->bitmapOffset = 2;
  68. console = new CBattleConsole();
  69. console->pos.x = 211;
  70. console->pos.y = 560;
  71. console->pos.w = 406;
  72. console->pos.h = 38;
  73. //loading hero animations
  74. if(hero1) // attacking hero
  75. {
  76. attackingHero = new CBattleHero(CGI->mh->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  77. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  78. }
  79. else
  80. {
  81. attackingHero = NULL;
  82. }
  83. if(hero2) // defending hero
  84. {
  85. defendingHero = new CBattleHero(CGI->mh->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  86. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  87. }
  88. else
  89. {
  90. defendingHero = NULL;
  91. }
  92. //preparing cells and hexes
  93. cellBorder = CGI->bitmaph->loadBitmap("CCELLGRD.BMP");
  94. cellBorder = CSDL_Ext::alphaTransform(cellBorder);
  95. cellShade = CGI->bitmaph->loadBitmap("CCELLSHD.BMP");
  96. cellShade = CSDL_Ext::alphaTransform(cellShade);
  97. for(int h=0; h<187; ++h)
  98. {
  99. bfield[h].myNumber = h;
  100. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  101. int y = 86 + 42 * (h/17);
  102. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  103. bfield[h].accesible = true;
  104. bfield[h].myInterface = this;
  105. }
  106. //locking occupied positions on batlefield
  107. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  108. {
  109. bfield[it->second.position].accesible = false;
  110. }
  111. }
  112. CBattleInterface::~CBattleInterface()
  113. {
  114. SDL_FreeSurface(background);
  115. SDL_FreeSurface(menu);
  116. SDL_FreeSurface(amountBasic);
  117. SDL_FreeSurface(amountNormal);
  118. delete bOptions;
  119. delete bSurrender;
  120. delete bFlee;
  121. delete bAutofight;
  122. delete bSpell;
  123. delete bWait;
  124. delete bDefence;
  125. delete bConsoleUp;
  126. delete bConsoleDown;
  127. delete console;
  128. delete attackingHero;
  129. delete defendingHero;
  130. SDL_FreeSurface(cellBorder);
  131. SDL_FreeSurface(cellShade);
  132. for(int g=0; g<creAnims.size(); ++g)
  133. delete creAnims[g];
  134. }
  135. void CBattleInterface::activate()
  136. {
  137. bOptions->activate();
  138. bSurrender->activate();
  139. bFlee->activate();
  140. bAutofight->activate();
  141. bSpell->activate();
  142. bWait->activate();
  143. bDefence->activate();
  144. bConsoleUp->activate();
  145. bConsoleDown->activate();
  146. for(int b=0; b<187; ++b)
  147. {
  148. bfield[b].activate();
  149. }
  150. }
  151. void CBattleInterface::deactivate()
  152. {
  153. bOptions->deactivate();
  154. bSurrender->deactivate();
  155. bFlee->deactivate();
  156. bAutofight->deactivate();
  157. bSpell->deactivate();
  158. bWait->deactivate();
  159. bDefence->deactivate();
  160. bConsoleUp->deactivate();
  161. bConsoleDown->deactivate();
  162. for(int b=0; b<187; ++b)
  163. {
  164. bfield[b].deactivate();
  165. }
  166. }
  167. void CBattleInterface::show(SDL_Surface * to)
  168. {
  169. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  170. ++animCount;
  171. if(!to) //"evaluating" to
  172. to = screen;
  173. //showing background
  174. blitAt(background, 0, 0, to);
  175. if(printCellBorders) //printing cell borders
  176. {
  177. for(int i=0; i<11; ++i) //rows
  178. {
  179. for(int j=0; j<15; ++j) //columns
  180. {
  181. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  182. int y = 86 + 42 * i;
  183. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorder, NULL, to, &genRect(cellBorder->h, cellBorder->w, x, y));
  184. }
  185. }
  186. }
  187. //printing hovered cell
  188. for(int b=0; b<187; ++b)
  189. {
  190. if(bfield[b].strictHovered && bfield[b].hovered)
  191. {
  192. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  193. int y = 86 + 42 * (b/17);
  194. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  195. }
  196. }
  197. //showing selected unit's range
  198. showRange(to, activeStack);
  199. //showing menu background and console
  200. blitAt(menu, 0, 556, to);
  201. console->show(to);
  202. //showing buttons
  203. bOptions->show(to);
  204. bSurrender->show(to);
  205. bFlee->show(to);
  206. bAutofight->show(to);
  207. bSpell->show(to);
  208. bWait->show(to);
  209. bDefence->show(to);
  210. bConsoleUp->show(to);
  211. bConsoleDown->show(to);
  212. //showing hero animations
  213. if(attackingHero)
  214. attackingHero->show(to);
  215. if(defendingHero)
  216. defendingHero->show(to);
  217. //showing units //a lot of work...
  218. int stackByHex[187];
  219. for(int b=0; b<187; ++b)
  220. stackByHex[b] = -1;
  221. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  222. {
  223. stackByHex[j->second.position] = j->second.ID;
  224. }
  225. for(int b=0; b<187; ++b)
  226. {
  227. if(stackByHex[b]!=-1)
  228. {
  229. creAnims[stackByHex[b]]->nextFrame(to, creAnims[stackByHex[b]]->pos.x, creAnims[stackByHex[b]]->pos.y, creDir[stackByHex[b]], animCount%2==0 || creAnims[stackByHex[b]]->getType()==0, stackByHex[b]==activeStack); //increment always when moving
  230. //printing amount
  231. if(stacks[stackByHex[b]].attackerOwned)
  232. {
  233. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackByHex[b]]->pos.x + 220, creAnims[stackByHex[b]]->pos.y + 260));
  234. std::stringstream ss;
  235. ss<<stacks[stackByHex[b]].amount;
  236. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackByHex[b]]->pos.x + 220 + 14, creAnims[stackByHex[b]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  237. }
  238. else
  239. {
  240. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackByHex[b]]->pos.x + 202, creAnims[stackByHex[b]]->pos.y + 260));
  241. std::stringstream ss;
  242. ss<<stacks[stackByHex[b]].amount;
  243. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackByHex[b]]->pos.x + 202 + 14, creAnims[stackByHex[b]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  244. }
  245. }
  246. }
  247. //units shown
  248. CSDL_Ext::update();
  249. }
  250. bool CBattleInterface::reverseCreature(int number, int hex)
  251. {
  252. if(creAnims[number]==NULL)
  253. return false; //there is no such creature
  254. creAnims[number]->setType(8);
  255. for(int g=0; g<creAnims[number]->framesInGroup(8); ++g)
  256. {
  257. show();
  258. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  259. }
  260. creDir[number] = !creDir[number];
  261. CStack curs = LOCPLINT->cb->battleGetStackByID(number);
  262. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  263. creAnims[number]->pos.x = coords.first;
  264. creAnims[number]->pos.y = coords.second;
  265. creAnims[number]->setType(7);
  266. for(int g=0; g<creAnims[number]->framesInGroup(7); ++g)
  267. {
  268. show();
  269. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  270. }
  271. creAnims[number]->setType(2);
  272. return true;
  273. }
  274. void CBattleInterface::bOptionsf()
  275. {
  276. }
  277. void CBattleInterface::bSurrenderf()
  278. {
  279. }
  280. void CBattleInterface::bFleef()
  281. {
  282. BattleAction * ba = new BattleAction;
  283. ba->actionType = 4;
  284. givenCommand = ba;
  285. }
  286. void CBattleInterface::bAutofightf()
  287. {
  288. }
  289. void CBattleInterface::bSpellf()
  290. {
  291. }
  292. void CBattleInterface::bWaitf()
  293. {
  294. }
  295. void CBattleInterface::bDefencef()
  296. {
  297. BattleAction * ba = new BattleAction;
  298. ba->actionType = 3;
  299. ba->stackNumber = activeStack;
  300. givenCommand = ba;
  301. }
  302. void CBattleInterface::bConsoleUpf()
  303. {
  304. console->scrollUp();
  305. }
  306. void CBattleInterface::bConsoleDownf()
  307. {
  308. console->scrollDown();
  309. }
  310. void CBattleInterface::newStack(CStack stack)
  311. {
  312. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  313. creAnims[stack.ID]->setType(2);
  314. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  315. }
  316. void CBattleInterface::stackRemoved(CStack stack)
  317. {
  318. delete creAnims[stack.ID];
  319. creAnims.erase(stack.ID);
  320. }
  321. void CBattleInterface::stackActivated(int number)
  322. {
  323. givenCommand = NULL;
  324. activeStack = number;
  325. }
  326. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  327. {
  328. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  329. int steps = creAnims[number]->framesInGroup(0);
  330. int hexWbase = 44, hexHbase = 42;
  331. if(startMoving) //animation of starting move
  332. {
  333. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  334. {
  335. show();
  336. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  337. }
  338. }
  339. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  340. {
  341. case 0:
  342. if(creDir[number] == true)
  343. reverseCreature(number, curStackPos);
  344. break;
  345. case 1:
  346. if(creDir[number] == false)
  347. reverseCreature(number, curStackPos);
  348. break;
  349. case 2:
  350. if(creDir[number] == false)
  351. reverseCreature(number, curStackPos);
  352. break;
  353. case 3:
  354. if(creDir[number] == false)
  355. reverseCreature(number, curStackPos);
  356. break;
  357. case 4:
  358. if(creDir[number] == true)
  359. reverseCreature(number, curStackPos);
  360. break;
  361. case 5:
  362. if(creDir[number] == true)
  363. reverseCreature(number, curStackPos);
  364. break;
  365. }
  366. //moving instructions
  367. creAnims[number]->setType(0);
  368. for(int i=0; i<steps; ++i)
  369. {
  370. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  371. {
  372. case 0:
  373. creAnims[number]->pos.x -= hexWbase/(2*steps);
  374. creAnims[number]->pos.y -= hexHbase/steps;
  375. break;
  376. case 1:
  377. creAnims[number]->pos.x += hexWbase/(2*steps);
  378. creAnims[number]->pos.y -= hexHbase/steps;
  379. break;
  380. case 2:
  381. creAnims[number]->pos.x += hexWbase/steps;
  382. break;
  383. case 3:
  384. creAnims[number]->pos.x += hexWbase/(2*steps);
  385. creAnims[number]->pos.y += hexHbase/steps;
  386. break;
  387. case 4:
  388. creAnims[number]->pos.x -= hexWbase/(2*steps);
  389. creAnims[number]->pos.y += hexHbase/steps;
  390. break;
  391. case 5:
  392. creAnims[number]->pos.x -= hexWbase/steps;
  393. break;
  394. }
  395. show();
  396. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  397. }
  398. if(endMoving) //animation of starting move
  399. {
  400. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  401. {
  402. show();
  403. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  404. }
  405. }
  406. creAnims[number]->setType(2); //resetting to default
  407. CStack curs = LOCPLINT->cb->battleGetStackByID(number);
  408. if(endMoving) //resetting to default
  409. {
  410. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  411. reverseCreature(number, destHex);
  412. //creDir[number] = (curs.owner == attackingHeroInstance->tempOwner);
  413. }
  414. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  415. creAnims[number]->pos.x = coords.first;
  416. creAnims[number]->pos.y = coords.second;
  417. }
  418. void CBattleInterface::stackAttacking(int ID, int dest)
  419. {
  420. }
  421. void CBattleInterface::newRound(int number)
  422. {
  423. console->addText(CGI->generaltexth->allTexts[412]);
  424. }
  425. void CBattleInterface::hexLclicked(int whichOne)
  426. {
  427. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  428. {
  429. BattleAction * ba = new BattleAction(); //to be deleted by engine
  430. ba->actionType = 6;
  431. ba->destinationTile = whichOne;
  432. ba->stackNumber = activeStack;
  433. givenCommand = ba;
  434. }
  435. }
  436. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  437. {
  438. std::vector<int> shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(ID);
  439. for(int i=0; i<shadedHexes.size(); ++i)
  440. {
  441. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  442. }
  443. }
  444. void CBattleHero::show(SDL_Surface *to)
  445. {
  446. //animation of flag
  447. if(flip)
  448. {
  449. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  450. }
  451. else
  452. {
  453. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  454. }
  455. {
  456. ++flagAnim;
  457. flagAnim %= flag->ourImages.size();
  458. }
  459. //animation of hero
  460. int tick=-1;
  461. for(int i=0; i<dh->ourImages.size(); ++i)
  462. {
  463. if(dh->ourImages[i].groupNumber==phase)
  464. ++tick;
  465. if(tick==image)
  466. {
  467. SDL_Rect posb = pos;
  468. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  469. ++image;
  470. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  471. {
  472. image = 0;
  473. }
  474. break;
  475. }
  476. }
  477. }
  478. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  479. {
  480. dh = CGI->spriteh->giveDef( defName );
  481. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  482. {
  483. if(flip)
  484. dh->ourImages[i].bitmap = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  485. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  486. }
  487. dh->alphaTransformed = true;
  488. if(flip)
  489. flag = CGI->spriteh->giveDef("CMFLAGR.DEF");
  490. else
  491. flag = CGI->spriteh->giveDef("CMFLAGL.DEF");
  492. //coloring flag and adding transparency
  493. for(int i=0; i<flag->ourImages.size(); ++i)
  494. {
  495. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  496. CSDL_Ext::blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  497. }
  498. }
  499. CBattleHero::~CBattleHero()
  500. {
  501. delete dh;
  502. delete flag;
  503. }
  504. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  505. {
  506. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  507. ret.second = -139 + 42 * (hexNum/17); //counting y
  508. //counting x
  509. if(attacker)
  510. {
  511. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  512. }
  513. else
  514. {
  515. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  516. }
  517. //shifting position for double - hex creatures
  518. if(creature->isDoubleWide())
  519. {
  520. if(attacker)
  521. {
  522. ret.first -= 42;
  523. }
  524. else
  525. {
  526. ret.first += 42;
  527. }
  528. }
  529. //returning
  530. return ret;
  531. }
  532. signed char CBattleHex::mutualPosition(int hex1, int hex2)
  533. {
  534. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  535. return 0;
  536. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  537. return 1;
  538. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  539. return 5;
  540. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  541. return 2;
  542. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  543. return 4;
  544. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  545. return 3;
  546. return -1;
  547. }
  548. void CBattleHex::activate()
  549. {
  550. Hoverable::activate();
  551. MotionInterested::activate();
  552. ClickableL::activate();
  553. }
  554. void CBattleHex::deactivate()
  555. {
  556. Hoverable::deactivate();
  557. MotionInterested::deactivate();
  558. ClickableL::deactivate();
  559. }
  560. void CBattleHex::hover(bool on)
  561. {
  562. hovered = on;
  563. Hoverable::hover(on);
  564. }
  565. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL)
  566. {
  567. }
  568. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  569. {
  570. if(CBattleInterface::cellShade)
  571. {
  572. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  573. {
  574. strictHovered = false;
  575. }
  576. else //hovered pixel is inside hex
  577. {
  578. strictHovered = true;
  579. }
  580. }
  581. }
  582. void CBattleHex::clickLeft(boost::logic::tribool down)
  583. {
  584. if(!down && hovered && strictHovered) //we've been really clicked!
  585. {
  586. myInterface->hexLclicked(myNumber);
  587. }
  588. }
  589. CBattleConsole::CBattleConsole() : lastShown(-1)
  590. {
  591. }
  592. CBattleConsole::~CBattleConsole()
  593. {
  594. texts.clear();
  595. }
  596. void CBattleConsole::show(SDL_Surface * to)
  597. {
  598. if(texts.size())
  599. {
  600. if(texts.size()==1)
  601. {
  602. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  603. }
  604. else
  605. {
  606. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  607. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  608. }
  609. }
  610. }
  611. bool CBattleConsole::addText(std::string text)
  612. {
  613. if(text.size()>70)
  614. return false; //text too long!
  615. texts.push_back(text);
  616. lastShown++;
  617. return true;
  618. }
  619. void CBattleConsole::eraseText(unsigned int pos)
  620. {
  621. if(pos < texts.size())
  622. {
  623. texts.erase(texts.begin() + pos);
  624. if(lastShown == texts.size())
  625. --lastShown;
  626. }
  627. }
  628. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  629. {
  630. if(pos >= texts.size()) //no such pos
  631. return;
  632. texts[pos] = text;
  633. }
  634. void CBattleConsole::scrollUp(unsigned int by)
  635. {
  636. if(lastShown > by)
  637. lastShown -= by;
  638. }
  639. void CBattleConsole::scrollDown(unsigned int by)
  640. {
  641. if(lastShown + by < texts.size())
  642. lastShown += by;
  643. }