CBattleInterface.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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/CObjectHandler.h"
  8. #include "hch/CHeroHandler.h"
  9. #include "hch/CDefHandler.h"
  10. #include "CCursorHandler.h"
  11. #include "CCallback.h"
  12. #include "CGameState.h"
  13. #include "hch/CGeneralTextHandler.h"
  14. #include "client/CCreatureAnimation.h"
  15. #include "client/Graphics.h"
  16. #include <queue>
  17. #include <sstream>
  18. #include "lib/CondSh.h"
  19. #include "lib/NetPacks.h"
  20. #ifndef __GNUC__
  21. const double M_PI = 3.14159265358979323846;
  22. #else
  23. #define _USE_MATH_DEFINES
  24. #include <cmath>
  25. #endif
  26. extern SDL_Surface * screen;
  27. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  28. extern SDL_Color zwykly;
  29. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  30. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL), attackingInfo(NULL), myTurn(false), resWindow(NULL)
  31. {
  32. givenCommand = new CondSh<BattleAction *>(NULL);
  33. //initializing armies
  34. this->army1 = army1;
  35. this->army2 = army2;
  36. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  37. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  38. {
  39. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  40. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  41. creAnims[b->second.ID]->setType(2);
  42. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  43. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  44. }
  45. //preparing menu background and terrain
  46. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  47. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  48. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  49. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  50. //preparing graphics for displaying amounts of creatures
  51. amountBasic = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  52. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  53. CSDL_Ext::alphaTransform(amountNormal);
  54. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  55. {
  56. if((amountNormal->format->palette->colors+g)->b != 132 &&
  57. (amountNormal->format->palette->colors+g)->g != 231 &&
  58. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  59. {
  60. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  61. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  62. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  63. }
  64. }
  65. ////blitting menu background and terrain
  66. blitAt(background, 0, 0);
  67. blitAt(menu, 0, 556);
  68. CSDL_Ext::update();
  69. //preparing buttons and console
  70. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  71. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  72. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  73. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  74. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  75. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  76. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  77. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  78. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", false, NULL, false);
  79. bConsoleDown->bitmapOffset = 2;
  80. console = new CBattleConsole();
  81. console->pos.x = 211;
  82. console->pos.y = 560;
  83. console->pos.w = 406;
  84. console->pos.h = 38;
  85. //loading hero animations
  86. if(hero1) // attacking hero
  87. {
  88. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  89. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  90. }
  91. else
  92. {
  93. attackingHero = NULL;
  94. }
  95. if(hero2) // defending hero
  96. {
  97. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  98. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  99. }
  100. else
  101. {
  102. defendingHero = NULL;
  103. }
  104. //preparing cells and hexes
  105. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  106. CSDL_Ext::alphaTransform(cellBorder);
  107. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  108. CSDL_Ext::alphaTransform(cellShade);
  109. for(int h=0; h<187; ++h)
  110. {
  111. bfield[h].myNumber = h;
  112. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  113. int y = 86 + 42 * (h/17);
  114. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  115. bfield[h].accesible = true;
  116. bfield[h].myInterface = this;
  117. }
  118. //locking occupied positions on batlefield
  119. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  120. {
  121. bfield[it->second.position].accesible = false;
  122. }
  123. //loading projectiles for units
  124. for(std::map<int, CStack>::iterator g = stacks.begin(); g != stacks.end(); ++g)
  125. {
  126. if(g->second.creature->isShooting() && CGI->creh->idToProjectile[g->second.creature->idNumber] != std::string())
  127. {
  128. idToProjectile[g->second.creature->idNumber] = CDefHandler::giveDef(CGI->creh->idToProjectile[g->second.creature->idNumber]);
  129. if(idToProjectile[g->second.creature->idNumber]->ourImages.size() > 2) //add symmetric images
  130. {
  131. for(int k = idToProjectile[g->second.creature->idNumber]->ourImages.size()-2; k > 1; --k)
  132. {
  133. Cimage ci;
  134. ci.bitmap = CSDL_Ext::rotate01(idToProjectile[g->second.creature->idNumber]->ourImages[k].bitmap);
  135. ci.groupNumber = 0;
  136. ci.imName = std::string();
  137. idToProjectile[g->second.creature->idNumber]->ourImages.push_back(ci);
  138. }
  139. }
  140. for(int s=0; s<idToProjectile[g->second.creature->idNumber]->ourImages.size(); ++s) //alpha transforming
  141. {
  142. CSDL_Ext::alphaTransform(idToProjectile[g->second.creature->idNumber]->ourImages[s].bitmap);
  143. }
  144. }
  145. }
  146. //prepairing graphic with cell borders
  147. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  148. //copying palette
  149. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  150. {
  151. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  152. }
  153. //palette copied
  154. for(int i=0; i<11; ++i) //rows
  155. {
  156. for(int j=0; j<15; ++j) //columns
  157. {
  158. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  159. int y = 86 + 42 * i;
  160. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  161. {
  162. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  163. {
  164. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  165. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  166. }
  167. }
  168. }
  169. }
  170. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  171. }
  172. CBattleInterface::~CBattleInterface()
  173. {
  174. SDL_FreeSurface(background);
  175. SDL_FreeSurface(menu);
  176. SDL_FreeSurface(amountBasic);
  177. SDL_FreeSurface(amountNormal);
  178. SDL_FreeSurface(cellBorders);
  179. SDL_FreeSurface(backgroundWithHexes);
  180. delete bOptions;
  181. delete bSurrender;
  182. delete bFlee;
  183. delete bAutofight;
  184. delete bSpell;
  185. delete bWait;
  186. delete bDefence;
  187. delete bConsoleUp;
  188. delete bConsoleDown;
  189. delete console;
  190. delete resWindow;
  191. delete givenCommand;
  192. delete attackingHero;
  193. delete defendingHero;
  194. SDL_FreeSurface(cellBorder);
  195. SDL_FreeSurface(cellShade);
  196. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  197. delete g->second;
  198. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  199. delete g->second;
  200. }
  201. void CBattleInterface::activate()
  202. {
  203. subInt = NULL;
  204. bOptions->activate();
  205. bSurrender->activate();
  206. bFlee->activate();
  207. bAutofight->activate();
  208. bSpell->activate();
  209. bWait->activate();
  210. bDefence->activate();
  211. bConsoleUp->activate();
  212. bConsoleDown->activate();
  213. for(int b=0; b<187; ++b)
  214. {
  215. bfield[b].activate();
  216. }
  217. }
  218. void CBattleInterface::deactivate()
  219. {
  220. bOptions->deactivate();
  221. bSurrender->deactivate();
  222. bFlee->deactivate();
  223. bAutofight->deactivate();
  224. bSpell->deactivate();
  225. bWait->deactivate();
  226. bDefence->deactivate();
  227. bConsoleUp->deactivate();
  228. bConsoleDown->deactivate();
  229. for(int b=0; b<187; ++b)
  230. {
  231. bfield[b].deactivate();
  232. }
  233. }
  234. void CBattleInterface::show(SDL_Surface * to)
  235. {
  236. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  237. ++animCount;
  238. if(!to) //"evaluating" to
  239. to = screen;
  240. //printing background and hexes
  241. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //show everything with range
  242. {
  243. blitAt(backgroundWithHexes, 0, 0, to);
  244. }
  245. else
  246. {
  247. //showing background
  248. blitAt(background, 0, 0, to);
  249. if(printCellBorders)
  250. {
  251. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, NULL);
  252. }
  253. }
  254. //printing hovered cell
  255. for(int b=0; b<187; ++b)
  256. {
  257. if(bfield[b].strictHovered && bfield[b].hovered)
  258. {
  259. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  260. int y = 86 + 42 * (b/17);
  261. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  262. }
  263. }
  264. //showing menu background and console
  265. blitAt(menu, 0, 556, to);
  266. console->show(to);
  267. //showing buttons
  268. bOptions->show(to);
  269. bSurrender->show(to);
  270. bFlee->show(to);
  271. bAutofight->show(to);
  272. bSpell->show(to);
  273. bWait->show(to);
  274. bDefence->show(to);
  275. bConsoleUp->show(to);
  276. bConsoleDown->show(to);
  277. //showing hero animations
  278. if(attackingHero)
  279. attackingHero->show(to);
  280. if(defendingHero)
  281. defendingHero->show(to);
  282. ////showing units //a lot of work...
  283. std::vector<int> stackAliveByHex[187];
  284. //double loop because dead stacks should be printed first
  285. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  286. {
  287. if(j->second.alive)
  288. stackAliveByHex[j->second.position].push_back(j->second.ID);
  289. }
  290. std::vector<int> stackDeadByHex[187];
  291. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  292. {
  293. if(!j->second.alive)
  294. stackDeadByHex[j->second.position].push_back(j->second.ID);
  295. }
  296. attackingShowHelper(); // handle attack animation
  297. for(int b=0; b<187; ++b) //showing dead stacks
  298. {
  299. for(int v=0; v<stackDeadByHex[b].size(); ++v)
  300. {
  301. creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x, creAnims[stackDeadByHex[b][v]]->pos.y, creDir[stackDeadByHex[b][v]], (animCount%4==0 || creAnims[stackDeadByHex[b][v]]->getType()!=2) && stacks[stackDeadByHex[b][v]].alive, stackDeadByHex[b][v]==activeStack); //increment always when moving, never if stack died
  302. //printing amount
  303. if(stacks[stackDeadByHex[b][v]].amount > 0) //don't print if stack is not alive
  304. {
  305. int xAdd = stacks[stackDeadByHex[b][v]].attackerOwned ? 220 : 202;
  306. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackDeadByHex[b][v]]->pos.x + xAdd, creAnims[stackDeadByHex[b][v]]->pos.y + 260));
  307. std::stringstream ss;
  308. ss<<stacks[stackDeadByHex[b][v]].amount;
  309. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackDeadByHex[b][v]]->pos.x + xAdd + 14, creAnims[stackDeadByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  310. }
  311. }
  312. }
  313. for(int b=0; b<187; ++b) //showing alive stacks
  314. {
  315. for(int v=0; v<stackAliveByHex[b].size(); ++v)
  316. {
  317. creAnims[stackAliveByHex[b][v]]->nextFrame(to, creAnims[stackAliveByHex[b][v]]->pos.x, creAnims[stackAliveByHex[b][v]]->pos.y, creDir[stackAliveByHex[b][v]], (animCount%4==0) && stacks[stackAliveByHex[b][v]].alive, stackAliveByHex[b][v]==activeStack); //increment always when moving, never if stack died
  318. //printing amount
  319. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  320. {
  321. int xAdd = stacks[stackAliveByHex[b][v]].attackerOwned ? 220 : 202;
  322. CSDL_Ext::blit8bppAlphaTo24bpp(amountNormal, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackAliveByHex[b][v]]->pos.x + xAdd, creAnims[stackAliveByHex[b][v]]->pos.y + 260));
  323. std::stringstream ss;
  324. ss<<stacks[stackAliveByHex[b][v]].amount;
  325. CSDL_Ext::printAtMiddleWB(ss.str(), creAnims[stackAliveByHex[b][v]]->pos.x + xAdd + 14, creAnims[stackAliveByHex[b][v]]->pos.y + 260 + 4, GEOR13, 20, zwykly, to);
  326. }
  327. }
  328. }
  329. //units shown
  330. projectileShowHelper(to);//showing projectiles
  331. //showing window with result of battle
  332. if(resWindow)
  333. {
  334. resWindow->show(to);
  335. }
  336. }
  337. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  338. {
  339. if(creAnims[number]==NULL)
  340. return false; //there is no such creature
  341. creAnims[number]->setType(8);
  342. for(int g=0; g<creAnims[number]->framesInGroup(8); ++g)
  343. {
  344. show();
  345. CSDL_Ext::update();
  346. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  347. if((animCount+1)%4)
  348. creAnims[number]->incrementFrame();
  349. }
  350. creDir[number] = !creDir[number];
  351. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  352. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  353. creAnims[number]->pos.x = coords.first;
  354. //creAnims[number]->pos.y = coords.second;
  355. if(wideTrick && curs.creature->isDoubleWide())
  356. {
  357. if(!creDir[number])
  358. creAnims[number]->pos.x -= 44;
  359. }
  360. creAnims[number]->setType(7);
  361. for(int g=0; g<creAnims[number]->framesInGroup(7); ++g)
  362. {
  363. show();
  364. CSDL_Ext::update();
  365. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  366. }
  367. creAnims[number]->setType(2);
  368. return true;
  369. }
  370. void CBattleInterface::bOptionsf()
  371. {
  372. }
  373. void CBattleInterface::bSurrenderf()
  374. {
  375. }
  376. void CBattleInterface::bFleef()
  377. {
  378. giveCommand(4,0,0);
  379. CGI->curh->changeGraphic(0, 0);
  380. }
  381. void CBattleInterface::bAutofightf()
  382. {
  383. }
  384. void CBattleInterface::bSpellf()
  385. {
  386. }
  387. void CBattleInterface::bWaitf()
  388. {
  389. }
  390. void CBattleInterface::bDefencef()
  391. {
  392. giveCommand(3,0,activeStack);
  393. }
  394. void CBattleInterface::bConsoleUpf()
  395. {
  396. console->scrollUp();
  397. }
  398. void CBattleInterface::bConsoleDownf()
  399. {
  400. console->scrollDown();
  401. }
  402. void CBattleInterface::newStack(CStack stack)
  403. {
  404. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  405. creAnims[stack.ID]->setType(2);
  406. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  407. }
  408. void CBattleInterface::stackRemoved(CStack stack)
  409. {
  410. delete creAnims[stack.ID];
  411. creAnims.erase(stack.ID);
  412. }
  413. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  414. {
  415. if(creAnims[ID]->getType() != 2)
  416. {
  417. return; //something went wrong
  418. }
  419. if(byShooting) //delay hit animation
  420. {
  421. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  422. while(true)
  423. {
  424. bool found = false;
  425. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  426. {
  427. if(it->creID == attacker.creature->idNumber)
  428. {
  429. found = true;
  430. break;
  431. }
  432. }
  433. if(!found)
  434. break;
  435. else
  436. {
  437. show();
  438. CSDL_Ext::update();
  439. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  440. }
  441. }
  442. }
  443. creAnims[ID]->setType(5); //death
  444. for(int i=0; i<creAnims[ID]->framesInGroup(5); ++i)
  445. {
  446. if(i)
  447. creAnims[ID]->incrementFrame();
  448. show();
  449. CSDL_Ext::update();
  450. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  451. }
  452. printConsoleAttacked(ID, dmg, killed, IDby);
  453. }
  454. void CBattleInterface::stackActivated(int number)
  455. {
  456. //givenCommand = NULL;
  457. activeStack = number;
  458. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(number);
  459. myTurn = true;
  460. //preparating background graphic with hexes and shaded hexes
  461. blitAt(background, 0, 0, backgroundWithHexes);
  462. if(printCellBorders)
  463. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  464. for(int m=0; m<shadedHexes.size(); ++m) //rows
  465. {
  466. int i = shadedHexes[m]/17; //row
  467. int j = shadedHexes[m]%17-1; //column
  468. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  469. int y = 86 + 42 * i;
  470. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  471. }
  472. }
  473. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  474. {
  475. //a few useful variables
  476. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  477. int steps = creAnims[number]->framesInGroup(0);
  478. int hexWbase = 44, hexHbase = 42;
  479. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  480. if(startMoving) //animation of starting move
  481. {
  482. deactivate();
  483. CGI->curh->hide();
  484. creAnims[number]->setType(20);
  485. //LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  486. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  487. {
  488. show();
  489. CSDL_Ext::update();
  490. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  491. if((animCount+1)%4)
  492. creAnims[number]->incrementFrame();
  493. }
  494. }
  495. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  496. {
  497. switch(mutPos) //reverse unit if necessary
  498. {
  499. case 0: case 4: case 5:
  500. if(creDir[number] == true)
  501. reverseCreature(number, curStackPos, twoTiles);
  502. break;
  503. case 1: case 2: case 3:
  504. if(creDir[number] == false)
  505. reverseCreature(number, curStackPos, twoTiles);
  506. break;
  507. }
  508. //moving instructions
  509. creAnims[number]->setType(0);
  510. for(int i=0; i<steps; ++i)
  511. {
  512. switch(mutPos)
  513. {
  514. case 0:
  515. creAnims[number]->pos.x -= hexWbase/(2*steps);
  516. creAnims[number]->pos.y -= hexHbase/steps;
  517. break;
  518. case 1:
  519. creAnims[number]->pos.x += hexWbase/(2*steps);
  520. creAnims[number]->pos.y -= hexHbase/steps;
  521. break;
  522. case 2:
  523. creAnims[number]->pos.x += hexWbase/steps;
  524. break;
  525. case 3:
  526. creAnims[number]->pos.x += hexWbase/(2*steps);
  527. creAnims[number]->pos.y += hexHbase/steps;
  528. break;
  529. case 4:
  530. creAnims[number]->pos.x -= hexWbase/(2*steps);
  531. creAnims[number]->pos.y += hexHbase/steps;
  532. break;
  533. case 5:
  534. creAnims[number]->pos.x -= hexWbase/steps;
  535. break;
  536. }
  537. show();
  538. CSDL_Ext::update();
  539. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  540. if((animCount+1)%4)
  541. creAnims[number]->incrementFrame();
  542. }
  543. }
  544. if(endMoving) //animation of ending move
  545. {
  546. creAnims[number]->setType(21);
  547. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  548. {
  549. show();
  550. CSDL_Ext::update();
  551. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  552. if((animCount+1)%4)
  553. creAnims[number]->incrementFrame();
  554. }
  555. creAnims[number]->setType(2); //resetting to default
  556. activate();
  557. CGI->curh->show();
  558. }
  559. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  560. if(endMoving) //resetting to default
  561. {
  562. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  563. reverseCreature(number, destHex, twoTiles);
  564. }
  565. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  566. creAnims[number]->pos.x = coords.first;
  567. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  568. creAnims[number]->pos.x -= 44;
  569. creAnims[number]->pos.y = coords.second;
  570. }
  571. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  572. {
  573. if(creAnims[ID]->getType() != 2)
  574. {
  575. return; //something went wrong
  576. }
  577. if(byShooting) //delay hit animation
  578. {
  579. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  580. while(true)
  581. {
  582. bool found = false;
  583. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  584. {
  585. if(it->creID == attacker.creature->idNumber)
  586. {
  587. found = true;
  588. break;
  589. }
  590. }
  591. if(!found)
  592. break;
  593. else
  594. {
  595. show();
  596. CSDL_Ext::update();
  597. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  598. }
  599. }
  600. }
  601. creAnims[ID]->setType(3); //getting hit
  602. for(int i=0; i<creAnims[ID]->framesInGroup(3); ++i)
  603. {
  604. show();
  605. CSDL_Ext::update();
  606. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  607. if((animCount+1)%4)
  608. creAnims[ID]->incrementFrame();
  609. }
  610. creAnims[ID]->setType(2);
  611. printConsoleAttacked(ID, dmg, killed, IDby);
  612. }
  613. void CBattleInterface::stackAttacking(int ID, int dest)
  614. {
  615. if(attackingInfo != NULL)
  616. {
  617. return; //something went wrong
  618. }
  619. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  620. if(aStack.creature->isDoubleWide())
  621. {
  622. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  623. {
  624. case 0:
  625. //reverseCreature(ID, aStack.position, true);
  626. break;
  627. case 1:
  628. break;
  629. case 2:
  630. break;
  631. case 3:
  632. break;
  633. case 4:
  634. //reverseCreature(ID, aStack.position, true);
  635. break;
  636. case 5:
  637. reverseCreature(ID, aStack.position, true);
  638. break;
  639. }
  640. }
  641. else //else for if(aStack.creature->isDoubleWide())
  642. {
  643. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  644. {
  645. case 0:
  646. reverseCreature(ID, aStack.position, true);
  647. break;
  648. case 1:
  649. break;
  650. case 2:
  651. break;
  652. case 3:
  653. break;
  654. case 4:
  655. reverseCreature(ID, aStack.position, true);
  656. break;
  657. case 5:
  658. reverseCreature(ID, aStack.position, true);
  659. break;
  660. }
  661. }
  662. attackingInfo = new CAttHelper;
  663. attackingInfo->dest = dest;
  664. attackingInfo->frame = 0;
  665. attackingInfo->ID = ID;
  666. attackingInfo->reversing = false;
  667. attackingInfo->shooting = false;
  668. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  669. {
  670. case 0:
  671. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  672. break;
  673. case 1:
  674. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  675. break;
  676. case 2:
  677. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  678. break;
  679. case 3:
  680. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  681. break;
  682. case 4:
  683. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  684. break;
  685. case 5:
  686. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  687. break;
  688. }
  689. }
  690. void CBattleInterface::newRound(int number)
  691. {
  692. console->addText(CGI->generaltexth->allTexts[412]);
  693. }
  694. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  695. {
  696. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  697. ba->actionType = action;
  698. ba->destinationTile = tile;
  699. ba->stackNumber = stack;
  700. ba->additionalInfo = additional;
  701. givenCommand->setn(ba);
  702. myTurn = false;
  703. activeStack = -1;
  704. }
  705. void CBattleInterface::hexLclicked(int whichOne)
  706. {
  707. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  708. {
  709. if(!myTurn)
  710. return; //we are not permit to do anything
  711. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  712. if(!dest || !dest->alive) //no creature at that tile
  713. {
  714. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  715. giveCommand(2,whichOne,activeStack);
  716. }
  717. else if(dest->owner != attackingHeroInstance->tempOwner
  718. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)) //shooting
  719. {
  720. giveCommand(7,whichOne,activeStack);
  721. }
  722. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  723. {
  724. std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  725. for(int i=0;i<n.size();i++)
  726. {
  727. //TODO: now we are using first available tile, but in the future we should add possibility of choosing from which tile we want to attack
  728. if(vstd::contains(shadedHexes,n[i]))
  729. {
  730. giveCommand(6,n[i],activeStack,whichOne);
  731. return;
  732. }
  733. }
  734. }
  735. }
  736. }
  737. void CBattleInterface::stackIsShooting(int ID, int dest)
  738. {
  739. if(attackingInfo != NULL)
  740. {
  741. return; //something went wrong
  742. }
  743. //projectile
  744. float projectileAngle; //in radians; if positive, projectiles goes up
  745. float straightAngle = 0.2f; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  746. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  747. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  748. if(fromHex < dest)
  749. projectileAngle = -projectileAngle;
  750. SProjectileInfo spi;
  751. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  752. spi.step = 0;
  753. spi.frameNum = 0;
  754. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  755. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  756. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  757. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  758. if(projectileAngle > straightAngle) //upper shot
  759. {
  760. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  761. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  762. }
  763. else if(projectileAngle < -straightAngle) //lower shot
  764. {
  765. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  766. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  767. }
  768. else //straight shot
  769. {
  770. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  771. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  772. }
  773. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  774. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  775. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  776. //set starting frame
  777. if(spi.spin)
  778. {
  779. spi.frameNum = 0;
  780. }
  781. else
  782. {
  783. spi.frameNum = ((M_PI/2.0f - projectileAngle) / (2.0f *M_PI) + 1/((float)(2*(idToProjectile[spi.creID]->ourImages.size()-1)))) * (idToProjectile[spi.creID]->ourImages.size()-1);
  784. }
  785. //set delay
  786. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  787. projectiles.push_back(spi);
  788. //attack aniamtion
  789. attackingInfo = new CAttHelper;
  790. attackingInfo->dest = dest;
  791. attackingInfo->frame = 0;
  792. attackingInfo->ID = ID;
  793. attackingInfo->reversing = false;
  794. attackingInfo->shooting = true;
  795. if(projectileAngle > straightAngle) //upper shot
  796. attackingInfo->shootingGroup = 14;
  797. else if(projectileAngle < -straightAngle) //lower shot
  798. attackingInfo->shootingGroup = 15;
  799. else //straight shot
  800. attackingInfo->shootingGroup = 16;
  801. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  802. }
  803. void CBattleInterface::battleFinished(const BattleResult& br)
  804. {
  805. deactivate();
  806. resWindow = new CBattleReslutWindow(br, genRect(561, 470, 165, 19), this);
  807. resWindow->activate();
  808. }
  809. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  810. {
  811. /*for(int i=0; i<shadedHexes.size(); ++i)
  812. {
  813. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  814. }*/
  815. //CSDL_Ext::blit8bppAlphaTo24bpp(shadedHexesGraphic, NULL, to, NULL);
  816. }
  817. void CBattleInterface::attackingShowHelper()
  818. {
  819. if(attackingInfo && !attackingInfo->reversing)
  820. {
  821. if(attackingInfo->frame == 0)
  822. {
  823. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  824. if(attackingInfo->shooting)
  825. {
  826. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  827. }
  828. else
  829. {
  830. if(aStack.creature->isDoubleWide())
  831. {
  832. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  833. {
  834. case 0:
  835. creAnims[attackingInfo->ID]->setType(10);
  836. break;
  837. case 1:
  838. creAnims[attackingInfo->ID]->setType(10);
  839. break;
  840. case 2:
  841. creAnims[attackingInfo->ID]->setType(11);
  842. break;
  843. case 3:
  844. creAnims[attackingInfo->ID]->setType(12);
  845. break;
  846. case 4:
  847. creAnims[attackingInfo->ID]->setType(12);
  848. break;
  849. case 5:
  850. creAnims[attackingInfo->ID]->setType(11);
  851. break;
  852. }
  853. }
  854. else //else for if(aStack.creature->isDoubleWide())
  855. {
  856. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  857. {
  858. case 0:
  859. creAnims[attackingInfo->ID]->setType(10);
  860. break;
  861. case 1:
  862. creAnims[attackingInfo->ID]->setType(10);
  863. break;
  864. case 2:
  865. creAnims[attackingInfo->ID]->setType(11);
  866. break;
  867. case 3:
  868. creAnims[attackingInfo->ID]->setType(12);
  869. break;
  870. case 4:
  871. creAnims[attackingInfo->ID]->setType(12);
  872. break;
  873. case 5:
  874. creAnims[attackingInfo->ID]->setType(11);
  875. break;
  876. }
  877. }
  878. }
  879. }
  880. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  881. {
  882. attackingInfo->reversing = true;
  883. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  884. if(aStack.creature->isDoubleWide())
  885. {
  886. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  887. {
  888. case 0:
  889. //reverseCreature(ID, aStack.position, true);
  890. break;
  891. case 1:
  892. break;
  893. case 2:
  894. break;
  895. case 3:
  896. break;
  897. case 4:
  898. //reverseCreature(ID, aStack.position, true);
  899. break;
  900. case 5:
  901. reverseCreature(attackingInfo->ID, aStack.position, true);
  902. break;
  903. }
  904. }
  905. else //else for if(aStack.creature->isDoubleWide())
  906. {
  907. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  908. {
  909. case 0:
  910. reverseCreature(attackingInfo->ID, aStack.position, true);
  911. break;
  912. case 1:
  913. break;
  914. case 2:
  915. break;
  916. case 3:
  917. break;
  918. case 4:
  919. reverseCreature(attackingInfo->ID, aStack.position, true);
  920. break;
  921. case 5:
  922. reverseCreature(attackingInfo->ID, aStack.position, true);
  923. break;
  924. }
  925. }
  926. attackingInfo->reversing = false;
  927. creAnims[attackingInfo->ID]->setType(2);
  928. delete attackingInfo;
  929. attackingInfo = NULL;
  930. }
  931. if(attackingInfo)
  932. {
  933. attackingInfo->frame++;
  934. }
  935. }
  936. }
  937. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  938. {
  939. char tabh[200];
  940. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  941. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  942. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  943. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  944. dmg);
  945. if(killed > 0)
  946. {
  947. if(killed > 1)
  948. {
  949. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  950. }
  951. else //killed == 1
  952. {
  953. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  954. }
  955. }
  956. console->addText(std::string(tabh));
  957. }
  958. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  959. {
  960. if(to == NULL)
  961. to = screen;
  962. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  963. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  964. {
  965. if(it->animStartDelay>0)
  966. {
  967. --(it->animStartDelay);
  968. continue;
  969. }
  970. SDL_Rect dst;
  971. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  972. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  973. dst.x = it->x;
  974. dst.y = it->y;
  975. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  976. //actualizing projectile
  977. ++it->step;
  978. if(it->step == it->lastStep)
  979. {
  980. toBeDeleted.insert(toBeDeleted.end(), it);
  981. }
  982. else
  983. {
  984. it->x += it->dx;
  985. it->y += it->dy;
  986. if(it->spin)
  987. {
  988. ++(it->frameNum);
  989. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  990. }
  991. }
  992. }
  993. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  994. {
  995. projectiles.erase(*it);
  996. }
  997. }
  998. void CBattleHero::show(SDL_Surface *to)
  999. {
  1000. //animation of flag
  1001. if(flip)
  1002. {
  1003. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1004. }
  1005. else
  1006. {
  1007. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1008. }
  1009. {
  1010. ++flagAnim;
  1011. flagAnim %= flag->ourImages.size();
  1012. }
  1013. //animation of hero
  1014. int tick=-1;
  1015. for(int i=0; i<dh->ourImages.size(); ++i)
  1016. {
  1017. if(dh->ourImages[i].groupNumber==phase)
  1018. ++tick;
  1019. if(tick==image)
  1020. {
  1021. SDL_Rect posb = pos;
  1022. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1023. ++image;
  1024. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1025. {
  1026. image = 0;
  1027. }
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  1033. {
  1034. dh = CDefHandler::giveDef( defName );
  1035. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1036. {
  1037. if(flip)
  1038. {
  1039. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1040. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1041. dh->ourImages[i].bitmap = hlp;
  1042. }
  1043. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1044. }
  1045. dh->alphaTransformed = true;
  1046. if(flip)
  1047. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1048. else
  1049. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1050. //coloring flag and adding transparency
  1051. for(int i=0; i<flag->ourImages.size(); ++i)
  1052. {
  1053. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1054. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1055. }
  1056. }
  1057. CBattleHero::~CBattleHero()
  1058. {
  1059. delete dh;
  1060. delete flag;
  1061. }
  1062. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  1063. {
  1064. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1065. ret.second = -139 + 42 * (hexNum/17); //counting y
  1066. //counting x
  1067. if(attacker)
  1068. {
  1069. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1070. }
  1071. else
  1072. {
  1073. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1074. }
  1075. //shifting position for double - hex creatures
  1076. if(creature->isDoubleWide())
  1077. {
  1078. if(attacker)
  1079. {
  1080. ret.first -= 42;
  1081. }
  1082. else
  1083. {
  1084. ret.first += 42;
  1085. }
  1086. }
  1087. //returning
  1088. return ret;
  1089. }
  1090. void CBattleHex::activate()
  1091. {
  1092. Hoverable::activate();
  1093. MotionInterested::activate();
  1094. ClickableL::activate();
  1095. ClickableR::activate();
  1096. }
  1097. void CBattleHex::deactivate()
  1098. {
  1099. Hoverable::deactivate();
  1100. MotionInterested::deactivate();
  1101. ClickableL::deactivate();
  1102. ClickableR::deactivate();
  1103. }
  1104. void CBattleHex::hover(bool on)
  1105. {
  1106. hovered = on;
  1107. Hoverable::hover(on);
  1108. if(!on && setAlterText)
  1109. {
  1110. myInterface->console->alterTxt = std::string();
  1111. setAlterText = false;
  1112. }
  1113. }
  1114. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1115. {
  1116. }
  1117. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  1118. {
  1119. if(myInterface->cellShade)
  1120. {
  1121. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1122. {
  1123. strictHovered = false;
  1124. }
  1125. else //hovered pixel is inside hex
  1126. {
  1127. strictHovered = true;
  1128. if(myInterface->activeStack>=0)
  1129. {
  1130. if(std::find(myInterface->shadedHexes.begin(),myInterface->shadedHexes.end(),myNumber) == myInterface->shadedHexes.end())
  1131. {
  1132. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  1133. if(shere)
  1134. {
  1135. if(shere->owner == LOCPLINT->playerID) //our stack
  1136. CGI->curh->changeGraphic(1,5);
  1137. else if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isShooting()) //we can shoot enemy
  1138. CGI->curh->changeGraphic(1,3);
  1139. else //unavailable enemy
  1140. CGI->curh->changeGraphic(1,0);
  1141. }
  1142. else //empty unavailable tile
  1143. CGI->curh->changeGraphic(1,0);
  1144. }
  1145. else //available tile
  1146. {
  1147. if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isFlying())
  1148. CGI->curh->changeGraphic(1,2);
  1149. else
  1150. CGI->curh->changeGraphic(1,1);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. if(hovered && strictHovered) //print attacked creature to console
  1156. {
  1157. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1158. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1159. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive)
  1160. {
  1161. char tabh[160];
  1162. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1163. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1164. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1165. myInterface->console->alterTxt = std::string(tabh);
  1166. setAlterText = true;
  1167. }
  1168. }
  1169. else if(setAlterText)
  1170. {
  1171. myInterface->console->alterTxt = std::string();
  1172. setAlterText = false;
  1173. }
  1174. }
  1175. void CBattleHex::clickLeft(boost::logic::tribool down)
  1176. {
  1177. if(!down && hovered && strictHovered) //we've been really clicked!
  1178. {
  1179. myInterface->hexLclicked(myNumber);
  1180. }
  1181. }
  1182. void CBattleHex::clickRight(boost::logic::tribool down)
  1183. {
  1184. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1185. if(hovered && strictHovered && stID!=-1)
  1186. {
  1187. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1188. if(!myst.alive) return;
  1189. StackState *pom = NULL;
  1190. if(down)
  1191. {
  1192. pom = new StackState();
  1193. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1194. if(h)
  1195. {
  1196. pom->attackBonus = h->primSkills[0];
  1197. pom->defenseBonus = h->primSkills[1];
  1198. pom->luck = h->getCurrentLuck();
  1199. pom->morale = h->getCurrentMorale();
  1200. pom->currentHealth = myst.firstHPleft;
  1201. }
  1202. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1203. ->activate();
  1204. }
  1205. delete pom;
  1206. }
  1207. }
  1208. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1209. {
  1210. }
  1211. CBattleConsole::~CBattleConsole()
  1212. {
  1213. texts.clear();
  1214. }
  1215. void CBattleConsole::show(SDL_Surface * to)
  1216. {
  1217. if(alterTxt.size())
  1218. {
  1219. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1220. }
  1221. else if(texts.size())
  1222. {
  1223. if(texts.size()==1)
  1224. {
  1225. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1226. }
  1227. else
  1228. {
  1229. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1230. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1231. }
  1232. }
  1233. }
  1234. bool CBattleConsole::addText(std::string text)
  1235. {
  1236. if(text.size()>70)
  1237. return false; //text too long!
  1238. int firstInToken = 0;
  1239. for(int i=0; i<text.size(); ++i) //tokenize
  1240. {
  1241. if(text[i] == 10)
  1242. {
  1243. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1244. firstInToken = i+1;
  1245. }
  1246. }
  1247. texts.push_back( text.substr(firstInToken, text.size()) );
  1248. lastShown = texts.size()-1;
  1249. return true;
  1250. }
  1251. void CBattleConsole::eraseText(unsigned int pos)
  1252. {
  1253. if(pos < texts.size())
  1254. {
  1255. texts.erase(texts.begin() + pos);
  1256. if(lastShown == texts.size())
  1257. --lastShown;
  1258. }
  1259. }
  1260. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1261. {
  1262. if(pos >= texts.size()) //no such pos
  1263. return;
  1264. texts[pos] = text;
  1265. }
  1266. void CBattleConsole::scrollUp(unsigned int by)
  1267. {
  1268. if(lastShown > by)
  1269. lastShown -= by;
  1270. }
  1271. void CBattleConsole::scrollDown(unsigned int by)
  1272. {
  1273. if(lastShown + by < texts.size())
  1274. lastShown += by;
  1275. }
  1276. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, SDL_Rect & pos, const CBattleInterface * owner)
  1277. {
  1278. this->pos = pos;
  1279. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1280. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1281. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1282. SDL_FreeSurface(background);
  1283. background = pom;
  1284. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1285. if(br.winner==0) //attacker won
  1286. {
  1287. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR16, zwykly, background);
  1288. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR16, zwykly, background);
  1289. }
  1290. else //if(br.winner==1)
  1291. {
  1292. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR16, zwykly, background);
  1293. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR16, zwykly, background);
  1294. }
  1295. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1296. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1297. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1298. std::string attackerName, defenderName;
  1299. if(owner->attackingHeroInstance) //a hero attacked
  1300. {
  1301. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1302. //setting attackerName
  1303. attackerName = owner->attackingHeroInstance->name;
  1304. }
  1305. else //a monster attacked
  1306. {
  1307. int bestMonsterID = -1;
  1308. int bestPower = 0;
  1309. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1310. {
  1311. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1312. {
  1313. bestPower = CGI->creh->creatures[it->first].AIValue;
  1314. bestMonsterID = it->first;
  1315. }
  1316. }
  1317. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1318. //setting attackerName
  1319. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1320. }
  1321. if(owner->defendingHeroInstance) //a hero defended
  1322. {
  1323. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1324. //setting defenderName
  1325. defenderName = owner->defendingHeroInstance->name;
  1326. }
  1327. else //a monster defended
  1328. {
  1329. int bestMonsterID = -1;
  1330. int bestPower = 0;
  1331. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1332. {
  1333. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1334. {
  1335. bestPower = CGI->creh->creatures[it->first].AIValue;
  1336. bestMonsterID = it->first;
  1337. }
  1338. }
  1339. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1340. //setting defenderName
  1341. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1342. }
  1343. //printing attacker and defender's names
  1344. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1345. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1346. }
  1347. CBattleReslutWindow::~CBattleReslutWindow()
  1348. {
  1349. SDL_FreeSurface(background);
  1350. }
  1351. void CBattleReslutWindow::activate()
  1352. {
  1353. exit->activate();
  1354. }
  1355. void CBattleReslutWindow::deactivate()
  1356. {
  1357. exit->deactivate();
  1358. }
  1359. void CBattleReslutWindow::show(SDL_Surface *to)
  1360. {
  1361. //evaluating to
  1362. if(!to)
  1363. to = screen;
  1364. SDL_BlitSurface(background, NULL, to, &pos);
  1365. exit->show(to);
  1366. }
  1367. void CBattleReslutWindow::bExitf()
  1368. {
  1369. LOCPLINT->battleResultQuited();
  1370. }