CBattleInterface.cpp 48 KB

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