CBattleInterface.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  59. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  60. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  61. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  62. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  63. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  64. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  65. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  66. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", 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. CSDL_Ext::alphaTransform(cellBorder);
  95. cellShade = CGI->bitmaph->loadBitmap("CCELLSHD.BMP");
  96. 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(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  133. delete g->second;
  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, bool wideTrick)
  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. if(wideTrick && curs.creature->isDoubleWide())
  266. {
  267. creAnims[number]->pos.x -= 44;
  268. }
  269. creAnims[number]->setType(7);
  270. for(int g=0; g<creAnims[number]->framesInGroup(7); ++g)
  271. {
  272. show();
  273. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  274. }
  275. creAnims[number]->setType(2);
  276. return true;
  277. }
  278. void CBattleInterface::bOptionsf()
  279. {
  280. }
  281. void CBattleInterface::bSurrenderf()
  282. {
  283. }
  284. void CBattleInterface::bFleef()
  285. {
  286. BattleAction * ba = new BattleAction;
  287. ba->actionType = 4;
  288. givenCommand = ba;
  289. }
  290. void CBattleInterface::bAutofightf()
  291. {
  292. }
  293. void CBattleInterface::bSpellf()
  294. {
  295. }
  296. void CBattleInterface::bWaitf()
  297. {
  298. }
  299. void CBattleInterface::bDefencef()
  300. {
  301. BattleAction * ba = new BattleAction;
  302. ba->actionType = 3;
  303. ba->stackNumber = activeStack;
  304. givenCommand = ba;
  305. }
  306. void CBattleInterface::bConsoleUpf()
  307. {
  308. console->scrollUp();
  309. }
  310. void CBattleInterface::bConsoleDownf()
  311. {
  312. console->scrollDown();
  313. }
  314. void CBattleInterface::newStack(CStack stack)
  315. {
  316. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  317. creAnims[stack.ID]->setType(2);
  318. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  319. }
  320. void CBattleInterface::stackRemoved(CStack stack)
  321. {
  322. delete creAnims[stack.ID];
  323. creAnims.erase(stack.ID);
  324. }
  325. void CBattleInterface::stackActivated(int number)
  326. {
  327. givenCommand = NULL;
  328. activeStack = number;
  329. }
  330. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  331. {
  332. //a few useful variables
  333. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  334. int steps = creAnims[number]->framesInGroup(0);
  335. int hexWbase = 44, hexHbase = 42;
  336. if(startMoving) //animation of starting move
  337. {
  338. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  339. {
  340. show();
  341. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  342. }
  343. }
  344. int mutPos = CBattleHex::mutualPosition(curStackPos, destHex);
  345. if(LOCPLINT->cb->battleGetCreature(number).isDoubleWide() &&
  346. ((creDir[number] && mutPos == 5) || (creDir[number] && mutPos == 0) || (creDir[number] && mutPos == 4))) //for special cases
  347. {
  348. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  349. {
  350. case 0:
  351. if(creDir[number] == true)
  352. reverseCreature(number, curStackPos, true);
  353. break;
  354. case 1:
  355. if(creDir[number] == false)
  356. reverseCreature(number, curStackPos, true);
  357. break;
  358. case 2:
  359. if(creDir[number] == false)
  360. reverseCreature(number, curStackPos, true);
  361. break;
  362. case 3:
  363. if(creDir[number] == false)
  364. reverseCreature(number, curStackPos, true);
  365. break;
  366. case 4:
  367. if(creDir[number] == true)
  368. reverseCreature(number, curStackPos, true);
  369. break;
  370. case 5:
  371. if(creDir[number] == true)
  372. reverseCreature(number, curStackPos, true);
  373. break;
  374. }
  375. //moving instructions
  376. creAnims[number]->setType(0);
  377. for(int i=0; i<steps; ++i)
  378. {
  379. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  380. {
  381. case 0:
  382. creAnims[number]->pos.x -= hexWbase/(2*steps);
  383. creAnims[number]->pos.y -= hexHbase/steps;
  384. break;
  385. case 1:
  386. creAnims[number]->pos.x += hexWbase/(2*steps);
  387. creAnims[number]->pos.y -= hexHbase/steps;
  388. break;
  389. case 2:
  390. creAnims[number]->pos.x += hexWbase/steps;
  391. break;
  392. case 3:
  393. creAnims[number]->pos.x += hexWbase/(2*steps);
  394. creAnims[number]->pos.y += hexHbase/steps;
  395. break;
  396. case 4:
  397. creAnims[number]->pos.x -= hexWbase/(2*steps);
  398. creAnims[number]->pos.y += hexHbase/steps;
  399. break;
  400. case 5:
  401. creAnims[number]->pos.x -= hexWbase/steps;
  402. break;
  403. }
  404. show();
  405. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  406. }
  407. if( (LOCPLINT->cb->battleGetStackByID(number).owner == attackingHeroInstance->tempOwner ) != creDir[number])
  408. {
  409. reverseCreature(number, curStackPos, true);
  410. }
  411. }
  412. else //normal move instructions
  413. {
  414. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  415. {
  416. case 0:
  417. if(creDir[number] == true)
  418. reverseCreature(number, curStackPos);
  419. break;
  420. case 1:
  421. if(creDir[number] == false)
  422. reverseCreature(number, curStackPos);
  423. break;
  424. case 2:
  425. if(creDir[number] == false)
  426. reverseCreature(number, curStackPos);
  427. break;
  428. case 3:
  429. if(creDir[number] == false)
  430. reverseCreature(number, curStackPos);
  431. break;
  432. case 4:
  433. if(creDir[number] == true)
  434. reverseCreature(number, curStackPos);
  435. break;
  436. case 5:
  437. if(creDir[number] == true)
  438. reverseCreature(number, curStackPos);
  439. break;
  440. }
  441. //moving instructions
  442. creAnims[number]->setType(0);
  443. for(int i=0; i<steps; ++i)
  444. {
  445. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  446. {
  447. case 0:
  448. creAnims[number]->pos.x -= hexWbase/(2*steps);
  449. creAnims[number]->pos.y -= hexHbase/steps;
  450. break;
  451. case 1:
  452. creAnims[number]->pos.x += hexWbase/(2*steps);
  453. creAnims[number]->pos.y -= hexHbase/steps;
  454. break;
  455. case 2:
  456. creAnims[number]->pos.x += hexWbase/steps;
  457. break;
  458. case 3:
  459. creAnims[number]->pos.x += hexWbase/(2*steps);
  460. creAnims[number]->pos.y += hexHbase/steps;
  461. break;
  462. case 4:
  463. creAnims[number]->pos.x -= hexWbase/(2*steps);
  464. creAnims[number]->pos.y += hexHbase/steps;
  465. break;
  466. case 5:
  467. creAnims[number]->pos.x -= hexWbase/steps;
  468. break;
  469. }
  470. show();
  471. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  472. }
  473. }
  474. if(endMoving) //animation of ending move
  475. {
  476. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  477. {
  478. show();
  479. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  480. }
  481. }
  482. creAnims[number]->setType(2); //resetting to default
  483. CStack curs = LOCPLINT->cb->battleGetStackByID(number);
  484. if(endMoving) //resetting to default
  485. {
  486. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  487. reverseCreature(number, destHex);
  488. //creDir[number] = (curs.owner == attackingHeroInstance->tempOwner);
  489. }
  490. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  491. creAnims[number]->pos.x = coords.first;
  492. creAnims[number]->pos.y = coords.second;
  493. }
  494. void CBattleInterface::stackAttacking(int ID, int dest)
  495. {
  496. }
  497. void CBattleInterface::newRound(int number)
  498. {
  499. console->addText(CGI->generaltexth->allTexts[412]);
  500. }
  501. void CBattleInterface::hexLclicked(int whichOne)
  502. {
  503. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  504. {
  505. BattleAction * ba = new BattleAction(); //to be deleted by engine
  506. ba->actionType = 6;
  507. ba->destinationTile = whichOne;
  508. ba->stackNumber = activeStack;
  509. givenCommand = ba;
  510. }
  511. }
  512. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  513. {
  514. std::vector<int> shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(ID);
  515. for(int i=0; i<shadedHexes.size(); ++i)
  516. {
  517. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  518. }
  519. }
  520. void CBattleHero::show(SDL_Surface *to)
  521. {
  522. //animation of flag
  523. if(flip)
  524. {
  525. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  526. }
  527. else
  528. {
  529. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  530. }
  531. {
  532. ++flagAnim;
  533. flagAnim %= flag->ourImages.size();
  534. }
  535. //animation of hero
  536. int tick=-1;
  537. for(int i=0; i<dh->ourImages.size(); ++i)
  538. {
  539. if(dh->ourImages[i].groupNumber==phase)
  540. ++tick;
  541. if(tick==image)
  542. {
  543. SDL_Rect posb = pos;
  544. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  545. ++image;
  546. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  547. {
  548. image = 0;
  549. }
  550. break;
  551. }
  552. }
  553. }
  554. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  555. {
  556. dh = CGI->spriteh->giveDef( defName );
  557. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  558. {
  559. if(flip)
  560. {
  561. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  562. SDL_FreeSurface(dh->ourImages[i].bitmap);
  563. dh->ourImages[i].bitmap = hlp;
  564. }
  565. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  566. }
  567. dh->alphaTransformed = true;
  568. if(flip)
  569. flag = CGI->spriteh->giveDef("CMFLAGR.DEF");
  570. else
  571. flag = CGI->spriteh->giveDef("CMFLAGL.DEF");
  572. //coloring flag and adding transparency
  573. for(int i=0; i<flag->ourImages.size(); ++i)
  574. {
  575. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  576. CSDL_Ext::blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  577. }
  578. }
  579. CBattleHero::~CBattleHero()
  580. {
  581. delete dh;
  582. delete flag;
  583. }
  584. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  585. {
  586. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  587. ret.second = -139 + 42 * (hexNum/17); //counting y
  588. //counting x
  589. if(attacker)
  590. {
  591. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  592. }
  593. else
  594. {
  595. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  596. }
  597. //shifting position for double - hex creatures
  598. if(creature->isDoubleWide())
  599. {
  600. if(attacker)
  601. {
  602. ret.first -= 42;
  603. }
  604. else
  605. {
  606. ret.first += 42;
  607. }
  608. }
  609. //returning
  610. return ret;
  611. }
  612. signed char CBattleHex::mutualPosition(int hex1, int hex2)
  613. {
  614. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  615. return 0;
  616. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  617. return 1;
  618. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  619. return 5;
  620. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  621. return 2;
  622. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  623. return 4;
  624. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  625. return 3;
  626. return -1;
  627. }
  628. void CBattleHex::activate()
  629. {
  630. Hoverable::activate();
  631. MotionInterested::activate();
  632. ClickableL::activate();
  633. }
  634. void CBattleHex::deactivate()
  635. {
  636. Hoverable::deactivate();
  637. MotionInterested::deactivate();
  638. ClickableL::deactivate();
  639. }
  640. void CBattleHex::hover(bool on)
  641. {
  642. hovered = on;
  643. Hoverable::hover(on);
  644. }
  645. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL)
  646. {
  647. }
  648. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  649. {
  650. if(CBattleInterface::cellShade)
  651. {
  652. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  653. {
  654. strictHovered = false;
  655. }
  656. else //hovered pixel is inside hex
  657. {
  658. strictHovered = true;
  659. }
  660. }
  661. }
  662. void CBattleHex::clickLeft(boost::logic::tribool down)
  663. {
  664. if(!down && hovered && strictHovered) //we've been really clicked!
  665. {
  666. myInterface->hexLclicked(myNumber);
  667. }
  668. }
  669. CBattleConsole::CBattleConsole() : lastShown(-1)
  670. {
  671. }
  672. CBattleConsole::~CBattleConsole()
  673. {
  674. texts.clear();
  675. }
  676. void CBattleConsole::show(SDL_Surface * to)
  677. {
  678. if(texts.size())
  679. {
  680. if(texts.size()==1)
  681. {
  682. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  683. }
  684. else
  685. {
  686. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  687. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  688. }
  689. }
  690. }
  691. bool CBattleConsole::addText(std::string text)
  692. {
  693. if(text.size()>70)
  694. return false; //text too long!
  695. texts.push_back(text);
  696. lastShown++;
  697. return true;
  698. }
  699. void CBattleConsole::eraseText(unsigned int pos)
  700. {
  701. if(pos < texts.size())
  702. {
  703. texts.erase(texts.begin() + pos);
  704. if(lastShown == texts.size())
  705. --lastShown;
  706. }
  707. }
  708. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  709. {
  710. if(pos >= texts.size()) //no such pos
  711. return;
  712. texts[pos] = text;
  713. }
  714. void CBattleConsole::scrollUp(unsigned int by)
  715. {
  716. if(lastShown > by)
  717. lastShown -= by;
  718. }
  719. void CBattleConsole::scrollDown(unsigned int by)
  720. {
  721. if(lastShown + by < texts.size())
  722. lastShown += by;
  723. }