CBattleInterface.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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 "client/CCreatureAnimation.h"
  13. #include <queue>
  14. #include <sstream>
  15. extern SDL_Surface * screen;
  16. extern TTF_Font * GEOR13;
  17. extern SDL_Color zwykly;
  18. SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
  19. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  20. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL), attackingInfo(NULL)
  21. {
  22. //initializing armies
  23. this->army1 = army1;
  24. this->army2 = army2;
  25. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  26. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  27. {
  28. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  29. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  30. creAnims[b->second.ID]->setType(2);
  31. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  32. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  33. }
  34. //preparing menu background and terrain
  35. std::vector< std::string > & backref = CGI->mh->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  36. background = CGI->bitmaph->loadBitmap(backref[ rand() % backref.size()] );
  37. menu = CGI->bitmaph->loadBitmap("CBAR.BMP");
  38. CSDL_Ext::blueToPlayersAdv(menu, hero1->tempOwner);
  39. //preparing graphics for displaying amounts of creatures
  40. amountBasic = CGI->bitmaph->loadBitmap("CMNUMWIN.BMP");
  41. amountNormal = CGI->bitmaph->loadBitmap("CMNUMWIN.BMP");
  42. CSDL_Ext::alphaTransform(amountNormal);
  43. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  44. {
  45. if((amountNormal->format->palette->colors+g)->b != 132 &&
  46. (amountNormal->format->palette->colors+g)->g != 231 &&
  47. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  48. {
  49. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  50. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  51. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  52. }
  53. }
  54. ////blitting menu background and terrain
  55. blitAt(background, 0, 0);
  56. blitAt(menu, 0, 556);
  57. CSDL_Ext::update();
  58. //preparing buttons and console
  59. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  60. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  61. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  62. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  63. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  64. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  65. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  66. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  67. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", false, NULL, false);
  68. bConsoleDown->bitmapOffset = 2;
  69. console = new CBattleConsole();
  70. console->pos.x = 211;
  71. console->pos.y = 560;
  72. console->pos.w = 406;
  73. console->pos.h = 38;
  74. //loading hero animations
  75. if(hero1) // attacking hero
  76. {
  77. attackingHero = new CBattleHero(CGI->mh->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  78. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  79. }
  80. else
  81. {
  82. attackingHero = NULL;
  83. }
  84. if(hero2) // defending hero
  85. {
  86. defendingHero = new CBattleHero(CGI->mh->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  87. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  88. }
  89. else
  90. {
  91. defendingHero = NULL;
  92. }
  93. //preparing cells and hexes
  94. cellBorder = CGI->bitmaph->loadBitmap("CCELLGRD.BMP");
  95. CSDL_Ext::alphaTransform(cellBorder);
  96. cellShade = CGI->bitmaph->loadBitmap("CCELLSHD.BMP");
  97. CSDL_Ext::alphaTransform(cellShade);
  98. for(int h=0; h<187; ++h)
  99. {
  100. bfield[h].myNumber = h;
  101. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  102. int y = 86 + 42 * (h/17);
  103. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  104. bfield[h].accesible = true;
  105. bfield[h].myInterface = this;
  106. }
  107. //locking occupied positions on batlefield
  108. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  109. {
  110. bfield[it->second.position].accesible = false;
  111. }
  112. }
  113. CBattleInterface::~CBattleInterface()
  114. {
  115. SDL_FreeSurface(background);
  116. SDL_FreeSurface(menu);
  117. SDL_FreeSurface(amountBasic);
  118. SDL_FreeSurface(amountNormal);
  119. delete bOptions;
  120. delete bSurrender;
  121. delete bFlee;
  122. delete bAutofight;
  123. delete bSpell;
  124. delete bWait;
  125. delete bDefence;
  126. delete bConsoleUp;
  127. delete bConsoleDown;
  128. delete console;
  129. delete attackingHero;
  130. delete defendingHero;
  131. SDL_FreeSurface(cellBorder);
  132. SDL_FreeSurface(cellShade);
  133. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  134. delete g->second;
  135. }
  136. void CBattleInterface::activate()
  137. {
  138. bOptions->activate();
  139. bSurrender->activate();
  140. bFlee->activate();
  141. bAutofight->activate();
  142. bSpell->activate();
  143. bWait->activate();
  144. bDefence->activate();
  145. bConsoleUp->activate();
  146. bConsoleDown->activate();
  147. for(int b=0; b<187; ++b)
  148. {
  149. bfield[b].activate();
  150. }
  151. }
  152. void CBattleInterface::deactivate()
  153. {
  154. bOptions->deactivate();
  155. bSurrender->deactivate();
  156. bFlee->deactivate();
  157. bAutofight->deactivate();
  158. bSpell->deactivate();
  159. bWait->deactivate();
  160. bDefence->deactivate();
  161. bConsoleUp->deactivate();
  162. bConsoleDown->deactivate();
  163. for(int b=0; b<187; ++b)
  164. {
  165. bfield[b].deactivate();
  166. }
  167. }
  168. void CBattleInterface::show(SDL_Surface * to)
  169. {
  170. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  171. ++animCount;
  172. if(!to) //"evaluating" to
  173. to = screen;
  174. //showing background
  175. blitAt(background, 0, 0, to);
  176. if(printCellBorders) //printing cell borders
  177. {
  178. for(int i=0; i<11; ++i) //rows
  179. {
  180. for(int j=0; j<15; ++j) //columns
  181. {
  182. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  183. int y = 86 + 42 * i;
  184. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorder, NULL, to, &genRect(cellBorder->h, cellBorder->w, x, y));
  185. }
  186. }
  187. }
  188. //printing hovered cell
  189. for(int b=0; b<187; ++b)
  190. {
  191. if(bfield[b].strictHovered && bfield[b].hovered)
  192. {
  193. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  194. int y = 86 + 42 * (b/17);
  195. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  196. }
  197. }
  198. //showing selected unit's range
  199. if(creAnims[activeStack]->getType() != 0) //don't show if unit is moving
  200. {
  201. showRange(to, activeStack);
  202. }
  203. //showing menu background and console
  204. blitAt(menu, 0, 556, to);
  205. console->show(to);
  206. //showing buttons
  207. bOptions->show(to);
  208. bSurrender->show(to);
  209. bFlee->show(to);
  210. bAutofight->show(to);
  211. bSpell->show(to);
  212. bWait->show(to);
  213. bDefence->show(to);
  214. bConsoleUp->show(to);
  215. bConsoleDown->show(to);
  216. //showing hero animations
  217. if(attackingHero)
  218. attackingHero->show(to);
  219. if(defendingHero)
  220. defendingHero->show(to);
  221. //showing units //a lot of work...
  222. int stackByHex[187];
  223. for(int b=0; b<187; ++b)
  224. stackByHex[b] = -1;
  225. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  226. {
  227. stackByHex[j->second.position] = j->second.ID;
  228. }
  229. attackingShowHelper(); // handle attack animation
  230. for(int b=0; b<187; ++b)
  231. {
  232. if(stackByHex[b]!=-1)
  233. {
  234. 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()!=2) && stacks[stackByHex[b]].alive, stackByHex[b]==activeStack); //increment always when moving, never if stack died
  235. //printing amount
  236. if(stacks[stackByHex[b]].amount > 0) //don't print if stack is not alive
  237. {
  238. if(stacks[stackByHex[b]].attackerOwned)
  239. {
  240. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackByHex[b]]->pos.x + 220, 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 + 220 + 14, creAnims[stackByHex[b]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  244. }
  245. else
  246. {
  247. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackByHex[b]]->pos.x + 202, creAnims[stackByHex[b]]->pos.y + 260));
  248. std::stringstream ss;
  249. ss<<stacks[stackByHex[b]].amount;
  250. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackByHex[b]]->pos.x + 202 + 14, creAnims[stackByHex[b]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  251. }
  252. }
  253. }
  254. }
  255. //units shown
  256. }
  257. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  258. {
  259. if(creAnims[number]==NULL)
  260. return false; //there is no such creature
  261. creAnims[number]->setType(8);
  262. for(int g=0; g<creAnims[number]->framesInGroup(8); ++g)
  263. {
  264. show();
  265. CSDL_Ext::update();
  266. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  267. }
  268. creDir[number] = !creDir[number];
  269. CStack curs = LOCPLINT->cb->battleGetStackByID(number);
  270. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  271. creAnims[number]->pos.x = coords.first;
  272. //creAnims[number]->pos.y = coords.second;
  273. if(wideTrick && curs.creature->isDoubleWide())
  274. {
  275. creAnims[number]->pos.x -= 44;
  276. }
  277. creAnims[number]->setType(7);
  278. for(int g=0; g<creAnims[number]->framesInGroup(7); ++g)
  279. {
  280. show();
  281. CSDL_Ext::update();
  282. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  283. }
  284. creAnims[number]->setType(2);
  285. return true;
  286. }
  287. void CBattleInterface::bOptionsf()
  288. {
  289. }
  290. void CBattleInterface::bSurrenderf()
  291. {
  292. }
  293. void CBattleInterface::bFleef()
  294. {
  295. BattleAction * ba = new BattleAction;
  296. ba->actionType = 4;
  297. givenCommand = ba;
  298. }
  299. void CBattleInterface::bAutofightf()
  300. {
  301. }
  302. void CBattleInterface::bSpellf()
  303. {
  304. }
  305. void CBattleInterface::bWaitf()
  306. {
  307. }
  308. void CBattleInterface::bDefencef()
  309. {
  310. BattleAction * ba = new BattleAction;
  311. ba->actionType = 3;
  312. ba->stackNumber = activeStack;
  313. givenCommand = ba;
  314. }
  315. void CBattleInterface::bConsoleUpf()
  316. {
  317. console->scrollUp();
  318. }
  319. void CBattleInterface::bConsoleDownf()
  320. {
  321. console->scrollDown();
  322. }
  323. void CBattleInterface::newStack(CStack stack)
  324. {
  325. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  326. creAnims[stack.ID]->setType(2);
  327. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  328. }
  329. void CBattleInterface::stackRemoved(CStack stack)
  330. {
  331. delete creAnims[stack.ID];
  332. creAnims.erase(stack.ID);
  333. }
  334. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby)
  335. {
  336. creAnims[ID]->setType(5); //death
  337. for(int i=0; i<creAnims[ID]->framesInGroup(5)-1; ++i)
  338. {
  339. show();
  340. CSDL_Ext::update();
  341. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  342. }
  343. printConsoleAttacked(ID, dmg, killed, IDby);
  344. }
  345. void CBattleInterface::stackActivated(int number)
  346. {
  347. givenCommand = NULL;
  348. activeStack = number;
  349. }
  350. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  351. {
  352. //a few useful variables
  353. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  354. int steps = creAnims[number]->framesInGroup(0);
  355. int hexWbase = 44, hexHbase = 42;
  356. if(startMoving) //animation of starting move
  357. {
  358. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  359. {
  360. show();
  361. CSDL_Ext::update();
  362. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  363. }
  364. }
  365. int mutPos = CBattleHex::mutualPosition(curStackPos, destHex);
  366. if(LOCPLINT->cb->battleGetCreature(number).isDoubleWide() &&
  367. ((creDir[number] && mutPos == 5) || (creDir[number] && mutPos == 0) || (creDir[number] && mutPos == 4))) //for special cases
  368. {
  369. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  370. {
  371. case 0:
  372. if(creDir[number] == true)
  373. reverseCreature(number, curStackPos, true);
  374. break;
  375. case 1:
  376. if(creDir[number] == false)
  377. reverseCreature(number, curStackPos, true);
  378. break;
  379. case 2:
  380. if(creDir[number] == false)
  381. reverseCreature(number, curStackPos, true);
  382. break;
  383. case 3:
  384. if(creDir[number] == false)
  385. reverseCreature(number, curStackPos, true);
  386. break;
  387. case 4:
  388. if(creDir[number] == true)
  389. reverseCreature(number, curStackPos, true);
  390. break;
  391. case 5:
  392. if(creDir[number] == true)
  393. reverseCreature(number, curStackPos, true);
  394. break;
  395. }
  396. //moving instructions
  397. creAnims[number]->setType(0);
  398. for(int i=0; i<steps; ++i)
  399. {
  400. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  401. {
  402. case 0:
  403. creAnims[number]->pos.x -= hexWbase/(2*steps);
  404. creAnims[number]->pos.y -= hexHbase/steps;
  405. break;
  406. case 1:
  407. creAnims[number]->pos.x += hexWbase/(2*steps);
  408. creAnims[number]->pos.y -= hexHbase/steps;
  409. break;
  410. case 2:
  411. creAnims[number]->pos.x += hexWbase/steps;
  412. break;
  413. case 3:
  414. creAnims[number]->pos.x += hexWbase/(2*steps);
  415. creAnims[number]->pos.y += hexHbase/steps;
  416. break;
  417. case 4:
  418. creAnims[number]->pos.x -= hexWbase/(2*steps);
  419. creAnims[number]->pos.y += hexHbase/steps;
  420. break;
  421. case 5:
  422. creAnims[number]->pos.x -= hexWbase/steps;
  423. break;
  424. }
  425. show();
  426. CSDL_Ext::update();
  427. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  428. }
  429. if( (LOCPLINT->cb->battleGetStackByID(number).owner == attackingHeroInstance->tempOwner ) != creDir[number])
  430. {
  431. reverseCreature(number, curStackPos, true);
  432. }
  433. }
  434. else //normal move instructions
  435. {
  436. switch(CBattleHex::mutualPosition(curStackPos, destHex)) //reverse unit if necessary
  437. {
  438. case 0:
  439. if(creDir[number] == true)
  440. reverseCreature(number, curStackPos);
  441. break;
  442. case 1:
  443. if(creDir[number] == false)
  444. reverseCreature(number, curStackPos);
  445. break;
  446. case 2:
  447. if(creDir[number] == false)
  448. reverseCreature(number, curStackPos);
  449. break;
  450. case 3:
  451. if(creDir[number] == false)
  452. reverseCreature(number, curStackPos);
  453. break;
  454. case 4:
  455. if(creDir[number] == true)
  456. reverseCreature(number, curStackPos);
  457. break;
  458. case 5:
  459. if(creDir[number] == true)
  460. reverseCreature(number, curStackPos);
  461. break;
  462. }
  463. //moving instructions
  464. creAnims[number]->setType(0);
  465. for(int i=0; i<steps; ++i)
  466. {
  467. switch(CBattleHex::mutualPosition(curStackPos, destHex))
  468. {
  469. case 0:
  470. creAnims[number]->pos.x -= hexWbase/(2*steps);
  471. creAnims[number]->pos.y -= hexHbase/steps;
  472. break;
  473. case 1:
  474. creAnims[number]->pos.x += hexWbase/(2*steps);
  475. creAnims[number]->pos.y -= hexHbase/steps;
  476. break;
  477. case 2:
  478. creAnims[number]->pos.x += hexWbase/steps;
  479. break;
  480. case 3:
  481. creAnims[number]->pos.x += hexWbase/(2*steps);
  482. creAnims[number]->pos.y += hexHbase/steps;
  483. break;
  484. case 4:
  485. creAnims[number]->pos.x -= hexWbase/(2*steps);
  486. creAnims[number]->pos.y += hexHbase/steps;
  487. break;
  488. case 5:
  489. creAnims[number]->pos.x -= hexWbase/steps;
  490. break;
  491. }
  492. show();
  493. CSDL_Ext::update();
  494. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  495. }
  496. }
  497. if(endMoving) //animation of ending move
  498. {
  499. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  500. {
  501. show();
  502. CSDL_Ext::update();
  503. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  504. }
  505. }
  506. creAnims[number]->setType(2); //resetting to default
  507. CStack curs = LOCPLINT->cb->battleGetStackByID(number);
  508. if(endMoving) //resetting to default
  509. {
  510. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  511. reverseCreature(number, destHex);
  512. //creDir[number] = (curs.owner == attackingHeroInstance->tempOwner);
  513. }
  514. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  515. creAnims[number]->pos.x = coords.first;
  516. creAnims[number]->pos.y = coords.second;
  517. }
  518. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby)
  519. {
  520. creAnims[ID]->setType(3); //getting hit
  521. for(int i=0; i<creAnims[ID]->framesInGroup(3); ++i)
  522. {
  523. show();
  524. CSDL_Ext::update();
  525. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  526. }
  527. creAnims[ID]->setType(2);
  528. printConsoleAttacked(ID, dmg, killed, IDby);
  529. }
  530. void CBattleInterface::stackAttacking(int ID, int dest)
  531. {
  532. CStack aStack = LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  533. if(aStack.creature->isDoubleWide())
  534. {
  535. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  536. {
  537. case 0:
  538. //reverseCreature(ID, aStack.position, true);
  539. break;
  540. case 1:
  541. break;
  542. case 2:
  543. break;
  544. case 3:
  545. break;
  546. case 4:
  547. //reverseCreature(ID, aStack.position, true);
  548. break;
  549. case 5:
  550. reverseCreature(ID, aStack.position, true);
  551. break;
  552. }
  553. }
  554. else //else for if(aStack.creature->isDoubleWide())
  555. {
  556. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  557. {
  558. case 0:
  559. reverseCreature(ID, aStack.position, true);
  560. break;
  561. case 1:
  562. break;
  563. case 2:
  564. break;
  565. case 3:
  566. break;
  567. case 4:
  568. reverseCreature(ID, aStack.position, true);
  569. break;
  570. case 5:
  571. reverseCreature(ID, aStack.position, true);
  572. break;
  573. }
  574. }
  575. attackingInfo = new CAttHelper;
  576. attackingInfo->dest = dest;
  577. attackingInfo->frame = 0;
  578. attackingInfo->ID = ID;
  579. attackingInfo->reversing = false;
  580. if(aStack.creature->isDoubleWide())
  581. {
  582. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  583. {
  584. case 0:
  585. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  586. break;
  587. case 1:
  588. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  589. break;
  590. case 2:
  591. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  592. break;
  593. case 3:
  594. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  595. break;
  596. case 4:
  597. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  598. break;
  599. case 5:
  600. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  601. break;
  602. }
  603. }
  604. else //else for if(aStack.creature->isDoubleWide())
  605. {
  606. switch(CBattleHex::mutualPosition(aStack.position, dest)) //attack direction
  607. {
  608. case 0:
  609. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  610. break;
  611. case 1:
  612. attackingInfo->maxframe = creAnims[ID]->framesInGroup(10);
  613. break;
  614. case 2:
  615. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  616. break;
  617. case 3:
  618. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  619. break;
  620. case 4:
  621. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  622. break;
  623. case 5:
  624. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  625. break;
  626. }
  627. }
  628. }
  629. void CBattleInterface::newRound(int number)
  630. {
  631. console->addText(CGI->generaltexth->allTexts[412]);
  632. }
  633. void CBattleInterface::hexLclicked(int whichOne)
  634. {
  635. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  636. {
  637. int atCre = LOCPLINT->cb->battleGetStack(whichOne); //creature at destination tile; -1 if there is no one
  638. //LOCPLINT->cb->battleGetCreature();
  639. if(atCre==-1) //normal move action
  640. {
  641. BattleAction * ba = new BattleAction(); //to be deleted by engine
  642. ba->actionType = 2;
  643. ba->destinationTile = whichOne;
  644. ba->stackNumber = activeStack;
  645. givenCommand = ba;
  646. }
  647. else if(LOCPLINT->cb->battleGetStackByID(atCre).owner != attackingHeroInstance->tempOwner) //attacking
  648. {
  649. BattleAction * ba = new BattleAction(); //to be deleted by engine
  650. ba->actionType = 6;
  651. ba->destinationTile = whichOne;
  652. ba->stackNumber = activeStack;
  653. givenCommand = ba;
  654. }
  655. }
  656. }
  657. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  658. {
  659. std::vector<int> shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(ID);
  660. for(int i=0; i<shadedHexes.size(); ++i)
  661. {
  662. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  663. }
  664. }
  665. void CBattleInterface::attackingShowHelper()
  666. {
  667. if(attackingInfo && !attackingInfo->reversing)
  668. {
  669. if(attackingInfo->frame == 0)
  670. {
  671. CStack aStack = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  672. if(aStack.creature->isDoubleWide())
  673. {
  674. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  675. {
  676. case 0:
  677. creAnims[attackingInfo->ID]->setType(10);
  678. break;
  679. case 1:
  680. creAnims[attackingInfo->ID]->setType(10);
  681. break;
  682. case 2:
  683. creAnims[attackingInfo->ID]->setType(11);
  684. break;
  685. case 3:
  686. creAnims[attackingInfo->ID]->setType(12);
  687. break;
  688. case 4:
  689. creAnims[attackingInfo->ID]->setType(12);
  690. break;
  691. case 5:
  692. creAnims[attackingInfo->ID]->setType(11);
  693. break;
  694. }
  695. }
  696. else //else for if(aStack.creature->isDoubleWide())
  697. {
  698. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  699. {
  700. case 0:
  701. creAnims[attackingInfo->ID]->setType(10);
  702. break;
  703. case 1:
  704. creAnims[attackingInfo->ID]->setType(10);
  705. break;
  706. case 2:
  707. creAnims[attackingInfo->ID]->setType(11);
  708. break;
  709. case 3:
  710. creAnims[attackingInfo->ID]->setType(12);
  711. break;
  712. case 4:
  713. creAnims[attackingInfo->ID]->setType(12);
  714. break;
  715. case 5:
  716. creAnims[attackingInfo->ID]->setType(11);
  717. break;
  718. }
  719. }
  720. }
  721. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  722. {
  723. attackingInfo->reversing = true;
  724. CStack aStack = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  725. if(aStack.creature->isDoubleWide())
  726. {
  727. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  728. {
  729. case 0:
  730. //reverseCreature(ID, aStack.position, true);
  731. break;
  732. case 1:
  733. break;
  734. case 2:
  735. break;
  736. case 3:
  737. break;
  738. case 4:
  739. //reverseCreature(ID, aStack.position, true);
  740. break;
  741. case 5:
  742. reverseCreature(attackingInfo->ID, aStack.position, true);
  743. break;
  744. }
  745. }
  746. else //else for if(aStack.creature->isDoubleWide())
  747. {
  748. switch(CBattleHex::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  749. {
  750. case 0:
  751. reverseCreature(attackingInfo->ID, aStack.position, true);
  752. break;
  753. case 1:
  754. break;
  755. case 2:
  756. break;
  757. case 3:
  758. break;
  759. case 4:
  760. reverseCreature(attackingInfo->ID, aStack.position, true);
  761. break;
  762. case 5:
  763. reverseCreature(attackingInfo->ID, aStack.position, true);
  764. break;
  765. }
  766. }
  767. attackingInfo->reversing = false;
  768. creAnims[attackingInfo->ID]->setType(2);
  769. delete attackingInfo;
  770. attackingInfo = NULL;
  771. }
  772. if(attackingInfo)
  773. {
  774. attackingInfo->frame++;
  775. }
  776. }
  777. }
  778. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  779. {
  780. char tabh[200];
  781. CStack attacker = LOCPLINT->cb->battleGetStackByID(IDby);
  782. CStack defender = LOCPLINT->cb->battleGetStackByID(ID);
  783. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  784. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  785. dmg);
  786. if(killed > 0)
  787. {
  788. if(killed > 1)
  789. {
  790. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  791. }
  792. else //killed == 1
  793. {
  794. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  795. }
  796. }
  797. console->addText(std::string(tabh));
  798. }
  799. void CBattleHero::show(SDL_Surface *to)
  800. {
  801. //animation of flag
  802. if(flip)
  803. {
  804. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  805. }
  806. else
  807. {
  808. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  809. }
  810. {
  811. ++flagAnim;
  812. flagAnim %= flag->ourImages.size();
  813. }
  814. //animation of hero
  815. int tick=-1;
  816. for(int i=0; i<dh->ourImages.size(); ++i)
  817. {
  818. if(dh->ourImages[i].groupNumber==phase)
  819. ++tick;
  820. if(tick==image)
  821. {
  822. SDL_Rect posb = pos;
  823. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  824. ++image;
  825. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  826. {
  827. image = 0;
  828. }
  829. break;
  830. }
  831. }
  832. }
  833. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  834. {
  835. dh = CGI->spriteh->giveDef( defName );
  836. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  837. {
  838. if(flip)
  839. {
  840. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  841. SDL_FreeSurface(dh->ourImages[i].bitmap);
  842. dh->ourImages[i].bitmap = hlp;
  843. }
  844. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  845. }
  846. dh->alphaTransformed = true;
  847. if(flip)
  848. flag = CGI->spriteh->giveDef("CMFLAGR.DEF");
  849. else
  850. flag = CGI->spriteh->giveDef("CMFLAGL.DEF");
  851. //coloring flag and adding transparency
  852. for(int i=0; i<flag->ourImages.size(); ++i)
  853. {
  854. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  855. CSDL_Ext::blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  856. }
  857. }
  858. CBattleHero::~CBattleHero()
  859. {
  860. delete dh;
  861. delete flag;
  862. }
  863. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  864. {
  865. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  866. ret.second = -139 + 42 * (hexNum/17); //counting y
  867. //counting x
  868. if(attacker)
  869. {
  870. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  871. }
  872. else
  873. {
  874. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  875. }
  876. //shifting position for double - hex creatures
  877. if(creature->isDoubleWide())
  878. {
  879. if(attacker)
  880. {
  881. ret.first -= 42;
  882. }
  883. else
  884. {
  885. ret.first += 42;
  886. }
  887. }
  888. //returning
  889. return ret;
  890. }
  891. signed char CBattleHex::mutualPosition(int hex1, int hex2)
  892. {
  893. if(hex2 == hex1 - ( (hex1/17)%2 ? 18 : 17 )) //top left
  894. return 0;
  895. if(hex2 == hex1 - ( (hex1/17)%2 ? 17 : 16 )) //top right
  896. return 1;
  897. if(hex2 == hex1 - 1 && hex1%17 != 0) //left
  898. return 5;
  899. if(hex2 == hex1 + 1 && hex1%17 != 16) //right
  900. return 2;
  901. if(hex2 == hex1 + ( (hex1/17)%2 ? 16 : 17 )) //bottom left
  902. return 4;
  903. if(hex2 == hex1 + ( (hex1/17)%2 ? 17 : 18 )) //bottom right
  904. return 3;
  905. return -1;
  906. }
  907. void CBattleHex::activate()
  908. {
  909. Hoverable::activate();
  910. MotionInterested::activate();
  911. ClickableL::activate();
  912. ClickableR::activate();
  913. }
  914. void CBattleHex::deactivate()
  915. {
  916. Hoverable::deactivate();
  917. MotionInterested::deactivate();
  918. ClickableL::deactivate();
  919. ClickableR::deactivate();
  920. }
  921. void CBattleHex::hover(bool on)
  922. {
  923. hovered = on;
  924. Hoverable::hover(on);
  925. if(!on && setAlterText)
  926. {
  927. myInterface->console->alterTxt = std::string();
  928. setAlterText = false;
  929. }
  930. }
  931. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  932. {
  933. }
  934. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  935. {
  936. if(CBattleInterface::cellShade)
  937. {
  938. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  939. {
  940. strictHovered = false;
  941. }
  942. else //hovered pixel is inside hex
  943. {
  944. strictHovered = true;
  945. }
  946. }
  947. if(hovered && strictHovered) //print attacked creature to console
  948. {
  949. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  950. LOCPLINT->cb->battleGetStackByPos(myNumber).owner != LOCPLINT->playerID &&
  951. LOCPLINT->cb->battleGetStackByPos(myNumber).alive)
  952. {
  953. char tabh[160];
  954. CStack attackedStack = LOCPLINT->cb->battleGetStackByPos(myNumber);
  955. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  956. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  957. myInterface->console->alterTxt = std::string(tabh);
  958. setAlterText = true;
  959. }
  960. }
  961. else if(setAlterText)
  962. {
  963. myInterface->console->alterTxt = std::string();
  964. setAlterText = false;
  965. }
  966. }
  967. void CBattleHex::clickLeft(boost::logic::tribool down)
  968. {
  969. if(!down && hovered && strictHovered) //we've been really clicked!
  970. {
  971. myInterface->hexLclicked(myNumber);
  972. }
  973. }
  974. void CBattleHex::clickRight(boost::logic::tribool down)
  975. {
  976. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  977. if(hovered && strictHovered && stID!=-1)
  978. {
  979. CStack myst = LOCPLINT->cb->battleGetStackByID(stID); //stack info
  980. StackState *pom = NULL;
  981. if(down)
  982. {
  983. pom = new StackState();
  984. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  985. if(h)
  986. {
  987. pom->attackBonus = h->primSkills[0];
  988. pom->defenseBonus = h->primSkills[1];
  989. pom->luck = h->getCurrentLuck();
  990. pom->morale = h->getCurrentMorale();
  991. }
  992. (new CCreInfoWindow(myst.creature->idNumber,0,pom,boost::function<void()>(),boost::function<void()>()))
  993. ->activate();
  994. }
  995. delete pom;
  996. }
  997. }
  998. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  999. {
  1000. }
  1001. CBattleConsole::~CBattleConsole()
  1002. {
  1003. texts.clear();
  1004. }
  1005. void CBattleConsole::show(SDL_Surface * to)
  1006. {
  1007. if(alterTxt.size())
  1008. {
  1009. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1010. }
  1011. else if(texts.size())
  1012. {
  1013. if(texts.size()==1)
  1014. {
  1015. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1016. }
  1017. else
  1018. {
  1019. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1020. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1021. }
  1022. }
  1023. }
  1024. bool CBattleConsole::addText(std::string text)
  1025. {
  1026. if(text.size()>70)
  1027. return false; //text too long!
  1028. int firstInToken = 0;
  1029. for(int i=0; i<text.size(); ++i) //tokenize
  1030. {
  1031. if(text[i] == 10)
  1032. {
  1033. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1034. firstInToken = i+1;
  1035. lastShown++;
  1036. }
  1037. }
  1038. texts.push_back( text.substr(firstInToken, text.size()) );
  1039. lastShown++;
  1040. return true;
  1041. }
  1042. void CBattleConsole::eraseText(unsigned int pos)
  1043. {
  1044. if(pos < texts.size())
  1045. {
  1046. texts.erase(texts.begin() + pos);
  1047. if(lastShown == texts.size())
  1048. --lastShown;
  1049. }
  1050. }
  1051. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1052. {
  1053. if(pos >= texts.size()) //no such pos
  1054. return;
  1055. texts[pos] = text;
  1056. }
  1057. void CBattleConsole::scrollUp(unsigned int by)
  1058. {
  1059. if(lastShown > by)
  1060. lastShown -= by;
  1061. }
  1062. void CBattleConsole::scrollDown(unsigned int by)
  1063. {
  1064. if(lastShown + by < texts.size())
  1065. lastShown += by;
  1066. }