CBattleInterface.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  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 "hch/CPreGameTextHandler.h"
  15. #include "client/CCreatureAnimation.h"
  16. #include "client/Graphics.h"
  17. #include "client/CSpellWindow.h"
  18. #include <queue>
  19. #include <sstream>
  20. #include "lib/CondSh.h"
  21. #include "lib/NetPacks.h"
  22. #include <boost/assign/list_of.hpp>
  23. #ifndef __GNUC__
  24. const double M_PI = 3.14159265358979323846;
  25. #else
  26. #define _USE_MATH_DEFINES
  27. #include <cmath>
  28. #endif
  29. extern SDL_Surface * screen;
  30. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  31. extern SDL_Color zwykly;
  32. class CMP_stack2
  33. {
  34. public:
  35. bool operator ()(const CStack& a, const CStack& b)
  36. {
  37. return (a.creature->speed)>(b.creature->speed);
  38. }
  39. } cmpst2 ;
  40. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  41. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL), attackingInfo(NULL), myTurn(false), resWindow(NULL), showStackQueue(false), animSpeed(2), printStackRange(true)
  42. {
  43. givenCommand = new CondSh<BattleAction *>(NULL);
  44. //initializing armies
  45. this->army1 = army1;
  46. this->army2 = army2;
  47. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  48. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  49. {
  50. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  51. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  52. creAnims[b->second.ID]->setType(2);
  53. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  54. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  55. }
  56. //preparing menu background and terrain
  57. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  58. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  59. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  60. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  61. //preparing graphics for displaying amounts of creatures
  62. amountBasic = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  63. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  64. CSDL_Ext::alphaTransform(amountNormal);
  65. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  66. {
  67. if((amountNormal->format->palette->colors+g)->b != 132 &&
  68. (amountNormal->format->palette->colors+g)->g != 231 &&
  69. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  70. {
  71. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  72. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  73. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  74. }
  75. }
  76. ////blitting menu background and terrain
  77. blitAt(background, 0, 0);
  78. blitAt(menu, 0, 556);
  79. CSDL_Ext::update();
  80. //preparing buttons and console
  81. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  82. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  83. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  84. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  85. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  86. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  87. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  88. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  89. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", false, NULL, false);
  90. bConsoleDown->bitmapOffset = 2;
  91. console = new CBattleConsole();
  92. console->pos.x = 211;
  93. console->pos.y = 560;
  94. console->pos.w = 406;
  95. console->pos.h = 38;
  96. //loading hero animations
  97. if(hero1) // attacking hero
  98. {
  99. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner, hero1->tempOwner == LOCPLINT->playerID ? hero1 : NULL);
  100. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  101. }
  102. else
  103. {
  104. attackingHero = NULL;
  105. }
  106. if(hero2) // defending hero
  107. {
  108. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner, hero2->tempOwner == LOCPLINT->playerID ? hero2 : NULL);
  109. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  110. }
  111. else
  112. {
  113. defendingHero = NULL;
  114. }
  115. //preparing cells and hexes
  116. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  117. CSDL_Ext::alphaTransform(cellBorder);
  118. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  119. CSDL_Ext::alphaTransform(cellShade);
  120. for(int h=0; h<187; ++h)
  121. {
  122. bfield[h].myNumber = h;
  123. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  124. int y = 86 + 42 * (h/17);
  125. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  126. bfield[h].accesible = true;
  127. bfield[h].myInterface = this;
  128. }
  129. //locking occupied positions on batlefield
  130. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  131. {
  132. bfield[it->second.position].accesible = false;
  133. }
  134. //loading projectiles for units
  135. for(std::map<int, CStack>::iterator g = stacks.begin(); g != stacks.end(); ++g)
  136. {
  137. if(g->second.creature->isShooting() && CGI->creh->idToProjectile[g->second.creature->idNumber] != std::string())
  138. {
  139. idToProjectile[g->second.creature->idNumber] = CDefHandler::giveDef(CGI->creh->idToProjectile[g->second.creature->idNumber]);
  140. if(idToProjectile[g->second.creature->idNumber]->ourImages.size() > 2) //add symmetric images
  141. {
  142. for(int k = idToProjectile[g->second.creature->idNumber]->ourImages.size()-2; k > 1; --k)
  143. {
  144. Cimage ci;
  145. ci.bitmap = CSDL_Ext::rotate01(idToProjectile[g->second.creature->idNumber]->ourImages[k].bitmap);
  146. ci.groupNumber = 0;
  147. ci.imName = std::string();
  148. idToProjectile[g->second.creature->idNumber]->ourImages.push_back(ci);
  149. }
  150. }
  151. for(int s=0; s<idToProjectile[g->second.creature->idNumber]->ourImages.size(); ++s) //alpha transforming
  152. {
  153. CSDL_Ext::alphaTransform(idToProjectile[g->second.creature->idNumber]->ourImages[s].bitmap);
  154. }
  155. }
  156. }
  157. //prepairing graphic with cell borders
  158. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  159. //copying palette
  160. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  161. {
  162. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  163. }
  164. //palette copied
  165. for(int i=0; i<11; ++i) //rows
  166. {
  167. for(int j=0; j<15; ++j) //columns
  168. {
  169. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  170. int y = 86 + 42 * i;
  171. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  172. {
  173. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  174. {
  175. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  176. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  177. }
  178. }
  179. }
  180. }
  181. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  182. }
  183. CBattleInterface::~CBattleInterface()
  184. {
  185. SDL_FreeSurface(background);
  186. SDL_FreeSurface(menu);
  187. SDL_FreeSurface(amountBasic);
  188. SDL_FreeSurface(amountNormal);
  189. SDL_FreeSurface(cellBorders);
  190. SDL_FreeSurface(backgroundWithHexes);
  191. delete bOptions;
  192. delete bSurrender;
  193. delete bFlee;
  194. delete bAutofight;
  195. delete bSpell;
  196. delete bWait;
  197. delete bDefence;
  198. delete bConsoleUp;
  199. delete bConsoleDown;
  200. delete console;
  201. delete resWindow;
  202. delete givenCommand;
  203. delete attackingHero;
  204. delete defendingHero;
  205. SDL_FreeSurface(cellBorder);
  206. SDL_FreeSurface(cellShade);
  207. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  208. delete g->second;
  209. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  210. delete g->second;
  211. }
  212. void CBattleInterface::setPrintCellBorders(bool set)
  213. {
  214. printCellBorders = set;
  215. redrawBackgroundWithHexes(activeStack);
  216. }
  217. void CBattleInterface::setPrintStackRange(bool set)
  218. {
  219. printStackRange = set;
  220. redrawBackgroundWithHexes(activeStack);
  221. }
  222. void CBattleInterface::setPrintMouseShadow(bool set)
  223. {
  224. printMouseShadow = set;
  225. }
  226. void CBattleInterface::activate()
  227. {
  228. subInt = NULL;
  229. bOptions->activate();
  230. bSurrender->activate();
  231. bFlee->activate();
  232. bAutofight->activate();
  233. bSpell->activate();
  234. bWait->activate();
  235. bDefence->activate();
  236. bConsoleUp->activate();
  237. bConsoleDown->activate();
  238. for(int b=0; b<187; ++b)
  239. {
  240. bfield[b].activate();
  241. }
  242. if(attackingHero)
  243. attackingHero->activate();
  244. if(defendingHero)
  245. defendingHero->activate();
  246. }
  247. void CBattleInterface::deactivate()
  248. {
  249. bOptions->deactivate();
  250. bSurrender->deactivate();
  251. bFlee->deactivate();
  252. bAutofight->deactivate();
  253. bSpell->deactivate();
  254. bWait->deactivate();
  255. bDefence->deactivate();
  256. bConsoleUp->deactivate();
  257. bConsoleDown->deactivate();
  258. for(int b=0; b<187; ++b)
  259. {
  260. bfield[b].deactivate();
  261. }
  262. if(attackingHero)
  263. attackingHero->deactivate();
  264. if(defendingHero)
  265. defendingHero->deactivate();
  266. }
  267. void CBattleInterface::show(SDL_Surface * to)
  268. {
  269. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  270. ++animCount;
  271. if(!to) //"evaluating" to
  272. to = screen;
  273. //printing background and hexes
  274. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //show everything with range
  275. {
  276. blitAt(backgroundWithHexes, 0, 0, to);
  277. }
  278. else
  279. {
  280. //showing background
  281. blitAt(background, 0, 0, to);
  282. if(printCellBorders)
  283. {
  284. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, NULL);
  285. }
  286. }
  287. //printing hovered cell
  288. if(printMouseShadow)
  289. {
  290. for(int b=0; b<187; ++b)
  291. {
  292. if(bfield[b].strictHovered && bfield[b].hovered)
  293. {
  294. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  295. int y = 86 + 42 * (b/17);
  296. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  297. }
  298. }
  299. }
  300. //showing menu background and console
  301. blitAt(menu, 0, 556, to);
  302. console->show(to);
  303. //showing buttons
  304. bOptions->show(to);
  305. bSurrender->show(to);
  306. bFlee->show(to);
  307. bAutofight->show(to);
  308. bSpell->show(to);
  309. bWait->show(to);
  310. bDefence->show(to);
  311. bConsoleUp->show(to);
  312. bConsoleDown->show(to);
  313. //showing hero animations
  314. if(attackingHero)
  315. attackingHero->show(to);
  316. if(defendingHero)
  317. defendingHero->show(to);
  318. ////showing units //a lot of work...
  319. std::vector<int> stackAliveByHex[187];
  320. //double loop because dead stacks should be printed first
  321. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  322. {
  323. if(j->second.alive())
  324. stackAliveByHex[j->second.position].push_back(j->second.ID);
  325. }
  326. std::vector<int> stackDeadByHex[187];
  327. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  328. {
  329. if(!j->second.alive())
  330. stackDeadByHex[j->second.position].push_back(j->second.ID);
  331. }
  332. attackingShowHelper(); // handle attack animation
  333. for(int b=0; b<187; ++b) //showing dead stacks
  334. {
  335. for(int v=0; v<stackDeadByHex[b].size(); ++v)
  336. {
  337. 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
  338. //printing amount
  339. if(stacks[stackDeadByHex[b][v]].amount > 0) //don't print if stack is not alive
  340. {
  341. int xAdd = stacks[stackDeadByHex[b][v]].attackerOwned ? 220 : 202;
  342. 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));
  343. std::stringstream ss;
  344. ss<<stacks[stackDeadByHex[b][v]].amount;
  345. 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);
  346. }
  347. }
  348. }
  349. for(int b=0; b<187; ++b) //showing alive stacks
  350. {
  351. for(int v=0; v<stackAliveByHex[b].size(); ++v)
  352. {
  353. 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
  354. //printing amount
  355. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  356. {
  357. int xAdd = stacks[stackAliveByHex[b][v]].attackerOwned ? 220 : 202;
  358. 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));
  359. std::stringstream ss;
  360. ss<<stacks[stackAliveByHex[b][v]].amount;
  361. 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);
  362. }
  363. }
  364. }
  365. //units shown
  366. projectileShowHelper(to);//showing projectiles
  367. //showing queue of stacks
  368. if(showStackQueue)
  369. {
  370. int xPos = 400 - ( stacks.size() * 42 )/2;
  371. int yPos = 10;
  372. std::vector<CStack> stacksSorted;
  373. for(int v=0; v<stacks.size(); ++v)
  374. {
  375. if(stacks[v].alive()) //we don't want dead stacks to be there
  376. stacksSorted.push_back(stacks[v]);
  377. }
  378. std::stable_sort(stacksSorted.begin(), stacksSorted.end(), cmpst2);
  379. int startFrom = -1;
  380. for(int n=0; n<stacksSorted.size(); ++n)
  381. {
  382. if(stacksSorted[n].ID == activeStack)
  383. {
  384. startFrom = n;
  385. break;
  386. }
  387. }
  388. if(startFrom != -1)
  389. {
  390. for(int b=startFrom; b<stacksSorted.size()+startFrom; ++b)
  391. {
  392. SDL_BlitSurface(graphics->smallImgs[-2], NULL, to, &genRect(32, 32, xPos, yPos));
  393. //printing colored border
  394. for(int xFrom = xPos-1; xFrom<xPos+33; ++xFrom)
  395. {
  396. for(int yFrom = yPos-1; yFrom<yPos+33; ++yFrom)
  397. {
  398. if(xFrom == xPos-1 || xFrom == xPos+32 || yFrom == yPos-1 || yFrom == yPos+32)
  399. {
  400. SDL_Color pc;
  401. if(stacksSorted[b % stacksSorted.size()].owner != 255)
  402. {
  403. pc = graphics->playerColors[stacksSorted[b % stacksSorted.size()].owner];
  404. }
  405. else
  406. {
  407. pc = *graphics->neutralColor;
  408. }
  409. CSDL_Ext::SDL_PutPixel(to, xFrom, yFrom, pc.r, pc.g, pc.b);
  410. }
  411. }
  412. }
  413. //colored border printed
  414. SDL_BlitSurface(graphics->smallImgs[stacksSorted[b % stacksSorted.size()].creature->idNumber], NULL, to, &genRect(32, 32, xPos, yPos));
  415. xPos += 42;
  416. }
  417. }
  418. }
  419. //showing window with result of battle
  420. if(resWindow)
  421. {
  422. resWindow->show(to);
  423. }
  424. }
  425. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  426. {
  427. if(creAnims[number]==NULL)
  428. return false; //there is no such creature
  429. creAnims[number]->setType(8);
  430. int firstFrame = creAnims[number]->getFrame();
  431. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(8) + firstFrame - 1; ++g)
  432. {
  433. show();
  434. CSDL_Ext::update();
  435. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  436. if((animCount+1)%(4/animSpeed)==0)
  437. creAnims[number]->incrementFrame();
  438. }
  439. creDir[number] = !creDir[number];
  440. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  441. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  442. creAnims[number]->pos.x = coords.first;
  443. //creAnims[number]->pos.y = coords.second;
  444. if(wideTrick && curs.creature->isDoubleWide())
  445. {
  446. if(!creDir[number])
  447. creAnims[number]->pos.x -= 44;
  448. }
  449. creAnims[number]->setType(7);
  450. firstFrame = creAnims[number]->getFrame();
  451. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(7) + firstFrame - 1; ++g)
  452. {
  453. show();
  454. CSDL_Ext::update();
  455. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  456. }
  457. creAnims[number]->setType(2);
  458. return true;
  459. }
  460. void CBattleInterface::bOptionsf()
  461. {
  462. CGI->curh->changeGraphic(0,0);
  463. LOCPLINT->curint->deactivate();
  464. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  465. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  466. optionsWin->activate();
  467. LOCPLINT->objsToBlit.push_back(optionsWin);
  468. }
  469. void CBattleInterface::bSurrenderf()
  470. {
  471. }
  472. void CBattleInterface::bFleef()
  473. {
  474. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  475. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  476. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  477. }
  478. void CBattleInterface::reallyFlee()
  479. {
  480. giveCommand(4,0,0);
  481. CGI->curh->changeGraphic(0, 0);
  482. }
  483. void CBattleInterface::bAutofightf()
  484. {
  485. }
  486. void CBattleInterface::bSpellf()
  487. {
  488. CGI->curh->changeGraphic(0,0);
  489. LOCPLINT->curint->deactivate();
  490. const CGHeroInstance * chi = NULL;
  491. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  492. chi = attackingHeroInstance;
  493. else
  494. chi = defendingHeroInstance;
  495. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), chi);
  496. spellWindow->activate();
  497. LOCPLINT->objsToBlit.push_back(spellWindow);
  498. }
  499. void CBattleInterface::bWaitf()
  500. {
  501. }
  502. void CBattleInterface::bDefencef()
  503. {
  504. giveCommand(3,0,activeStack);
  505. }
  506. void CBattleInterface::bConsoleUpf()
  507. {
  508. console->scrollUp();
  509. }
  510. void CBattleInterface::bConsoleDownf()
  511. {
  512. console->scrollDown();
  513. }
  514. void CBattleInterface::newStack(CStack stack)
  515. {
  516. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  517. creAnims[stack.ID]->setType(2);
  518. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  519. }
  520. void CBattleInterface::stackRemoved(CStack stack)
  521. {
  522. delete creAnims[stack.ID];
  523. creAnims.erase(stack.ID);
  524. }
  525. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  526. {
  527. if(creAnims[ID]->getType() != 2)
  528. {
  529. return; //something went wrong
  530. }
  531. if(byShooting) //delay hit animation
  532. {
  533. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  534. while(true)
  535. {
  536. bool found = false;
  537. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  538. {
  539. if(it->creID == attacker.creature->idNumber)
  540. {
  541. found = true;
  542. break;
  543. }
  544. }
  545. if(!found)
  546. break;
  547. else
  548. {
  549. show();
  550. CSDL_Ext::update();
  551. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  552. }
  553. }
  554. }
  555. creAnims[ID]->setType(5); //death
  556. int firstFrame = creAnims[ID]->getFrame();
  557. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(5) + firstFrame - 1; ++i)
  558. {
  559. if((animCount%(4/animSpeed))==0)
  560. creAnims[ID]->incrementFrame();
  561. show();
  562. CSDL_Ext::update();
  563. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  564. }
  565. printConsoleAttacked(ID, dmg, killed, IDby);
  566. }
  567. void CBattleInterface::stackActivated(int number)
  568. {
  569. //givenCommand = NULL;
  570. activeStack = number;
  571. myTurn = true;
  572. redrawBackgroundWithHexes(number);
  573. }
  574. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  575. {
  576. //a few useful variables
  577. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  578. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  579. int hexWbase = 44, hexHbase = 42;
  580. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  581. if(startMoving && creAnims[number]->framesInGroup(20)!=0) //animation of starting move; some units don't have this animation (ie. halberdier)
  582. {
  583. deactivate();
  584. CGI->curh->hide();
  585. creAnims[number]->setType(20);
  586. //LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  587. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  588. {
  589. show();
  590. CSDL_Ext::update();
  591. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  592. if((animCount+1)%(4/animSpeed)==0)
  593. creAnims[number]->incrementFrame();
  594. }
  595. }
  596. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  597. {
  598. switch(mutPos) //reverse unit if necessary
  599. {
  600. case 0: case 4: case 5:
  601. if(creDir[number] == true)
  602. reverseCreature(number, curStackPos, twoTiles);
  603. break;
  604. case 1: case 2: case 3:
  605. if(creDir[number] == false)
  606. reverseCreature(number, curStackPos, twoTiles);
  607. break;
  608. }
  609. //moving instructions
  610. creAnims[number]->setType(0);
  611. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  612. for(int i=0; i<steps; ++i)
  613. {
  614. switch(mutPos)
  615. {
  616. case 0:
  617. posX -= ((float)hexWbase)/(2.0f*steps);
  618. creAnims[number]->pos.x = posX;
  619. posY -= ((float)hexHbase)/((float)steps);
  620. creAnims[number]->pos.y = posY;
  621. break;
  622. case 1:
  623. posX += ((float)hexWbase)/(2.0f*steps);
  624. creAnims[number]->pos.x = posX;
  625. posY -= ((float)hexHbase)/((float)steps);
  626. creAnims[number]->pos.y = posY;
  627. break;
  628. case 2:
  629. posX += ((float)hexWbase)/((float)steps);
  630. creAnims[number]->pos.x = posX;
  631. break;
  632. case 3:
  633. posX += ((float)hexWbase)/(2.0f*steps);
  634. creAnims[number]->pos.x = posX;
  635. posY += ((float)hexHbase)/((float)steps);
  636. creAnims[number]->pos.y = posY;
  637. break;
  638. case 4:
  639. posX -= ((float)hexWbase)/(2.0f*steps);
  640. creAnims[number]->pos.x = posX;
  641. posY += ((float)hexHbase)/((float)steps);
  642. creAnims[number]->pos.y = posY;
  643. break;
  644. case 5:
  645. posX -= ((float)hexWbase)/((float)steps);
  646. creAnims[number]->pos.x = posX;
  647. break;
  648. }
  649. show();
  650. CSDL_Ext::update();
  651. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  652. if((animCount+1)%(4/animSpeed)==0)
  653. creAnims[number]->incrementFrame();
  654. }
  655. }
  656. if(endMoving) //animation of ending move
  657. {
  658. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  659. {
  660. creAnims[number]->setType(21);
  661. for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  662. {
  663. show();
  664. CSDL_Ext::update();
  665. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  666. if((animCount+1)%(4/animSpeed)==0)
  667. creAnims[number]->incrementFrame();
  668. }
  669. }
  670. creAnims[number]->setType(2); //resetting to default
  671. activate();
  672. CGI->curh->show();
  673. }
  674. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  675. if(endMoving) //resetting to default
  676. {
  677. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  678. reverseCreature(number, destHex, twoTiles);
  679. }
  680. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  681. creAnims[number]->pos.x = coords.first;
  682. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  683. creAnims[number]->pos.x -= 44;
  684. creAnims[number]->pos.y = coords.second;
  685. }
  686. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  687. {
  688. while(creAnims[ID]->getType() != 2 || (attackingInfo && attackingInfo->IDby == IDby))
  689. {
  690. show();
  691. CSDL_Ext::update();
  692. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  693. }
  694. if(byShooting) //delay hit animation
  695. {
  696. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  697. while(true)
  698. {
  699. bool found = false;
  700. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  701. {
  702. if(it->creID == attacker.creature->idNumber)
  703. {
  704. found = true;
  705. break;
  706. }
  707. }
  708. if(!found)
  709. break;
  710. else
  711. {
  712. show();
  713. CSDL_Ext::update();
  714. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  715. }
  716. }
  717. }
  718. creAnims[ID]->setType(3); //getting hit
  719. int firstFrame = creAnims[ID]->getFrame();
  720. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(3) + firstFrame - 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::setAnimSpeed(int set)
  988. {
  989. animSpeed = set;
  990. }
  991. int CBattleInterface::getAnimSpeed() const
  992. {
  993. return animSpeed;
  994. }
  995. float CBattleInterface::getAnimSpeedMultiplier() const
  996. {
  997. switch(animSpeed)
  998. {
  999. case 1:
  1000. return 3.5f;
  1001. case 2:
  1002. return 2.2f;
  1003. case 4:
  1004. return 1.0f;
  1005. }
  1006. }
  1007. void CBattleInterface::attackingShowHelper()
  1008. {
  1009. if(attackingInfo && !attackingInfo->reversing)
  1010. {
  1011. if(attackingInfo->frame == 0)
  1012. {
  1013. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1014. if(attackingInfo->shooting)
  1015. {
  1016. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1017. }
  1018. else
  1019. {
  1020. if(aStack.creature->isDoubleWide())
  1021. {
  1022. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1023. {
  1024. case 0:
  1025. creAnims[attackingInfo->ID]->setType(11);
  1026. break;
  1027. case 1:
  1028. creAnims[attackingInfo->ID]->setType(11);
  1029. break;
  1030. case 2:
  1031. creAnims[attackingInfo->ID]->setType(12);
  1032. break;
  1033. case 3:
  1034. creAnims[attackingInfo->ID]->setType(13);
  1035. break;
  1036. case 4:
  1037. creAnims[attackingInfo->ID]->setType(13);
  1038. break;
  1039. case 5:
  1040. creAnims[attackingInfo->ID]->setType(12);
  1041. break;
  1042. }
  1043. }
  1044. else //else for if(aStack.creature->isDoubleWide())
  1045. {
  1046. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1047. {
  1048. case 0:
  1049. creAnims[attackingInfo->ID]->setType(11);
  1050. break;
  1051. case 1:
  1052. creAnims[attackingInfo->ID]->setType(11);
  1053. break;
  1054. case 2:
  1055. creAnims[attackingInfo->ID]->setType(12);
  1056. break;
  1057. case 3:
  1058. creAnims[attackingInfo->ID]->setType(13);
  1059. break;
  1060. case 4:
  1061. creAnims[attackingInfo->ID]->setType(13);
  1062. break;
  1063. case 5:
  1064. creAnims[attackingInfo->ID]->setType(12);
  1065. break;
  1066. }
  1067. }
  1068. }
  1069. }
  1070. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1071. {
  1072. attackingInfo->reversing = true;
  1073. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1074. if(aStackp == NULL)
  1075. return;
  1076. CStack aStack = *aStackp;
  1077. 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. break;
  1090. case 3:
  1091. break;
  1092. case 4:
  1093. //reverseCreature(ID, aStack.position, true);
  1094. break;
  1095. case 5:
  1096. reverseCreature(attackingInfo->ID, aStack.position, true);
  1097. break;
  1098. }
  1099. }
  1100. else //else for if(aStack.creature->isDoubleWide())
  1101. {
  1102. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1103. {
  1104. case 0:
  1105. reverseCreature(attackingInfo->ID, aStack.position, true);
  1106. break;
  1107. case 1:
  1108. break;
  1109. case 2:
  1110. break;
  1111. case 3:
  1112. break;
  1113. case 4:
  1114. reverseCreature(attackingInfo->ID, aStack.position, true);
  1115. break;
  1116. case 5:
  1117. reverseCreature(attackingInfo->ID, aStack.position, true);
  1118. break;
  1119. }
  1120. }
  1121. }
  1122. else //if(aStack.attackerOwned)
  1123. {
  1124. if(aStack.creature->isDoubleWide())
  1125. {
  1126. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1127. {
  1128. case 0:
  1129. //reverseCreature(ID, aStack.position, true);
  1130. break;
  1131. case 1:
  1132. break;
  1133. case 2:
  1134. reverseCreature(attackingInfo->ID, aStack.position, true);
  1135. break;
  1136. case 3:
  1137. break;
  1138. case 4:
  1139. //reverseCreature(ID, aStack.position, true);
  1140. break;
  1141. case 5:
  1142. //reverseCreature(ID, aStack.position, true);
  1143. break;
  1144. }
  1145. }
  1146. else //else for if(aStack.creature->isDoubleWide())
  1147. {
  1148. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1149. {
  1150. case 0:
  1151. //reverseCreature(ID, aStack.position, true);
  1152. break;
  1153. case 1:
  1154. reverseCreature(attackingInfo->ID, aStack.position, true);
  1155. break;
  1156. case 2:
  1157. reverseCreature(attackingInfo->ID, aStack.position, true);
  1158. break;
  1159. case 3:
  1160. reverseCreature(attackingInfo->ID, aStack.position, true);
  1161. break;
  1162. case 4:
  1163. //reverseCreature(ID, aStack.position, true);
  1164. break;
  1165. case 5:
  1166. //reverseCreature(ID, aStack.position, true);
  1167. break;
  1168. }
  1169. }
  1170. }
  1171. attackingInfo->reversing = false;
  1172. creAnims[attackingInfo->ID]->setType(2);
  1173. delete attackingInfo;
  1174. attackingInfo = NULL;
  1175. }
  1176. if(attackingInfo)
  1177. {
  1178. attackingInfo->hitCount++;
  1179. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1180. attackingInfo->frame++;
  1181. }
  1182. }
  1183. }
  1184. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1185. {
  1186. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1187. //preparating background graphic with hexes and shaded hexes
  1188. blitAt(background, 0, 0, backgroundWithHexes);
  1189. if(printCellBorders)
  1190. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1191. if(printStackRange)
  1192. {
  1193. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1194. {
  1195. int i = shadedHexes[m]/17; //row
  1196. int j = shadedHexes[m]%17-1; //column
  1197. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1198. int y = 86 + 42 * i;
  1199. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1200. }
  1201. }
  1202. }
  1203. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1204. {
  1205. char tabh[200];
  1206. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1207. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1208. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1209. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1210. dmg);
  1211. if(killed > 0)
  1212. {
  1213. if(killed > 1)
  1214. {
  1215. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1216. }
  1217. else //killed == 1
  1218. {
  1219. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1220. }
  1221. }
  1222. console->addText(std::string(tabh));
  1223. }
  1224. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1225. {
  1226. if(to == NULL)
  1227. to = screen;
  1228. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1229. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1230. {
  1231. if(it->animStartDelay>0)
  1232. {
  1233. --(it->animStartDelay);
  1234. continue;
  1235. }
  1236. SDL_Rect dst;
  1237. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1238. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1239. dst.x = it->x;
  1240. dst.y = it->y;
  1241. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1242. //actualizing projectile
  1243. ++it->step;
  1244. if(it->step == it->lastStep)
  1245. {
  1246. toBeDeleted.insert(toBeDeleted.end(), it);
  1247. }
  1248. else
  1249. {
  1250. it->x += it->dx;
  1251. it->y += it->dy;
  1252. if(it->spin)
  1253. {
  1254. ++(it->frameNum);
  1255. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1256. }
  1257. }
  1258. }
  1259. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1260. {
  1261. projectiles.erase(*it);
  1262. }
  1263. }
  1264. void CBattleHero::show(SDL_Surface *to)
  1265. {
  1266. //animation of flag
  1267. if(flip)
  1268. {
  1269. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1270. }
  1271. else
  1272. {
  1273. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1274. }
  1275. ++flagAnimCount;
  1276. if(flagAnimCount%4==0)
  1277. {
  1278. ++flagAnim;
  1279. flagAnim %= flag->ourImages.size();
  1280. }
  1281. //animation of hero
  1282. int tick=-1;
  1283. for(int i=0; i<dh->ourImages.size(); ++i)
  1284. {
  1285. if(dh->ourImages[i].groupNumber==phase)
  1286. ++tick;
  1287. if(tick==image)
  1288. {
  1289. SDL_Rect posb = pos;
  1290. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1291. ++image;
  1292. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1293. {
  1294. image = 0;
  1295. }
  1296. break;
  1297. }
  1298. }
  1299. }
  1300. void CBattleHero::activate()
  1301. {
  1302. ClickableL::activate();
  1303. }
  1304. void CBattleHero::deactivate()
  1305. {
  1306. ClickableL::deactivate();
  1307. }
  1308. void CBattleHero::clickLeft(boost::logic::tribool down)
  1309. {
  1310. if(!down && myHero)
  1311. {
  1312. CGI->curh->changeGraphic(0,0);
  1313. LOCPLINT->curint->deactivate();
  1314. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1315. spellWindow->activate();
  1316. LOCPLINT->objsToBlit.push_back(spellWindow);
  1317. }
  1318. }
  1319. 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)
  1320. {
  1321. dh = CDefHandler::giveDef( defName );
  1322. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1323. {
  1324. if(flip)
  1325. {
  1326. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1327. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1328. dh->ourImages[i].bitmap = hlp;
  1329. }
  1330. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1331. }
  1332. dh->alphaTransformed = true;
  1333. if(flip)
  1334. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1335. else
  1336. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1337. //coloring flag and adding transparency
  1338. for(int i=0; i<flag->ourImages.size(); ++i)
  1339. {
  1340. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1341. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1342. }
  1343. }
  1344. CBattleHero::~CBattleHero()
  1345. {
  1346. delete dh;
  1347. delete flag;
  1348. }
  1349. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1350. {
  1351. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1352. ret.second = -139 + 42 * (hexNum/17); //counting y
  1353. //counting x
  1354. if(attacker)
  1355. {
  1356. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1357. }
  1358. else
  1359. {
  1360. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1361. }
  1362. //shifting position for double - hex creatures
  1363. if(creature->isDoubleWide())
  1364. {
  1365. if(attacker)
  1366. {
  1367. ret.first -= 42;
  1368. }
  1369. else
  1370. {
  1371. ret.first += 42;
  1372. }
  1373. }
  1374. //returning
  1375. return ret;
  1376. }
  1377. void CBattleHex::activate()
  1378. {
  1379. Hoverable::activate();
  1380. MotionInterested::activate();
  1381. ClickableL::activate();
  1382. ClickableR::activate();
  1383. }
  1384. void CBattleHex::deactivate()
  1385. {
  1386. Hoverable::deactivate();
  1387. MotionInterested::deactivate();
  1388. ClickableL::deactivate();
  1389. ClickableR::deactivate();
  1390. }
  1391. void CBattleHex::hover(bool on)
  1392. {
  1393. hovered = on;
  1394. Hoverable::hover(on);
  1395. if(!on && setAlterText)
  1396. {
  1397. myInterface->console->alterTxt = std::string();
  1398. setAlterText = false;
  1399. }
  1400. }
  1401. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1402. {
  1403. }
  1404. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  1405. {
  1406. if(myInterface->cellShade)
  1407. {
  1408. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1409. {
  1410. strictHovered = false;
  1411. }
  1412. else //hovered pixel is inside hex
  1413. {
  1414. strictHovered = true;
  1415. if(myInterface->activeStack>=0)
  1416. {
  1417. if(std::find(myInterface->shadedHexes.begin(),myInterface->shadedHexes.end(),myNumber) == myInterface->shadedHexes.end())
  1418. {
  1419. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  1420. if(shere)
  1421. {
  1422. if(shere->owner == LOCPLINT->playerID) //our stack
  1423. CGI->curh->changeGraphic(1,5);
  1424. else if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isShooting()) //we can shoot enemy
  1425. CGI->curh->changeGraphic(1,3);
  1426. else //unavailable enemy
  1427. CGI->curh->changeGraphic(1,0);
  1428. }
  1429. else //empty unavailable tile
  1430. CGI->curh->changeGraphic(1,0);
  1431. }
  1432. else //available tile
  1433. {
  1434. if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isFlying())
  1435. CGI->curh->changeGraphic(1,2);
  1436. else
  1437. CGI->curh->changeGraphic(1,1);
  1438. }
  1439. }
  1440. }
  1441. }
  1442. if(hovered && strictHovered) //print attacked creature to console
  1443. {
  1444. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1445. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1446. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1447. {
  1448. char tabh[160];
  1449. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1450. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1451. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1452. myInterface->console->alterTxt = std::string(tabh);
  1453. setAlterText = true;
  1454. }
  1455. }
  1456. else if(setAlterText)
  1457. {
  1458. myInterface->console->alterTxt = std::string();
  1459. setAlterText = false;
  1460. }
  1461. }
  1462. void CBattleHex::clickLeft(boost::logic::tribool down)
  1463. {
  1464. if(!down && hovered && strictHovered) //we've been really clicked!
  1465. {
  1466. myInterface->hexLclicked(myNumber);
  1467. }
  1468. }
  1469. void CBattleHex::clickRight(boost::logic::tribool down)
  1470. {
  1471. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1472. if(hovered && strictHovered && stID!=-1)
  1473. {
  1474. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1475. if(!myst.alive()) return;
  1476. StackState *pom = NULL;
  1477. if(down)
  1478. {
  1479. pom = new StackState();
  1480. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1481. if(h)
  1482. {
  1483. pom->attackBonus = h->primSkills[0];
  1484. pom->defenseBonus = h->primSkills[1];
  1485. pom->luck = h->getCurrentLuck();
  1486. pom->morale = h->getCurrentMorale();
  1487. pom->currentHealth = myst.firstHPleft;
  1488. }
  1489. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1490. ->activate();
  1491. }
  1492. delete pom;
  1493. }
  1494. }
  1495. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1496. {
  1497. }
  1498. CBattleConsole::~CBattleConsole()
  1499. {
  1500. texts.clear();
  1501. }
  1502. void CBattleConsole::show(SDL_Surface * to)
  1503. {
  1504. if(alterTxt.size())
  1505. {
  1506. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1507. }
  1508. else if(texts.size())
  1509. {
  1510. if(texts.size()==1)
  1511. {
  1512. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1513. }
  1514. else
  1515. {
  1516. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1517. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1518. }
  1519. }
  1520. }
  1521. bool CBattleConsole::addText(std::string text)
  1522. {
  1523. if(text.size()>70)
  1524. return false; //text too long!
  1525. int firstInToken = 0;
  1526. for(int i=0; i<text.size(); ++i) //tokenize
  1527. {
  1528. if(text[i] == 10)
  1529. {
  1530. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1531. firstInToken = i+1;
  1532. }
  1533. }
  1534. texts.push_back( text.substr(firstInToken, text.size()) );
  1535. lastShown = texts.size()-1;
  1536. return true;
  1537. }
  1538. void CBattleConsole::eraseText(unsigned int pos)
  1539. {
  1540. if(pos < texts.size())
  1541. {
  1542. texts.erase(texts.begin() + pos);
  1543. if(lastShown == texts.size())
  1544. --lastShown;
  1545. }
  1546. }
  1547. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1548. {
  1549. if(pos >= texts.size()) //no such pos
  1550. return;
  1551. texts[pos] = text;
  1552. }
  1553. void CBattleConsole::scrollUp(unsigned int by)
  1554. {
  1555. if(lastShown > by)
  1556. lastShown -= by;
  1557. }
  1558. void CBattleConsole::scrollDown(unsigned int by)
  1559. {
  1560. if(lastShown + by < texts.size())
  1561. lastShown += by;
  1562. }
  1563. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1564. {
  1565. this->pos = pos;
  1566. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1567. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1568. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1569. SDL_FreeSurface(background);
  1570. background = pom;
  1571. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1572. if(br.winner==0) //attacker won
  1573. {
  1574. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1575. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1576. }
  1577. else //if(br.winner==1)
  1578. {
  1579. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1580. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1581. }
  1582. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1583. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1584. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1585. std::string attackerName, defenderName;
  1586. if(owner->attackingHeroInstance) //a hero attacked
  1587. {
  1588. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1589. //setting attackerName
  1590. attackerName = owner->attackingHeroInstance->name;
  1591. }
  1592. else //a monster attacked
  1593. {
  1594. int bestMonsterID = -1;
  1595. int bestPower = 0;
  1596. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1597. {
  1598. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1599. {
  1600. bestPower = CGI->creh->creatures[it->first].AIValue;
  1601. bestMonsterID = it->first;
  1602. }
  1603. }
  1604. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1605. //setting attackerName
  1606. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1607. }
  1608. if(owner->defendingHeroInstance) //a hero defended
  1609. {
  1610. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1611. //setting defenderName
  1612. defenderName = owner->defendingHeroInstance->name;
  1613. }
  1614. else //a monster defended
  1615. {
  1616. int bestMonsterID = -1;
  1617. int bestPower = 0;
  1618. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1619. {
  1620. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1621. {
  1622. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1623. bestMonsterID = it->second.first;
  1624. }
  1625. }
  1626. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1627. //setting defenderName
  1628. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1629. }
  1630. //printing attacker and defender's names
  1631. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1632. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1633. //printing casualities
  1634. for(int step = 0; step < 2; ++step)
  1635. {
  1636. if(br.casualties[step].size()==0)
  1637. {
  1638. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  1639. }
  1640. else
  1641. {
  1642. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  1643. int yPos = 344 + step*97;
  1644. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  1645. {
  1646. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1647. std::stringstream amount;
  1648. amount<<it->second;
  1649. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1650. xPos += 42;
  1651. }
  1652. }
  1653. }
  1654. //printing result description
  1655. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  1656. switch(br.result)
  1657. {
  1658. case 0: //normal victory
  1659. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1660. {
  1661. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  1662. }
  1663. else
  1664. {
  1665. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  1666. }
  1667. break;
  1668. case 1: //flee
  1669. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1670. {
  1671. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  1672. }
  1673. else
  1674. {
  1675. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  1676. }
  1677. break;
  1678. case 2: //surrender
  1679. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1680. {
  1681. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  1682. }
  1683. else
  1684. {
  1685. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  1686. }
  1687. break;
  1688. }
  1689. }
  1690. CBattleReslutWindow::~CBattleReslutWindow()
  1691. {
  1692. SDL_FreeSurface(background);
  1693. }
  1694. void CBattleReslutWindow::activate()
  1695. {
  1696. exit->activate();
  1697. }
  1698. void CBattleReslutWindow::deactivate()
  1699. {
  1700. exit->deactivate();
  1701. }
  1702. void CBattleReslutWindow::show(SDL_Surface *to)
  1703. {
  1704. //evaluating to
  1705. if(!to)
  1706. to = screen;
  1707. SDL_BlitSurface(background, NULL, to, &pos);
  1708. exit->show(to);
  1709. }
  1710. void CBattleReslutWindow::bExitf()
  1711. {
  1712. LOCPLINT->battleResultQuited();
  1713. }
  1714. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  1715. {
  1716. pos = position;
  1717. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  1718. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1719. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->preth->zelp[427].first)(3,CGI->preth->zelp[427].first), CGI->preth->zelp[427].second, false, "sysopchk.def", NULL, 185, 140, false);
  1720. viewGrid->select(owner->printCellBorders);
  1721. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->preth->zelp[428].first)(3,CGI->preth->zelp[428].first), CGI->preth->zelp[428].second, false, "sysopchk.def", NULL, 185, 173, false);
  1722. movementShadow->select(owner->printStackRange);
  1723. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->preth->zelp[429].first)(3,CGI->preth->zelp[429].first), CGI->preth->zelp[429].second, false, "sysopchk.def", NULL, 185, 207, false);
  1724. mouseShadow->select(owner->printMouseShadow);
  1725. animSpeeds = new CHighlightableButtonsGroup(0);
  1726. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[422].first),CGI->preth->zelp[422].second, "sysopb9.def",188, 309, 1);
  1727. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[423].first),CGI->preth->zelp[423].second, "sysob10.def",252, 309, 2);
  1728. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[424].first),CGI->preth->zelp[424].second, "sysob11.def",315, 309, 4);
  1729. animSpeeds->select(owner->getAnimSpeed(), 1);
  1730. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  1731. setToDefault = new AdventureMapButton (CGI->preth->zelp[392].first, CGI->preth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def", false, NULL, false);
  1732. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  1733. exit = new AdventureMapButton (CGI->preth->zelp[393].first, CGI->preth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def", false, NULL, false);
  1734. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  1735. //printing texts to background
  1736. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  1737. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  1738. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  1739. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  1740. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  1741. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  1742. //auto - combat options
  1743. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  1744. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  1745. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  1746. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  1747. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  1748. //creature info
  1749. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  1750. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  1751. //general options
  1752. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  1753. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  1754. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  1755. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  1756. //texts printed
  1757. }
  1758. CBattleOptionsWindow::~CBattleOptionsWindow()
  1759. {
  1760. SDL_FreeSurface(background);
  1761. delete setToDefault;
  1762. delete exit;
  1763. delete viewGrid;
  1764. delete movementShadow;
  1765. delete animSpeeds;
  1766. delete mouseShadow;
  1767. }
  1768. void CBattleOptionsWindow::activate()
  1769. {
  1770. setToDefault->activate();
  1771. exit->activate();
  1772. viewGrid->activate();
  1773. movementShadow->activate();
  1774. animSpeeds->activate();
  1775. mouseShadow->activate();
  1776. }
  1777. void CBattleOptionsWindow::deactivate()
  1778. {
  1779. setToDefault->deactivate();
  1780. exit->deactivate();
  1781. viewGrid->deactivate();
  1782. movementShadow->deactivate();
  1783. animSpeeds->deactivate();
  1784. mouseShadow->deactivate();
  1785. }
  1786. void CBattleOptionsWindow::show(SDL_Surface *to)
  1787. {
  1788. if(!to) //"evaluating" to
  1789. to = screen;
  1790. SDL_BlitSurface(background, NULL, to, &pos);
  1791. setToDefault->show(to);
  1792. exit->show(to);
  1793. viewGrid->show(to);
  1794. movementShadow->show(to);
  1795. animSpeeds->show(to);
  1796. mouseShadow->show(to);
  1797. }
  1798. void CBattleOptionsWindow::bDefaultf()
  1799. {
  1800. }
  1801. void CBattleOptionsWindow::bExitf()
  1802. {
  1803. deactivate();
  1804. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  1805. {
  1806. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  1807. {
  1808. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  1809. break;
  1810. }
  1811. }
  1812. delete this;
  1813. LOCPLINT->curint->activate();
  1814. }