CBattleInterface.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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. }
  249. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  250. {
  251. if(creAnims[number]==NULL)
  252. return false; //there is no such creature
  253. creAnims[number]->setType(8);
  254. for(int g=0; g<creAnims[number]->framesInGroup(8); ++g)
  255. {
  256. show();
  257. CSDL_Ext::update();
  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. CSDL_Ext::update();
  274. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  275. }
  276. creAnims[number]->setType(2);
  277. return true;
  278. }
  279. void CBattleInterface::bOptionsf()
  280. {
  281. }
  282. void CBattleInterface::bSurrenderf()
  283. {
  284. }
  285. void CBattleInterface::bFleef()
  286. {
  287. BattleAction * ba = new BattleAction;
  288. ba->actionType = 4;
  289. givenCommand = ba;
  290. }
  291. void CBattleInterface::bAutofightf()
  292. {
  293. }
  294. void CBattleInterface::bSpellf()
  295. {
  296. }
  297. void CBattleInterface::bWaitf()
  298. {
  299. }
  300. void CBattleInterface::bDefencef()
  301. {
  302. BattleAction * ba = new BattleAction;
  303. ba->actionType = 3;
  304. ba->stackNumber = activeStack;
  305. givenCommand = ba;
  306. }
  307. void CBattleInterface::bConsoleUpf()
  308. {
  309. console->scrollUp();
  310. }
  311. void CBattleInterface::bConsoleDownf()
  312. {
  313. console->scrollDown();
  314. }
  315. void CBattleInterface::newStack(CStack stack)
  316. {
  317. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  318. creAnims[stack.ID]->setType(2);
  319. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  320. }
  321. void CBattleInterface::stackRemoved(CStack stack)
  322. {
  323. delete creAnims[stack.ID];
  324. creAnims.erase(stack.ID);
  325. }
  326. void CBattleInterface::stackKilled(int ID)
  327. {
  328. }
  329. void CBattleInterface::stackActivated(int number)
  330. {
  331. givenCommand = NULL;
  332. activeStack = number;
  333. }
  334. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  335. {
  336. //a few useful variables
  337. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  338. int steps = creAnims[number]->framesInGroup(0);
  339. int hexWbase = 44, hexHbase = 42;
  340. if(startMoving) //animation of starting move
  341. {
  342. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  343. {
  344. show();
  345. CSDL_Ext::update();
  346. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  347. }
  348. }
  349. int mutPos = CBattleHex::mutualPosition(curStackPos, destHex);
  350. if(LOCPLINT->cb->battleGetCreature(number).isDoubleWide() &&
  351. ((creDir[number] && mutPos == 5) || (creDir[number] && mutPos == 0) || (creDir[number] && mutPos == 4))) //for special cases
  352. {
  353. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  354. {
  355. case 0:
  356. if(creDir[number] == true)
  357. reverseCreature(number, curStackPos, true);
  358. break;
  359. case 1:
  360. if(creDir[number] == false)
  361. reverseCreature(number, curStackPos, true);
  362. break;
  363. case 2:
  364. if(creDir[number] == false)
  365. reverseCreature(number, curStackPos, true);
  366. break;
  367. case 3:
  368. if(creDir[number] == false)
  369. reverseCreature(number, curStackPos, true);
  370. break;
  371. case 4:
  372. if(creDir[number] == true)
  373. reverseCreature(number, curStackPos, true);
  374. break;
  375. case 5:
  376. if(creDir[number] == true)
  377. reverseCreature(number, curStackPos, true);
  378. break;
  379. }
  380. //moving instructions
  381. creAnims[number]->setType(0);
  382. for(int i=0; i<steps; ++i)
  383. {
  384. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  385. {
  386. case 0:
  387. creAnims[number]->pos.x -= hexWbase/(2*steps);
  388. creAnims[number]->pos.y -= hexHbase/steps;
  389. break;
  390. case 1:
  391. creAnims[number]->pos.x += hexWbase/(2*steps);
  392. creAnims[number]->pos.y -= hexHbase/steps;
  393. break;
  394. case 2:
  395. creAnims[number]->pos.x += hexWbase/steps;
  396. break;
  397. case 3:
  398. creAnims[number]->pos.x += hexWbase/(2*steps);
  399. creAnims[number]->pos.y += hexHbase/steps;
  400. break;
  401. case 4:
  402. creAnims[number]->pos.x -= hexWbase/(2*steps);
  403. creAnims[number]->pos.y += hexHbase/steps;
  404. break;
  405. case 5:
  406. creAnims[number]->pos.x -= hexWbase/steps;
  407. break;
  408. }
  409. show();
  410. CSDL_Ext::update();
  411. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  412. }
  413. if( (LOCPLINT->cb->battleGetStackByID(number).owner == attackingHeroInstance->tempOwner ) != creDir[number])
  414. {
  415. reverseCreature(number, curStackPos, true);
  416. }
  417. }
  418. else //normal move instructions
  419. {
  420. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  421. {
  422. case 0:
  423. if(creDir[number] == true)
  424. reverseCreature(number, curStackPos);
  425. break;
  426. case 1:
  427. if(creDir[number] == false)
  428. reverseCreature(number, curStackPos);
  429. break;
  430. case 2:
  431. if(creDir[number] == false)
  432. reverseCreature(number, curStackPos);
  433. break;
  434. case 3:
  435. if(creDir[number] == false)
  436. reverseCreature(number, curStackPos);
  437. break;
  438. case 4:
  439. if(creDir[number] == true)
  440. reverseCreature(number, curStackPos);
  441. break;
  442. case 5:
  443. if(creDir[number] == true)
  444. reverseCreature(number, curStackPos);
  445. break;
  446. }
  447. //moving instructions
  448. creAnims[number]->setType(0);
  449. for(int i=0; i<steps; ++i)
  450. {
  451. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  452. {
  453. case 0:
  454. creAnims[number]->pos.x -= hexWbase/(2*steps);
  455. creAnims[number]->pos.y -= hexHbase/steps;
  456. break;
  457. case 1:
  458. creAnims[number]->pos.x += hexWbase/(2*steps);
  459. creAnims[number]->pos.y -= hexHbase/steps;
  460. break;
  461. case 2:
  462. creAnims[number]->pos.x += hexWbase/steps;
  463. break;
  464. case 3:
  465. creAnims[number]->pos.x += hexWbase/(2*steps);
  466. creAnims[number]->pos.y += hexHbase/steps;
  467. break;
  468. case 4:
  469. creAnims[number]->pos.x -= hexWbase/(2*steps);
  470. creAnims[number]->pos.y += hexHbase/steps;
  471. break;
  472. case 5:
  473. creAnims[number]->pos.x -= hexWbase/steps;
  474. break;
  475. }
  476. show();
  477. CSDL_Ext::update();
  478. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  479. }
  480. }
  481. if(endMoving) //animation of ending move
  482. {
  483. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  484. {
  485. show();
  486. CSDL_Ext::update();
  487. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  488. }
  489. }
  490. creAnims[number]->setType(2); //resetting to default
  491. CStack curs = LOCPLINT->cb->battleGetStackByID(number);
  492. if(endMoving) //resetting to default
  493. {
  494. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  495. reverseCreature(number, destHex);
  496. //creDir[number] = (curs.owner == attackingHeroInstance->tempOwner);
  497. }
  498. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  499. creAnims[number]->pos.x = coords.first;
  500. creAnims[number]->pos.y = coords.second;
  501. }
  502. void CBattleInterface::stackIsAttacked(int ID)
  503. {
  504. }
  505. void CBattleInterface::stackAttacking(int ID, int dest)
  506. {
  507. CStack aStack = LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  508. if(aStack.creature->isDoubleWide())
  509. {
  510. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  511. {
  512. case 0:
  513. //reverseCreature(ID, aStack.position, true);
  514. creAnims[ID]->setType(10);
  515. for(int i=0; i<creAnims[ID]->framesInGroup(10); ++i)
  516. {
  517. show();
  518. CSDL_Ext::update();
  519. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  520. }
  521. //reverseCreature(ID, aStack.position, true);
  522. break;
  523. case 1:
  524. creAnims[ID]->setType(10);
  525. for(int i=0; i<creAnims[ID]->framesInGroup(10); ++i)
  526. {
  527. show();
  528. CSDL_Ext::update();
  529. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  530. }
  531. break;
  532. case 2:
  533. creAnims[ID]->setType(11);
  534. for(int i=0; i<creAnims[ID]->framesInGroup(11); ++i)
  535. {
  536. show();
  537. CSDL_Ext::update();
  538. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  539. }
  540. break;
  541. case 3:
  542. creAnims[ID]->setType(12);
  543. for(int i=0; i<creAnims[ID]->framesInGroup(12); ++i)
  544. {
  545. show();
  546. CSDL_Ext::update();
  547. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  548. }
  549. break;
  550. case 4:
  551. //reverseCreature(ID, aStack.position, true);
  552. creAnims[ID]->setType(12);
  553. for(int i=0; i<creAnims[ID]->framesInGroup(12); ++i)
  554. {
  555. show();
  556. CSDL_Ext::update();
  557. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  558. }
  559. //reverseCreature(ID, aStack.position, true);
  560. break;
  561. case 5:
  562. reverseCreature(ID, aStack.position, true);
  563. creAnims[ID]->setType(11);
  564. for(int i=0; i<creAnims[ID]->framesInGroup(11); ++i)
  565. {
  566. show();
  567. CSDL_Ext::update();
  568. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  569. }
  570. reverseCreature(ID, aStack.position, true);
  571. break;
  572. }
  573. creAnims[ID]->setType(2);
  574. }
  575. else //else for if(aStack.creature->isDoubleWide())
  576. {
  577. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  578. {
  579. case 0:
  580. reverseCreature(ID, aStack.position, true);
  581. creAnims[ID]->setType(10);
  582. for(int i=0; i<creAnims[ID]->framesInGroup(10); ++i)
  583. {
  584. show();
  585. CSDL_Ext::update();
  586. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  587. }
  588. reverseCreature(ID, aStack.position, true);
  589. break;
  590. case 1:
  591. creAnims[ID]->setType(10);
  592. for(int i=0; i<creAnims[ID]->framesInGroup(10); ++i)
  593. {
  594. show();
  595. CSDL_Ext::update();
  596. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  597. }
  598. break;
  599. case 2:
  600. creAnims[ID]->setType(11);
  601. for(int i=0; i<creAnims[ID]->framesInGroup(11); ++i)
  602. {
  603. show();
  604. CSDL_Ext::update();
  605. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  606. }
  607. break;
  608. case 3:
  609. creAnims[ID]->setType(12);
  610. for(int i=0; i<creAnims[ID]->framesInGroup(12); ++i)
  611. {
  612. show();
  613. CSDL_Ext::update();
  614. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  615. }
  616. break;
  617. case 4:
  618. reverseCreature(ID, aStack.position, true);
  619. creAnims[ID]->setType(12);
  620. for(int i=0; i<creAnims[ID]->framesInGroup(12); ++i)
  621. {
  622. show();
  623. CSDL_Ext::update();
  624. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  625. }
  626. reverseCreature(ID, aStack.position, true);
  627. break;
  628. case 5:
  629. reverseCreature(ID, aStack.position, true);
  630. creAnims[ID]->setType(11);
  631. for(int i=0; i<creAnims[ID]->framesInGroup(11); ++i)
  632. {
  633. show();
  634. CSDL_Ext::update();
  635. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  636. }
  637. reverseCreature(ID, aStack.position, true);
  638. break;
  639. }
  640. creAnims[ID]->setType(2);
  641. }
  642. }
  643. void CBattleInterface::newRound(int number)
  644. {
  645. console->addText(CGI->generaltexth->allTexts[412]);
  646. }
  647. void CBattleInterface::hexLclicked(int whichOne)
  648. {
  649. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  650. {
  651. int atCre = LOCPLINT->cb->battleGetStack(whichOne); //creature at destination tile; -1 if there is no one
  652. //LOCPLINT->cb->battleGetCreature();
  653. if(atCre==-1) //normal move action
  654. {
  655. BattleAction * ba = new BattleAction(); //to be deleted by engine
  656. ba->actionType = 2;
  657. ba->destinationTile = whichOne;
  658. ba->stackNumber = activeStack;
  659. givenCommand = ba;
  660. }
  661. else if(LOCPLINT->cb->battleGetStackByID(atCre).owner != attackingHeroInstance->tempOwner) //attacking
  662. {
  663. BattleAction * ba = new BattleAction(); //to be deleted by engine
  664. ba->actionType = 6;
  665. ba->destinationTile = whichOne;
  666. ba->stackNumber = activeStack;
  667. givenCommand = ba;
  668. }
  669. }
  670. }
  671. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  672. {
  673. std::vector<int> shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(ID);
  674. for(int i=0; i<shadedHexes.size(); ++i)
  675. {
  676. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  677. }
  678. }
  679. void CBattleHero::show(SDL_Surface *to)
  680. {
  681. //animation of flag
  682. if(flip)
  683. {
  684. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  685. }
  686. else
  687. {
  688. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  689. }
  690. {
  691. ++flagAnim;
  692. flagAnim %= flag->ourImages.size();
  693. }
  694. //animation of hero
  695. int tick=-1;
  696. for(int i=0; i<dh->ourImages.size(); ++i)
  697. {
  698. if(dh->ourImages[i].groupNumber==phase)
  699. ++tick;
  700. if(tick==image)
  701. {
  702. SDL_Rect posb = pos;
  703. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  704. ++image;
  705. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  706. {
  707. image = 0;
  708. }
  709. break;
  710. }
  711. }
  712. }
  713. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  714. {
  715. dh = CGI->spriteh->giveDef( defName );
  716. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  717. {
  718. if(flip)
  719. {
  720. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  721. SDL_FreeSurface(dh->ourImages[i].bitmap);
  722. dh->ourImages[i].bitmap = hlp;
  723. }
  724. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  725. }
  726. dh->alphaTransformed = true;
  727. if(flip)
  728. flag = CGI->spriteh->giveDef("CMFLAGR.DEF");
  729. else
  730. flag = CGI->spriteh->giveDef("CMFLAGL.DEF");
  731. //coloring flag and adding transparency
  732. for(int i=0; i<flag->ourImages.size(); ++i)
  733. {
  734. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  735. CSDL_Ext::blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  736. }
  737. }
  738. CBattleHero::~CBattleHero()
  739. {
  740. delete dh;
  741. delete flag;
  742. }
  743. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  744. {
  745. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  746. ret.second = -139 + 42 * (hexNum/17); //counting y
  747. //counting x
  748. if(attacker)
  749. {
  750. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  751. }
  752. else
  753. {
  754. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  755. }
  756. //shifting position for double - hex creatures
  757. if(creature->isDoubleWide())
  758. {
  759. if(attacker)
  760. {
  761. ret.first -= 42;
  762. }
  763. else
  764. {
  765. ret.first += 42;
  766. }
  767. }
  768. //returning
  769. return ret;
  770. }
  771. signed char CBattleHex::mutualPosition(int hex1, int hex2)
  772. {
  773. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  774. return 0;
  775. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  776. return 1;
  777. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  778. return 5;
  779. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  780. return 2;
  781. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  782. return 4;
  783. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  784. return 3;
  785. return -1;
  786. }
  787. void CBattleHex::activate()
  788. {
  789. Hoverable::activate();
  790. MotionInterested::activate();
  791. ClickableL::activate();
  792. ClickableR::activate();
  793. }
  794. void CBattleHex::deactivate()
  795. {
  796. Hoverable::deactivate();
  797. MotionInterested::deactivate();
  798. ClickableL::deactivate();
  799. ClickableR::deactivate();
  800. }
  801. void CBattleHex::hover(bool on)
  802. {
  803. hovered = on;
  804. Hoverable::hover(on);
  805. }
  806. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL)
  807. {
  808. }
  809. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  810. {
  811. if(CBattleInterface::cellShade)
  812. {
  813. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  814. {
  815. strictHovered = false;
  816. }
  817. else //hovered pixel is inside hex
  818. {
  819. strictHovered = true;
  820. }
  821. }
  822. }
  823. void CBattleHex::clickLeft(boost::logic::tribool down)
  824. {
  825. if(!down && hovered && strictHovered) //we've been really clicked!
  826. {
  827. myInterface->hexLclicked(myNumber);
  828. }
  829. }
  830. void CBattleHex::clickRight(boost::logic::tribool down)
  831. {
  832. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  833. if(hovered && strictHovered && stID!=-1)
  834. {
  835. CStack myst = LOCPLINT->cb->battleGetStackByID(stID); //stack info
  836. StackState *pom = NULL;
  837. if(down)
  838. {
  839. pom = new StackState();
  840. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  841. pom->attackBonus = h->primSkills[0];
  842. pom->defenseBonus = h->primSkills[1];
  843. pom->luck = h->getCurrentLuck();
  844. pom->morale = h->getCurrentMorale();
  845. (new CCreInfoWindow(myst.creature->idNumber,0,pom,boost::function<void()>(),boost::function<void()>()))
  846. ->activate();
  847. }
  848. delete pom;
  849. }
  850. }
  851. CBattleConsole::CBattleConsole() : lastShown(-1)
  852. {
  853. }
  854. CBattleConsole::~CBattleConsole()
  855. {
  856. texts.clear();
  857. }
  858. void CBattleConsole::show(SDL_Surface * to)
  859. {
  860. if(texts.size())
  861. {
  862. if(texts.size()==1)
  863. {
  864. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  865. }
  866. else
  867. {
  868. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  869. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  870. }
  871. }
  872. }
  873. bool CBattleConsole::addText(std::string text)
  874. {
  875. if(text.size()>70)
  876. return false; //text too long!
  877. texts.push_back(text);
  878. lastShown++;
  879. return true;
  880. }
  881. void CBattleConsole::eraseText(unsigned int pos)
  882. {
  883. if(pos < texts.size())
  884. {
  885. texts.erase(texts.begin() + pos);
  886. if(lastShown == texts.size())
  887. --lastShown;
  888. }
  889. }
  890. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  891. {
  892. if(pos >= texts.size()) //no such pos
  893. return;
  894. texts[pos] = text;
  895. }
  896. void CBattleConsole::scrollUp(unsigned int by)
  897. {
  898. if(lastShown > by)
  899. lastShown -= by;
  900. }
  901. void CBattleConsole::scrollDown(unsigned int by)
  902. {
  903. if(lastShown + by < texts.size())
  904. lastShown += by;
  905. }