CBattleInterface.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900
  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), animSpeed(1)
  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/animSpeed)==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/animSpeed)==0) && creAnims[stackAliveByHex[b][v]]->getType()!=0 && creAnims[stackAliveByHex[b][v]]->getType()!=20 && creAnims[stackAliveByHex[b][v]]->getType()!=21, 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/animSpeed)==0)
  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. CGI->curh->changeGraphic(0,0);
  442. LOCPLINT->curint->deactivate();
  443. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  444. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  445. optionsWin->activate();
  446. LOCPLINT->objsToBlit.push_back(optionsWin);
  447. }
  448. void CBattleInterface::bSurrenderf()
  449. {
  450. }
  451. void CBattleInterface::bFleef()
  452. {
  453. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  454. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  455. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  456. }
  457. void CBattleInterface::reallyFlee()
  458. {
  459. giveCommand(4,0,0);
  460. CGI->curh->changeGraphic(0, 0);
  461. }
  462. void CBattleInterface::bAutofightf()
  463. {
  464. }
  465. void CBattleInterface::bSpellf()
  466. {
  467. CGI->curh->changeGraphic(0,0);
  468. LOCPLINT->curint->deactivate();
  469. const CGHeroInstance * chi = NULL;
  470. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  471. chi = attackingHeroInstance;
  472. else
  473. chi = defendingHeroInstance;
  474. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), chi);
  475. spellWindow->activate();
  476. LOCPLINT->objsToBlit.push_back(spellWindow);
  477. }
  478. void CBattleInterface::bWaitf()
  479. {
  480. }
  481. void CBattleInterface::bDefencef()
  482. {
  483. giveCommand(3,0,activeStack);
  484. }
  485. void CBattleInterface::bConsoleUpf()
  486. {
  487. console->scrollUp();
  488. }
  489. void CBattleInterface::bConsoleDownf()
  490. {
  491. console->scrollDown();
  492. }
  493. void CBattleInterface::newStack(CStack stack)
  494. {
  495. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  496. creAnims[stack.ID]->setType(2);
  497. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  498. }
  499. void CBattleInterface::stackRemoved(CStack stack)
  500. {
  501. delete creAnims[stack.ID];
  502. creAnims.erase(stack.ID);
  503. }
  504. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  505. {
  506. if(creAnims[ID]->getType() != 2)
  507. {
  508. return; //something went wrong
  509. }
  510. if(byShooting) //delay hit animation
  511. {
  512. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  513. while(true)
  514. {
  515. bool found = false;
  516. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  517. {
  518. if(it->creID == attacker.creature->idNumber)
  519. {
  520. found = true;
  521. break;
  522. }
  523. }
  524. if(!found)
  525. break;
  526. else
  527. {
  528. show();
  529. CSDL_Ext::update();
  530. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  531. }
  532. }
  533. }
  534. creAnims[ID]->setType(5); //death
  535. for(int i=0; i<creAnims[ID]->framesInGroup(5)*3+1; ++i)
  536. {
  537. if((animCount%(4/animSpeed))==0)
  538. creAnims[ID]->incrementFrame();
  539. show();
  540. CSDL_Ext::update();
  541. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  542. }
  543. printConsoleAttacked(ID, dmg, killed, IDby);
  544. }
  545. void CBattleInterface::stackActivated(int number)
  546. {
  547. //givenCommand = NULL;
  548. activeStack = number;
  549. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(number);
  550. myTurn = true;
  551. //preparating background graphic with hexes and shaded hexes
  552. blitAt(background, 0, 0, backgroundWithHexes);
  553. if(printCellBorders)
  554. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  555. for(int m=0; m<shadedHexes.size(); ++m) //rows
  556. {
  557. int i = shadedHexes[m]/17; //row
  558. int j = shadedHexes[m]%17-1; //column
  559. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  560. int y = 86 + 42 * i;
  561. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  562. }
  563. }
  564. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  565. {
  566. //a few useful variables
  567. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  568. int steps;
  569. switch(animSpeed)
  570. {
  571. case 1:
  572. steps = creAnims[number]->framesInGroup(0)*3.5;
  573. break;
  574. case 2:
  575. steps = creAnims[number]->framesInGroup(0)*2.2;
  576. break;
  577. case 4:
  578. steps = creAnims[number]->framesInGroup(0);
  579. }
  580. int hexWbase = 44, hexHbase = 42;
  581. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  582. if(startMoving && creAnims[number]->framesInGroup(20)!=0) //animation of starting move; some units don't have this animation (ie. halberdier)
  583. {
  584. deactivate();
  585. CGI->curh->hide();
  586. creAnims[number]->setType(20);
  587. //LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  588. for(int i=0; i<creAnims[number]->framesInGroup(20)*3+1; ++i)
  589. {
  590. show();
  591. CSDL_Ext::update();
  592. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  593. if((animCount+1)%(4/animSpeed)==0)
  594. creAnims[number]->incrementFrame();
  595. }
  596. }
  597. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  598. {
  599. switch(mutPos) //reverse unit if necessary
  600. {
  601. case 0: case 4: case 5:
  602. if(creDir[number] == true)
  603. reverseCreature(number, curStackPos, twoTiles);
  604. break;
  605. case 1: case 2: case 3:
  606. if(creDir[number] == false)
  607. reverseCreature(number, curStackPos, twoTiles);
  608. break;
  609. }
  610. //moving instructions
  611. creAnims[number]->setType(0);
  612. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  613. for(int i=0; i<steps; ++i)
  614. {
  615. switch(mutPos)
  616. {
  617. case 0:
  618. posX -= ((float)hexWbase)/(2.0f*steps);
  619. creAnims[number]->pos.x = posX;
  620. posY -= ((float)hexHbase)/((float)steps);
  621. creAnims[number]->pos.y = posY;
  622. break;
  623. case 1:
  624. posX += ((float)hexWbase)/(2.0f*steps);
  625. creAnims[number]->pos.x = posX;
  626. posY -= ((float)hexHbase)/((float)steps);
  627. creAnims[number]->pos.y = posY;
  628. break;
  629. case 2:
  630. posX += ((float)hexWbase)/((float)steps);
  631. creAnims[number]->pos.x = posX;
  632. break;
  633. case 3:
  634. posX += ((float)hexWbase)/(2.0f*steps);
  635. creAnims[number]->pos.x = posX;
  636. posY += ((float)hexHbase)/((float)steps);
  637. creAnims[number]->pos.y = posY;
  638. break;
  639. case 4:
  640. posX -= ((float)hexWbase)/(2.0f*steps);
  641. creAnims[number]->pos.x = posX;
  642. posY += ((float)hexHbase)/((float)steps);
  643. creAnims[number]->pos.y = posY;
  644. break;
  645. case 5:
  646. posX -= ((float)hexWbase)/((float)steps);
  647. creAnims[number]->pos.x = posX;
  648. break;
  649. }
  650. show();
  651. CSDL_Ext::update();
  652. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  653. if((animCount+1)%(4/animSpeed)==0)
  654. creAnims[number]->incrementFrame();
  655. }
  656. }
  657. if(endMoving) //animation of ending move
  658. {
  659. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  660. {
  661. creAnims[number]->setType(21);
  662. for(int i=0; i<creAnims[number]->framesInGroup(21)*3+1; ++i)
  663. {
  664. show();
  665. CSDL_Ext::update();
  666. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  667. if((animCount+1)%(4/animSpeed)==0)
  668. creAnims[number]->incrementFrame();
  669. }
  670. }
  671. creAnims[number]->setType(2); //resetting to default
  672. activate();
  673. CGI->curh->show();
  674. }
  675. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  676. if(endMoving) //resetting to default
  677. {
  678. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  679. reverseCreature(number, destHex, twoTiles);
  680. }
  681. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  682. creAnims[number]->pos.x = coords.first;
  683. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  684. creAnims[number]->pos.x -= 44;
  685. creAnims[number]->pos.y = coords.second;
  686. }
  687. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  688. {
  689. while(creAnims[ID]->getType() != 2 || (attackingInfo && attackingInfo->IDby == IDby))
  690. {
  691. show();
  692. CSDL_Ext::update();
  693. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  694. }
  695. if(byShooting) //delay hit animation
  696. {
  697. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  698. while(true)
  699. {
  700. bool found = false;
  701. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  702. {
  703. if(it->creID == attacker.creature->idNumber)
  704. {
  705. found = true;
  706. break;
  707. }
  708. }
  709. if(!found)
  710. break;
  711. else
  712. {
  713. show();
  714. CSDL_Ext::update();
  715. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  716. }
  717. }
  718. }
  719. creAnims[ID]->setType(3); //getting hit
  720. for(int i=0; i<creAnims[ID]->framesInGroup(3)*3+1; ++i)
  721. {
  722. show();
  723. CSDL_Ext::update();
  724. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  725. /*if((animCount+1)%(4/animSpeed)==0)
  726. creAnims[ID]->incrementFrame();*/
  727. }
  728. creAnims[ID]->setType(2);
  729. printConsoleAttacked(ID, dmg, killed, IDby);
  730. }
  731. void CBattleInterface::stackAttacking(int ID, int dest)
  732. {
  733. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  734. {
  735. show();
  736. CSDL_Ext::update();
  737. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  738. }
  739. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  740. if(aStack.attackerOwned)
  741. {
  742. if(aStack.creature->isDoubleWide())
  743. {
  744. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  745. {
  746. case 0:
  747. //reverseCreature(ID, aStack.position, true);
  748. break;
  749. case 1:
  750. break;
  751. case 2:
  752. break;
  753. case 3:
  754. break;
  755. case 4:
  756. //reverseCreature(ID, aStack.position, true);
  757. break;
  758. case 5:
  759. reverseCreature(ID, aStack.position, true);
  760. break;
  761. }
  762. }
  763. else //else for if(aStack.creature->isDoubleWide())
  764. {
  765. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  766. {
  767. case 0:
  768. reverseCreature(ID, aStack.position, true);
  769. break;
  770. case 1:
  771. break;
  772. case 2:
  773. break;
  774. case 3:
  775. break;
  776. case 4:
  777. reverseCreature(ID, aStack.position, true);
  778. break;
  779. case 5:
  780. reverseCreature(ID, aStack.position, true);
  781. break;
  782. }
  783. }
  784. }
  785. else //if(aStack.attackerOwned)
  786. {
  787. if(aStack.creature->isDoubleWide())
  788. {
  789. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  790. {
  791. case 0:
  792. //reverseCreature(ID, aStack.position, true);
  793. break;
  794. case 1:
  795. break;
  796. case 2:
  797. reverseCreature(ID, aStack.position, true);
  798. break;
  799. case 3:
  800. break;
  801. case 4:
  802. //reverseCreature(ID, aStack.position, true);
  803. break;
  804. case 5:
  805. //reverseCreature(ID, aStack.position, true);
  806. break;
  807. }
  808. }
  809. else //else for if(aStack.creature->isDoubleWide())
  810. {
  811. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  812. {
  813. case 0:
  814. //reverseCreature(ID, aStack.position, true);
  815. break;
  816. case 1:
  817. reverseCreature(ID, aStack.position, true);
  818. break;
  819. case 2:
  820. reverseCreature(ID, aStack.position, true);
  821. break;
  822. case 3:
  823. reverseCreature(ID, aStack.position, true);
  824. break;
  825. case 4:
  826. //reverseCreature(ID, aStack.position, true);
  827. break;
  828. case 5:
  829. //reverseCreature(ID, aStack.position, true);
  830. break;
  831. }
  832. }
  833. }
  834. attackingInfo = new CAttHelper;
  835. attackingInfo->dest = dest;
  836. attackingInfo->frame = 0;
  837. attackingInfo->hitCount = 0;
  838. attackingInfo->ID = ID;
  839. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  840. attackingInfo->reversing = false;
  841. attackingInfo->shooting = false;
  842. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  843. {
  844. case 0:
  845. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  846. break;
  847. case 1:
  848. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  849. break;
  850. case 2:
  851. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  852. break;
  853. case 3:
  854. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  855. break;
  856. case 4:
  857. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  858. break;
  859. case 5:
  860. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  861. break;
  862. }
  863. }
  864. void CBattleInterface::newRound(int number)
  865. {
  866. console->addText(CGI->generaltexth->allTexts[412]);
  867. }
  868. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  869. {
  870. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  871. ba->actionType = action;
  872. ba->destinationTile = tile;
  873. ba->stackNumber = stack;
  874. ba->additionalInfo = additional;
  875. givenCommand->setn(ba);
  876. myTurn = false;
  877. activeStack = -1;
  878. }
  879. void CBattleInterface::hexLclicked(int whichOne)
  880. {
  881. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  882. {
  883. if(!myTurn)
  884. return; //we are not permit to do anything
  885. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  886. if(!dest || !dest->alive()) //no creature at that tile
  887. {
  888. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  889. giveCommand(2,whichOne,activeStack);
  890. }
  891. else if(dest->owner != attackingHeroInstance->tempOwner
  892. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)
  893. && BattleInfo::mutualPosition(LOCPLINT->cb->battleGetPos(activeStack),whichOne) < 0 ) //shooting
  894. {
  895. giveCommand(7,whichOne,activeStack);
  896. }
  897. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  898. {
  899. std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  900. for(int i=0;i<n.size();i++)
  901. {
  902. //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
  903. if(vstd::contains(shadedHexes,n[i]))
  904. {
  905. giveCommand(6,n[i],activeStack,whichOne);
  906. return;
  907. }
  908. }
  909. }
  910. }
  911. }
  912. void CBattleInterface::stackIsShooting(int ID, int dest)
  913. {
  914. if(attackingInfo != NULL)
  915. {
  916. return; //something went wrong
  917. }
  918. //projectile
  919. float projectileAngle; //in radians; if positive, projectiles goes up
  920. 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)
  921. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  922. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  923. if(fromHex < dest)
  924. projectileAngle = -projectileAngle;
  925. SProjectileInfo spi;
  926. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  927. spi.step = 0;
  928. spi.frameNum = 0;
  929. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  930. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  931. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  932. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  933. if(projectileAngle > straightAngle) //upper shot
  934. {
  935. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  936. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  937. }
  938. else if(projectileAngle < -straightAngle) //lower shot
  939. {
  940. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  941. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  942. }
  943. else //straight shot
  944. {
  945. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  946. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  947. }
  948. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  949. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  950. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  951. //set starting frame
  952. if(spi.spin)
  953. {
  954. spi.frameNum = 0;
  955. }
  956. else
  957. {
  958. 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);
  959. }
  960. //set delay
  961. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  962. projectiles.push_back(spi);
  963. //attack aniamtion
  964. attackingInfo = new CAttHelper;
  965. attackingInfo->dest = dest;
  966. attackingInfo->frame = 0;
  967. attackingInfo->hitCount = 0;
  968. attackingInfo->ID = ID;
  969. attackingInfo->reversing = false;
  970. attackingInfo->shooting = true;
  971. if(projectileAngle > straightAngle) //upper shot
  972. attackingInfo->shootingGroup = 14;
  973. else if(projectileAngle < -straightAngle) //lower shot
  974. attackingInfo->shootingGroup = 16;
  975. else //straight shot
  976. attackingInfo->shootingGroup = 15;
  977. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  978. }
  979. void CBattleInterface::battleFinished(const BattleResult& br)
  980. {
  981. deactivate();
  982. CGI->curh->changeGraphic(0,0);
  983. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  984. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  985. resWindow->activate();
  986. }
  987. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  988. {
  989. /*for(int i=0; i<shadedHexes.size(); ++i)
  990. {
  991. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  992. }*/
  993. //CSDL_Ext::blit8bppAlphaTo24bpp(shadedHexesGraphic, NULL, to, NULL);
  994. }
  995. void CBattleInterface::attackingShowHelper()
  996. {
  997. if(attackingInfo && !attackingInfo->reversing)
  998. {
  999. if(attackingInfo->frame == 0)
  1000. {
  1001. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1002. if(attackingInfo->shooting)
  1003. {
  1004. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1005. }
  1006. else
  1007. {
  1008. if(aStack.creature->isDoubleWide())
  1009. {
  1010. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1011. {
  1012. case 0:
  1013. creAnims[attackingInfo->ID]->setType(11);
  1014. break;
  1015. case 1:
  1016. creAnims[attackingInfo->ID]->setType(11);
  1017. break;
  1018. case 2:
  1019. creAnims[attackingInfo->ID]->setType(12);
  1020. break;
  1021. case 3:
  1022. creAnims[attackingInfo->ID]->setType(13);
  1023. break;
  1024. case 4:
  1025. creAnims[attackingInfo->ID]->setType(13);
  1026. break;
  1027. case 5:
  1028. creAnims[attackingInfo->ID]->setType(12);
  1029. break;
  1030. }
  1031. }
  1032. else //else for if(aStack.creature->isDoubleWide())
  1033. {
  1034. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1035. {
  1036. case 0:
  1037. creAnims[attackingInfo->ID]->setType(11);
  1038. break;
  1039. case 1:
  1040. creAnims[attackingInfo->ID]->setType(11);
  1041. break;
  1042. case 2:
  1043. creAnims[attackingInfo->ID]->setType(12);
  1044. break;
  1045. case 3:
  1046. creAnims[attackingInfo->ID]->setType(13);
  1047. break;
  1048. case 4:
  1049. creAnims[attackingInfo->ID]->setType(13);
  1050. break;
  1051. case 5:
  1052. creAnims[attackingInfo->ID]->setType(12);
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. }
  1058. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1059. {
  1060. attackingInfo->reversing = true;
  1061. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1062. if(aStackp == NULL)
  1063. return;
  1064. CStack aStack = *aStackp;
  1065. if(aStack.attackerOwned)
  1066. {
  1067. if(aStack.creature->isDoubleWide())
  1068. {
  1069. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1070. {
  1071. case 0:
  1072. //reverseCreature(ID, aStack.position, true);
  1073. break;
  1074. case 1:
  1075. break;
  1076. case 2:
  1077. break;
  1078. case 3:
  1079. break;
  1080. case 4:
  1081. //reverseCreature(ID, aStack.position, true);
  1082. break;
  1083. case 5:
  1084. reverseCreature(attackingInfo->ID, aStack.position, true);
  1085. break;
  1086. }
  1087. }
  1088. else //else for if(aStack.creature->isDoubleWide())
  1089. {
  1090. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1091. {
  1092. case 0:
  1093. reverseCreature(attackingInfo->ID, aStack.position, true);
  1094. break;
  1095. case 1:
  1096. break;
  1097. case 2:
  1098. break;
  1099. case 3:
  1100. break;
  1101. case 4:
  1102. reverseCreature(attackingInfo->ID, aStack.position, true);
  1103. break;
  1104. case 5:
  1105. reverseCreature(attackingInfo->ID, aStack.position, true);
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. else //if(aStack.attackerOwned)
  1111. {
  1112. if(aStack.creature->isDoubleWide())
  1113. {
  1114. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1115. {
  1116. case 0:
  1117. //reverseCreature(ID, aStack.position, true);
  1118. break;
  1119. case 1:
  1120. break;
  1121. case 2:
  1122. reverseCreature(attackingInfo->ID, aStack.position, true);
  1123. break;
  1124. case 3:
  1125. break;
  1126. case 4:
  1127. //reverseCreature(ID, aStack.position, true);
  1128. break;
  1129. case 5:
  1130. //reverseCreature(ID, aStack.position, true);
  1131. break;
  1132. }
  1133. }
  1134. else //else for if(aStack.creature->isDoubleWide())
  1135. {
  1136. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1137. {
  1138. case 0:
  1139. //reverseCreature(ID, aStack.position, true);
  1140. break;
  1141. case 1:
  1142. reverseCreature(attackingInfo->ID, aStack.position, true);
  1143. break;
  1144. case 2:
  1145. reverseCreature(attackingInfo->ID, aStack.position, true);
  1146. break;
  1147. case 3:
  1148. reverseCreature(attackingInfo->ID, aStack.position, true);
  1149. break;
  1150. case 4:
  1151. //reverseCreature(ID, aStack.position, true);
  1152. break;
  1153. case 5:
  1154. //reverseCreature(ID, aStack.position, true);
  1155. break;
  1156. }
  1157. }
  1158. }
  1159. attackingInfo->reversing = false;
  1160. creAnims[attackingInfo->ID]->setType(2);
  1161. delete attackingInfo;
  1162. attackingInfo = NULL;
  1163. }
  1164. if(attackingInfo)
  1165. {
  1166. attackingInfo->hitCount++;
  1167. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1168. attackingInfo->frame++;
  1169. }
  1170. }
  1171. }
  1172. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1173. {
  1174. char tabh[200];
  1175. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1176. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1177. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1178. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1179. dmg);
  1180. if(killed > 0)
  1181. {
  1182. if(killed > 1)
  1183. {
  1184. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1185. }
  1186. else //killed == 1
  1187. {
  1188. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1189. }
  1190. }
  1191. console->addText(std::string(tabh));
  1192. }
  1193. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1194. {
  1195. if(to == NULL)
  1196. to = screen;
  1197. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1198. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1199. {
  1200. if(it->animStartDelay>0)
  1201. {
  1202. --(it->animStartDelay);
  1203. continue;
  1204. }
  1205. SDL_Rect dst;
  1206. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1207. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1208. dst.x = it->x;
  1209. dst.y = it->y;
  1210. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1211. //actualizing projectile
  1212. ++it->step;
  1213. if(it->step == it->lastStep)
  1214. {
  1215. toBeDeleted.insert(toBeDeleted.end(), it);
  1216. }
  1217. else
  1218. {
  1219. it->x += it->dx;
  1220. it->y += it->dy;
  1221. if(it->spin)
  1222. {
  1223. ++(it->frameNum);
  1224. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1225. }
  1226. }
  1227. }
  1228. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1229. {
  1230. projectiles.erase(*it);
  1231. }
  1232. }
  1233. void CBattleHero::show(SDL_Surface *to)
  1234. {
  1235. //animation of flag
  1236. if(flip)
  1237. {
  1238. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1239. }
  1240. else
  1241. {
  1242. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1243. }
  1244. {
  1245. ++flagAnim;
  1246. flagAnim %= flag->ourImages.size();
  1247. }
  1248. //animation of hero
  1249. int tick=-1;
  1250. for(int i=0; i<dh->ourImages.size(); ++i)
  1251. {
  1252. if(dh->ourImages[i].groupNumber==phase)
  1253. ++tick;
  1254. if(tick==image)
  1255. {
  1256. SDL_Rect posb = pos;
  1257. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1258. ++image;
  1259. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1260. {
  1261. image = 0;
  1262. }
  1263. break;
  1264. }
  1265. }
  1266. }
  1267. void CBattleHero::activate()
  1268. {
  1269. ClickableL::activate();
  1270. }
  1271. void CBattleHero::deactivate()
  1272. {
  1273. ClickableL::deactivate();
  1274. }
  1275. void CBattleHero::clickLeft(boost::logic::tribool down)
  1276. {
  1277. if(!down && myHero)
  1278. {
  1279. CGI->curh->changeGraphic(0,0);
  1280. LOCPLINT->curint->deactivate();
  1281. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1282. spellWindow->activate();
  1283. LOCPLINT->objsToBlit.push_back(spellWindow);
  1284. }
  1285. }
  1286. 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)
  1287. {
  1288. dh = CDefHandler::giveDef( defName );
  1289. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1290. {
  1291. if(flip)
  1292. {
  1293. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1294. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1295. dh->ourImages[i].bitmap = hlp;
  1296. }
  1297. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1298. }
  1299. dh->alphaTransformed = true;
  1300. if(flip)
  1301. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1302. else
  1303. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1304. //coloring flag and adding transparency
  1305. for(int i=0; i<flag->ourImages.size(); ++i)
  1306. {
  1307. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1308. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1309. }
  1310. }
  1311. CBattleHero::~CBattleHero()
  1312. {
  1313. delete dh;
  1314. delete flag;
  1315. }
  1316. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  1317. {
  1318. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1319. ret.second = -139 + 42 * (hexNum/17); //counting y
  1320. //counting x
  1321. if(attacker)
  1322. {
  1323. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1324. }
  1325. else
  1326. {
  1327. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1328. }
  1329. //shifting position for double - hex creatures
  1330. if(creature->isDoubleWide())
  1331. {
  1332. if(attacker)
  1333. {
  1334. ret.first -= 42;
  1335. }
  1336. else
  1337. {
  1338. ret.first += 42;
  1339. }
  1340. }
  1341. //returning
  1342. return ret;
  1343. }
  1344. void CBattleHex::activate()
  1345. {
  1346. Hoverable::activate();
  1347. MotionInterested::activate();
  1348. ClickableL::activate();
  1349. ClickableR::activate();
  1350. }
  1351. void CBattleHex::deactivate()
  1352. {
  1353. Hoverable::deactivate();
  1354. MotionInterested::deactivate();
  1355. ClickableL::deactivate();
  1356. ClickableR::deactivate();
  1357. }
  1358. void CBattleHex::hover(bool on)
  1359. {
  1360. hovered = on;
  1361. Hoverable::hover(on);
  1362. if(!on && setAlterText)
  1363. {
  1364. myInterface->console->alterTxt = std::string();
  1365. setAlterText = false;
  1366. }
  1367. }
  1368. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1369. {
  1370. }
  1371. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  1372. {
  1373. if(myInterface->cellShade)
  1374. {
  1375. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1376. {
  1377. strictHovered = false;
  1378. }
  1379. else //hovered pixel is inside hex
  1380. {
  1381. strictHovered = true;
  1382. if(myInterface->activeStack>=0)
  1383. {
  1384. if(std::find(myInterface->shadedHexes.begin(),myInterface->shadedHexes.end(),myNumber) == myInterface->shadedHexes.end())
  1385. {
  1386. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  1387. if(shere)
  1388. {
  1389. if(shere->owner == LOCPLINT->playerID) //our stack
  1390. CGI->curh->changeGraphic(1,5);
  1391. else if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isShooting()) //we can shoot enemy
  1392. CGI->curh->changeGraphic(1,3);
  1393. else //unavailable enemy
  1394. CGI->curh->changeGraphic(1,0);
  1395. }
  1396. else //empty unavailable tile
  1397. CGI->curh->changeGraphic(1,0);
  1398. }
  1399. else //available tile
  1400. {
  1401. if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isFlying())
  1402. CGI->curh->changeGraphic(1,2);
  1403. else
  1404. CGI->curh->changeGraphic(1,1);
  1405. }
  1406. }
  1407. }
  1408. }
  1409. if(hovered && strictHovered) //print attacked creature to console
  1410. {
  1411. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1412. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1413. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1414. {
  1415. char tabh[160];
  1416. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1417. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1418. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1419. myInterface->console->alterTxt = std::string(tabh);
  1420. setAlterText = true;
  1421. }
  1422. }
  1423. else if(setAlterText)
  1424. {
  1425. myInterface->console->alterTxt = std::string();
  1426. setAlterText = false;
  1427. }
  1428. }
  1429. void CBattleHex::clickLeft(boost::logic::tribool down)
  1430. {
  1431. if(!down && hovered && strictHovered) //we've been really clicked!
  1432. {
  1433. myInterface->hexLclicked(myNumber);
  1434. }
  1435. }
  1436. void CBattleHex::clickRight(boost::logic::tribool down)
  1437. {
  1438. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1439. if(hovered && strictHovered && stID!=-1)
  1440. {
  1441. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1442. if(!myst.alive()) return;
  1443. StackState *pom = NULL;
  1444. if(down)
  1445. {
  1446. pom = new StackState();
  1447. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1448. if(h)
  1449. {
  1450. pom->attackBonus = h->primSkills[0];
  1451. pom->defenseBonus = h->primSkills[1];
  1452. pom->luck = h->getCurrentLuck();
  1453. pom->morale = h->getCurrentMorale();
  1454. pom->currentHealth = myst.firstHPleft;
  1455. }
  1456. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1457. ->activate();
  1458. }
  1459. delete pom;
  1460. }
  1461. }
  1462. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1463. {
  1464. }
  1465. CBattleConsole::~CBattleConsole()
  1466. {
  1467. texts.clear();
  1468. }
  1469. void CBattleConsole::show(SDL_Surface * to)
  1470. {
  1471. if(alterTxt.size())
  1472. {
  1473. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1474. }
  1475. else if(texts.size())
  1476. {
  1477. if(texts.size()==1)
  1478. {
  1479. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1480. }
  1481. else
  1482. {
  1483. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1484. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1485. }
  1486. }
  1487. }
  1488. bool CBattleConsole::addText(std::string text)
  1489. {
  1490. if(text.size()>70)
  1491. return false; //text too long!
  1492. int firstInToken = 0;
  1493. for(int i=0; i<text.size(); ++i) //tokenize
  1494. {
  1495. if(text[i] == 10)
  1496. {
  1497. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1498. firstInToken = i+1;
  1499. }
  1500. }
  1501. texts.push_back( text.substr(firstInToken, text.size()) );
  1502. lastShown = texts.size()-1;
  1503. return true;
  1504. }
  1505. void CBattleConsole::eraseText(unsigned int pos)
  1506. {
  1507. if(pos < texts.size())
  1508. {
  1509. texts.erase(texts.begin() + pos);
  1510. if(lastShown == texts.size())
  1511. --lastShown;
  1512. }
  1513. }
  1514. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1515. {
  1516. if(pos >= texts.size()) //no such pos
  1517. return;
  1518. texts[pos] = text;
  1519. }
  1520. void CBattleConsole::scrollUp(unsigned int by)
  1521. {
  1522. if(lastShown > by)
  1523. lastShown -= by;
  1524. }
  1525. void CBattleConsole::scrollDown(unsigned int by)
  1526. {
  1527. if(lastShown + by < texts.size())
  1528. lastShown += by;
  1529. }
  1530. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, SDL_Rect & pos, const CBattleInterface * owner)
  1531. {
  1532. this->pos = pos;
  1533. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1534. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1535. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1536. SDL_FreeSurface(background);
  1537. background = pom;
  1538. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1539. if(br.winner==0) //attacker won
  1540. {
  1541. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1542. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1543. }
  1544. else //if(br.winner==1)
  1545. {
  1546. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1547. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1548. }
  1549. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1550. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1551. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1552. std::string attackerName, defenderName;
  1553. if(owner->attackingHeroInstance) //a hero attacked
  1554. {
  1555. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1556. //setting attackerName
  1557. attackerName = owner->attackingHeroInstance->name;
  1558. }
  1559. else //a monster attacked
  1560. {
  1561. int bestMonsterID = -1;
  1562. int bestPower = 0;
  1563. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1564. {
  1565. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1566. {
  1567. bestPower = CGI->creh->creatures[it->first].AIValue;
  1568. bestMonsterID = it->first;
  1569. }
  1570. }
  1571. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1572. //setting attackerName
  1573. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1574. }
  1575. if(owner->defendingHeroInstance) //a hero defended
  1576. {
  1577. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1578. //setting defenderName
  1579. defenderName = owner->defendingHeroInstance->name;
  1580. }
  1581. else //a monster defended
  1582. {
  1583. int bestMonsterID = -1;
  1584. int bestPower = 0;
  1585. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1586. {
  1587. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1588. {
  1589. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1590. bestMonsterID = it->second.first;
  1591. }
  1592. }
  1593. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1594. //setting defenderName
  1595. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1596. }
  1597. //printing attacker and defender's names
  1598. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1599. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1600. //printing casualities
  1601. for(int step = 0; step < 2; ++step)
  1602. {
  1603. if(br.casualties[step].size()==0)
  1604. {
  1605. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  1606. }
  1607. else
  1608. {
  1609. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  1610. int yPos = 344 + step*97;
  1611. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  1612. {
  1613. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1614. std::stringstream amount;
  1615. amount<<it->second;
  1616. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1617. xPos += 42;
  1618. }
  1619. }
  1620. }
  1621. //printing result description
  1622. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  1623. switch(br.result)
  1624. {
  1625. case 0: //normal victory
  1626. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1627. {
  1628. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  1629. }
  1630. else
  1631. {
  1632. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  1633. }
  1634. break;
  1635. case 1: //flee
  1636. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1637. {
  1638. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  1639. }
  1640. else
  1641. {
  1642. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  1643. }
  1644. break;
  1645. case 2: //surrender
  1646. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1647. {
  1648. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  1649. }
  1650. else
  1651. {
  1652. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  1653. }
  1654. break;
  1655. }
  1656. }
  1657. CBattleReslutWindow::~CBattleReslutWindow()
  1658. {
  1659. SDL_FreeSurface(background);
  1660. }
  1661. void CBattleReslutWindow::activate()
  1662. {
  1663. exit->activate();
  1664. }
  1665. void CBattleReslutWindow::deactivate()
  1666. {
  1667. exit->deactivate();
  1668. }
  1669. void CBattleReslutWindow::show(SDL_Surface *to)
  1670. {
  1671. //evaluating to
  1672. if(!to)
  1673. to = screen;
  1674. SDL_BlitSurface(background, NULL, to, &pos);
  1675. exit->show(to);
  1676. }
  1677. void CBattleReslutWindow::bExitf()
  1678. {
  1679. LOCPLINT->battleResultQuited();
  1680. }
  1681. CBattleOptionsWindow::CBattleOptionsWindow(SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  1682. {
  1683. pos = position;
  1684. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  1685. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1686. check = CDefHandler::giveDef("SYSOPCHK.DEF");
  1687. setToDefault = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def", false, NULL, false);
  1688. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  1689. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def", false, NULL, false);
  1690. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  1691. //printing texts to background
  1692. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  1693. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  1694. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  1695. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  1696. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  1697. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  1698. //auto - combat options
  1699. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  1700. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  1701. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  1702. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  1703. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  1704. //creature info
  1705. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  1706. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  1707. //general options
  1708. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  1709. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  1710. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  1711. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  1712. //texts printed
  1713. }
  1714. CBattleOptionsWindow::~CBattleOptionsWindow()
  1715. {
  1716. SDL_FreeSurface(background);
  1717. delete setToDefault;
  1718. delete exit;
  1719. delete check;
  1720. }
  1721. void CBattleOptionsWindow::activate()
  1722. {
  1723. setToDefault->activate();
  1724. exit->activate();
  1725. }
  1726. void CBattleOptionsWindow::deactivate()
  1727. {
  1728. setToDefault->deactivate();
  1729. exit->deactivate();
  1730. }
  1731. void CBattleOptionsWindow::show(SDL_Surface *to)
  1732. {
  1733. if(!to) //"evaluating" to
  1734. to = screen;
  1735. SDL_BlitSurface(background, NULL, to, &pos);
  1736. setToDefault->show(to);
  1737. exit->show(to);
  1738. }
  1739. void CBattleOptionsWindow::bDefaultf()
  1740. {
  1741. }
  1742. void CBattleOptionsWindow::bExitf()
  1743. {
  1744. deactivate();
  1745. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  1746. {
  1747. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  1748. {
  1749. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  1750. break;
  1751. }
  1752. }
  1753. delete this;
  1754. LOCPLINT->curint->activate();
  1755. }