CBattleInterface.cpp 53 KB

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