CBattleInterface.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  807. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  808. resWindow->activate();
  809. }
  810. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  811. {
  812. /*for(int i=0; i<shadedHexes.size(); ++i)
  813. {
  814. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  815. }*/
  816. //CSDL_Ext::blit8bppAlphaTo24bpp(shadedHexesGraphic, NULL, to, NULL);
  817. }
  818. void CBattleInterface::attackingShowHelper()
  819. {
  820. if(attackingInfo && !attackingInfo->reversing)
  821. {
  822. if(attackingInfo->frame == 0)
  823. {
  824. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  825. if(attackingInfo->shooting)
  826. {
  827. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  828. }
  829. else
  830. {
  831. if(aStack.creature->isDoubleWide())
  832. {
  833. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  834. {
  835. case 0:
  836. creAnims[attackingInfo->ID]->setType(10);
  837. break;
  838. case 1:
  839. creAnims[attackingInfo->ID]->setType(10);
  840. break;
  841. case 2:
  842. creAnims[attackingInfo->ID]->setType(11);
  843. break;
  844. case 3:
  845. creAnims[attackingInfo->ID]->setType(12);
  846. break;
  847. case 4:
  848. creAnims[attackingInfo->ID]->setType(12);
  849. break;
  850. case 5:
  851. creAnims[attackingInfo->ID]->setType(11);
  852. break;
  853. }
  854. }
  855. else //else for if(aStack.creature->isDoubleWide())
  856. {
  857. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  858. {
  859. case 0:
  860. creAnims[attackingInfo->ID]->setType(10);
  861. break;
  862. case 1:
  863. creAnims[attackingInfo->ID]->setType(10);
  864. break;
  865. case 2:
  866. creAnims[attackingInfo->ID]->setType(11);
  867. break;
  868. case 3:
  869. creAnims[attackingInfo->ID]->setType(12);
  870. break;
  871. case 4:
  872. creAnims[attackingInfo->ID]->setType(12);
  873. break;
  874. case 5:
  875. creAnims[attackingInfo->ID]->setType(11);
  876. break;
  877. }
  878. }
  879. }
  880. }
  881. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  882. {
  883. attackingInfo->reversing = true;
  884. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  885. if(aStack.creature->isDoubleWide())
  886. {
  887. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  888. {
  889. case 0:
  890. //reverseCreature(ID, aStack.position, true);
  891. break;
  892. case 1:
  893. break;
  894. case 2:
  895. break;
  896. case 3:
  897. break;
  898. case 4:
  899. //reverseCreature(ID, aStack.position, true);
  900. break;
  901. case 5:
  902. reverseCreature(attackingInfo->ID, aStack.position, true);
  903. break;
  904. }
  905. }
  906. else //else for if(aStack.creature->isDoubleWide())
  907. {
  908. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  909. {
  910. case 0:
  911. reverseCreature(attackingInfo->ID, aStack.position, true);
  912. break;
  913. case 1:
  914. break;
  915. case 2:
  916. break;
  917. case 3:
  918. break;
  919. case 4:
  920. reverseCreature(attackingInfo->ID, aStack.position, true);
  921. break;
  922. case 5:
  923. reverseCreature(attackingInfo->ID, aStack.position, true);
  924. break;
  925. }
  926. }
  927. attackingInfo->reversing = false;
  928. creAnims[attackingInfo->ID]->setType(2);
  929. delete attackingInfo;
  930. attackingInfo = NULL;
  931. }
  932. if(attackingInfo)
  933. {
  934. attackingInfo->frame++;
  935. }
  936. }
  937. }
  938. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  939. {
  940. char tabh[200];
  941. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  942. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  943. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  944. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  945. dmg);
  946. if(killed > 0)
  947. {
  948. if(killed > 1)
  949. {
  950. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  951. }
  952. else //killed == 1
  953. {
  954. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  955. }
  956. }
  957. console->addText(std::string(tabh));
  958. }
  959. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  960. {
  961. if(to == NULL)
  962. to = screen;
  963. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  964. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  965. {
  966. if(it->animStartDelay>0)
  967. {
  968. --(it->animStartDelay);
  969. continue;
  970. }
  971. SDL_Rect dst;
  972. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  973. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  974. dst.x = it->x;
  975. dst.y = it->y;
  976. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  977. //actualizing projectile
  978. ++it->step;
  979. if(it->step == it->lastStep)
  980. {
  981. toBeDeleted.insert(toBeDeleted.end(), it);
  982. }
  983. else
  984. {
  985. it->x += it->dx;
  986. it->y += it->dy;
  987. if(it->spin)
  988. {
  989. ++(it->frameNum);
  990. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  991. }
  992. }
  993. }
  994. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  995. {
  996. projectiles.erase(*it);
  997. }
  998. }
  999. void CBattleHero::show(SDL_Surface *to)
  1000. {
  1001. //animation of flag
  1002. if(flip)
  1003. {
  1004. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1005. }
  1006. else
  1007. {
  1008. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1009. }
  1010. {
  1011. ++flagAnim;
  1012. flagAnim %= flag->ourImages.size();
  1013. }
  1014. //animation of hero
  1015. int tick=-1;
  1016. for(int i=0; i<dh->ourImages.size(); ++i)
  1017. {
  1018. if(dh->ourImages[i].groupNumber==phase)
  1019. ++tick;
  1020. if(tick==image)
  1021. {
  1022. SDL_Rect posb = pos;
  1023. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1024. ++image;
  1025. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1026. {
  1027. image = 0;
  1028. }
  1029. break;
  1030. }
  1031. }
  1032. }
  1033. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  1034. {
  1035. dh = CDefHandler::giveDef( defName );
  1036. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1037. {
  1038. if(flip)
  1039. {
  1040. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1041. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1042. dh->ourImages[i].bitmap = hlp;
  1043. }
  1044. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1045. }
  1046. dh->alphaTransformed = true;
  1047. if(flip)
  1048. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1049. else
  1050. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1051. //coloring flag and adding transparency
  1052. for(int i=0; i<flag->ourImages.size(); ++i)
  1053. {
  1054. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1055. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1056. }
  1057. }
  1058. CBattleHero::~CBattleHero()
  1059. {
  1060. delete dh;
  1061. delete flag;
  1062. }
  1063. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  1064. {
  1065. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1066. ret.second = -139 + 42 * (hexNum/17); //counting y
  1067. //counting x
  1068. if(attacker)
  1069. {
  1070. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1071. }
  1072. else
  1073. {
  1074. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1075. }
  1076. //shifting position for double - hex creatures
  1077. if(creature->isDoubleWide())
  1078. {
  1079. if(attacker)
  1080. {
  1081. ret.first -= 42;
  1082. }
  1083. else
  1084. {
  1085. ret.first += 42;
  1086. }
  1087. }
  1088. //returning
  1089. return ret;
  1090. }
  1091. void CBattleHex::activate()
  1092. {
  1093. Hoverable::activate();
  1094. MotionInterested::activate();
  1095. ClickableL::activate();
  1096. ClickableR::activate();
  1097. }
  1098. void CBattleHex::deactivate()
  1099. {
  1100. Hoverable::deactivate();
  1101. MotionInterested::deactivate();
  1102. ClickableL::deactivate();
  1103. ClickableR::deactivate();
  1104. }
  1105. void CBattleHex::hover(bool on)
  1106. {
  1107. hovered = on;
  1108. Hoverable::hover(on);
  1109. if(!on && setAlterText)
  1110. {
  1111. myInterface->console->alterTxt = std::string();
  1112. setAlterText = false;
  1113. }
  1114. }
  1115. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1116. {
  1117. }
  1118. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  1119. {
  1120. if(myInterface->cellShade)
  1121. {
  1122. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1123. {
  1124. strictHovered = false;
  1125. }
  1126. else //hovered pixel is inside hex
  1127. {
  1128. strictHovered = true;
  1129. if(myInterface->activeStack>=0)
  1130. {
  1131. if(std::find(myInterface->shadedHexes.begin(),myInterface->shadedHexes.end(),myNumber) == myInterface->shadedHexes.end())
  1132. {
  1133. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  1134. if(shere)
  1135. {
  1136. if(shere->owner == LOCPLINT->playerID) //our stack
  1137. CGI->curh->changeGraphic(1,5);
  1138. else if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isShooting()) //we can shoot enemy
  1139. CGI->curh->changeGraphic(1,3);
  1140. else //unavailable enemy
  1141. CGI->curh->changeGraphic(1,0);
  1142. }
  1143. else //empty unavailable tile
  1144. CGI->curh->changeGraphic(1,0);
  1145. }
  1146. else //available tile
  1147. {
  1148. if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isFlying())
  1149. CGI->curh->changeGraphic(1,2);
  1150. else
  1151. CGI->curh->changeGraphic(1,1);
  1152. }
  1153. }
  1154. }
  1155. }
  1156. if(hovered && strictHovered) //print attacked creature to console
  1157. {
  1158. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1159. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1160. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive)
  1161. {
  1162. char tabh[160];
  1163. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1164. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1165. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1166. myInterface->console->alterTxt = std::string(tabh);
  1167. setAlterText = true;
  1168. }
  1169. }
  1170. else if(setAlterText)
  1171. {
  1172. myInterface->console->alterTxt = std::string();
  1173. setAlterText = false;
  1174. }
  1175. }
  1176. void CBattleHex::clickLeft(boost::logic::tribool down)
  1177. {
  1178. if(!down && hovered && strictHovered) //we've been really clicked!
  1179. {
  1180. myInterface->hexLclicked(myNumber);
  1181. }
  1182. }
  1183. void CBattleHex::clickRight(boost::logic::tribool down)
  1184. {
  1185. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1186. if(hovered && strictHovered && stID!=-1)
  1187. {
  1188. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1189. if(!myst.alive) return;
  1190. StackState *pom = NULL;
  1191. if(down)
  1192. {
  1193. pom = new StackState();
  1194. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1195. if(h)
  1196. {
  1197. pom->attackBonus = h->primSkills[0];
  1198. pom->defenseBonus = h->primSkills[1];
  1199. pom->luck = h->getCurrentLuck();
  1200. pom->morale = h->getCurrentMorale();
  1201. pom->currentHealth = myst.firstHPleft;
  1202. }
  1203. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1204. ->activate();
  1205. }
  1206. delete pom;
  1207. }
  1208. }
  1209. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1210. {
  1211. }
  1212. CBattleConsole::~CBattleConsole()
  1213. {
  1214. texts.clear();
  1215. }
  1216. void CBattleConsole::show(SDL_Surface * to)
  1217. {
  1218. if(alterTxt.size())
  1219. {
  1220. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1221. }
  1222. else if(texts.size())
  1223. {
  1224. if(texts.size()==1)
  1225. {
  1226. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1227. }
  1228. else
  1229. {
  1230. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1231. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1232. }
  1233. }
  1234. }
  1235. bool CBattleConsole::addText(std::string text)
  1236. {
  1237. if(text.size()>70)
  1238. return false; //text too long!
  1239. int firstInToken = 0;
  1240. for(int i=0; i<text.size(); ++i) //tokenize
  1241. {
  1242. if(text[i] == 10)
  1243. {
  1244. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1245. firstInToken = i+1;
  1246. }
  1247. }
  1248. texts.push_back( text.substr(firstInToken, text.size()) );
  1249. lastShown = texts.size()-1;
  1250. return true;
  1251. }
  1252. void CBattleConsole::eraseText(unsigned int pos)
  1253. {
  1254. if(pos < texts.size())
  1255. {
  1256. texts.erase(texts.begin() + pos);
  1257. if(lastShown == texts.size())
  1258. --lastShown;
  1259. }
  1260. }
  1261. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1262. {
  1263. if(pos >= texts.size()) //no such pos
  1264. return;
  1265. texts[pos] = text;
  1266. }
  1267. void CBattleConsole::scrollUp(unsigned int by)
  1268. {
  1269. if(lastShown > by)
  1270. lastShown -= by;
  1271. }
  1272. void CBattleConsole::scrollDown(unsigned int by)
  1273. {
  1274. if(lastShown + by < texts.size())
  1275. lastShown += by;
  1276. }
  1277. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, SDL_Rect & pos, const CBattleInterface * owner)
  1278. {
  1279. this->pos = pos;
  1280. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1281. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1282. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1283. SDL_FreeSurface(background);
  1284. background = pom;
  1285. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1286. if(br.winner==0) //attacker won
  1287. {
  1288. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1289. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1290. }
  1291. else //if(br.winner==1)
  1292. {
  1293. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1294. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1295. }
  1296. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1297. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1298. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1299. std::string attackerName, defenderName;
  1300. if(owner->attackingHeroInstance) //a hero attacked
  1301. {
  1302. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1303. //setting attackerName
  1304. attackerName = owner->attackingHeroInstance->name;
  1305. }
  1306. else //a monster attacked
  1307. {
  1308. int bestMonsterID = -1;
  1309. int bestPower = 0;
  1310. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1311. {
  1312. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1313. {
  1314. bestPower = CGI->creh->creatures[it->first].AIValue;
  1315. bestMonsterID = it->first;
  1316. }
  1317. }
  1318. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1319. //setting attackerName
  1320. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1321. }
  1322. if(owner->defendingHeroInstance) //a hero defended
  1323. {
  1324. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1325. //setting defenderName
  1326. defenderName = owner->defendingHeroInstance->name;
  1327. }
  1328. else //a monster defended
  1329. {
  1330. int bestMonsterID = -1;
  1331. int bestPower = 0;
  1332. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1333. {
  1334. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1335. {
  1336. bestPower = CGI->creh->creatures[it->first].AIValue;
  1337. bestMonsterID = it->first;
  1338. }
  1339. }
  1340. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1341. //setting defenderName
  1342. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1343. }
  1344. //printing attacker and defender's names
  1345. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1346. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1347. //printing casualities
  1348. if(br.s1.size()==0)
  1349. {
  1350. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360, GEOR16, zwykly, background);
  1351. }
  1352. else
  1353. {
  1354. int xPos = 235 - (br.s1.size()*32 + (br.s1.size() - 1)*10)/2; //increment by 42 with each picture
  1355. int yPos = 344;
  1356. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.s1.begin(); it!=br.s1.end(); ++it)
  1357. {
  1358. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1359. std::stringstream amount;
  1360. amount<<it->second;
  1361. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1362. xPos += 42;
  1363. }
  1364. }
  1365. if(br.s2.size()==0)
  1366. {
  1367. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 457, GEOR16, zwykly, background);
  1368. }
  1369. else
  1370. {
  1371. int xPos = 235 - (br.s2.size()*32 + (br.s2.size() - 1)*10)/2; //increment by 42 with each picture
  1372. int yPos = 441;
  1373. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.s2.begin(); it!=br.s2.end(); ++it)
  1374. {
  1375. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1376. std::stringstream amount;
  1377. amount<<it->second;
  1378. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1379. xPos += 42;
  1380. }
  1381. }
  1382. }
  1383. CBattleReslutWindow::~CBattleReslutWindow()
  1384. {
  1385. SDL_FreeSurface(background);
  1386. }
  1387. void CBattleReslutWindow::activate()
  1388. {
  1389. exit->activate();
  1390. }
  1391. void CBattleReslutWindow::deactivate()
  1392. {
  1393. exit->deactivate();
  1394. }
  1395. void CBattleReslutWindow::show(SDL_Surface *to)
  1396. {
  1397. //evaluating to
  1398. if(!to)
  1399. to = screen;
  1400. SDL_BlitSurface(background, NULL, to, &pos);
  1401. exit->show(to);
  1402. }
  1403. void CBattleReslutWindow::bExitf()
  1404. {
  1405. LOCPLINT->battleResultQuited();
  1406. }