CBattleInterface.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  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 "hch/CSpellHandler.h"
  11. #include "CCursorHandler.h"
  12. #include "CCallback.h"
  13. #include "CGameState.h"
  14. #include "hch/CGeneralTextHandler.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. inline bool operator ()(const CStack& a, const CStack& b)
  36. {
  37. return (a.speed())>(b.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), printMouseShadow(true), spellDestSelectMode(false), spellToCast(NULL)
  42. {
  43. strongInterest = true;
  44. givenCommand = new CondSh<BattleAction *>(NULL);
  45. //initializing armies
  46. this->army1 = army1;
  47. this->army2 = army2;
  48. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  49. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  50. {
  51. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  52. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  53. creAnims[b->second.ID]->setType(2);
  54. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  55. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  56. }
  57. //preparing menu background and terrain
  58. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  59. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  60. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  61. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  62. //preparing graphics for displaying amounts of creatures
  63. amountBasic = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  64. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  65. CSDL_Ext::alphaTransform(amountNormal);
  66. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  67. {
  68. if((amountNormal->format->palette->colors+g)->b != 132 &&
  69. (amountNormal->format->palette->colors+g)->g != 231 &&
  70. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  71. {
  72. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  73. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  74. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  75. }
  76. }
  77. ////blitting menu background and terrain
  78. blitAt(background, 0, 0);
  79. blitAt(menu, 0, 556);
  80. CSDL_Ext::update();
  81. //preparing buttons and console
  82. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", SDLK_o);
  83. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", SDLK_s);
  84. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", SDLK_r);
  85. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", SDLK_a);
  86. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", SDLK_c);
  87. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", SDLK_w);
  88. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", SDLK_d);
  89. bDefence->assignedKeys.insert(SDLK_SPACE);
  90. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", SDLK_UP);
  91. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", SDLK_DOWN);
  92. bConsoleDown->bitmapOffset = 2;
  93. console = new CBattleConsole();
  94. console->pos.x = 211;
  95. console->pos.y = 560;
  96. console->pos.w = 406;
  97. console->pos.h = 38;
  98. //loading hero animations
  99. if(hero1) // attacking hero
  100. {
  101. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner, hero1->tempOwner == LOCPLINT->playerID ? hero1 : NULL, this);
  102. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  103. }
  104. else
  105. {
  106. attackingHero = NULL;
  107. }
  108. if(hero2) // defending hero
  109. {
  110. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner, hero2->tempOwner == LOCPLINT->playerID ? hero2 : NULL, this);
  111. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  112. }
  113. else
  114. {
  115. defendingHero = NULL;
  116. }
  117. //preparing cells and hexes
  118. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  119. CSDL_Ext::alphaTransform(cellBorder);
  120. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  121. CSDL_Ext::alphaTransform(cellShade);
  122. for(int h=0; h<BFIELD_SIZE; ++h)
  123. {
  124. bfield[h].myNumber = h;
  125. int x = 14 + ((h/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(h%BFIELD_WIDTH);
  126. int y = 86 + 42 * (h/BFIELD_WIDTH);
  127. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  128. bfield[h].accesible = true;
  129. bfield[h].myInterface = this;
  130. }
  131. //locking occupied positions on batlefield
  132. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  133. {
  134. bfield[it->second.position].accesible = false;
  135. }
  136. //loading projectiles for units
  137. for(std::map<int, CStack>::iterator g = stacks.begin(); g != stacks.end(); ++g)
  138. {
  139. if(g->second.creature->isShooting() && CGI->creh->idToProjectile[g->second.creature->idNumber] != std::string())
  140. {
  141. idToProjectile[g->second.creature->idNumber] = CDefHandler::giveDef(CGI->creh->idToProjectile[g->second.creature->idNumber]);
  142. if(idToProjectile[g->second.creature->idNumber]->ourImages.size() > 2) //add symmetric images
  143. {
  144. for(int k = idToProjectile[g->second.creature->idNumber]->ourImages.size()-2; k > 1; --k)
  145. {
  146. Cimage ci;
  147. ci.bitmap = CSDL_Ext::rotate01(idToProjectile[g->second.creature->idNumber]->ourImages[k].bitmap);
  148. ci.groupNumber = 0;
  149. ci.imName = std::string();
  150. idToProjectile[g->second.creature->idNumber]->ourImages.push_back(ci);
  151. }
  152. }
  153. for(int s=0; s<idToProjectile[g->second.creature->idNumber]->ourImages.size(); ++s) //alpha transforming
  154. {
  155. CSDL_Ext::alphaTransform(idToProjectile[g->second.creature->idNumber]->ourImages[s].bitmap);
  156. }
  157. }
  158. }
  159. //preparing graphic with cell borders
  160. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  161. //copying palette
  162. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  163. {
  164. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  165. }
  166. //palette copied
  167. for(int i=0; i<BFIELD_HEIGHT; ++i) //rows
  168. {
  169. for(int j=0; j<BFIELD_WIDTH-2; ++j) //columns
  170. {
  171. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  172. int y = 86 + 42 * i;
  173. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  174. {
  175. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  176. {
  177. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  178. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  179. }
  180. }
  181. }
  182. }
  183. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  184. }
  185. CBattleInterface::~CBattleInterface()
  186. {
  187. SDL_FreeSurface(background);
  188. SDL_FreeSurface(menu);
  189. SDL_FreeSurface(amountBasic);
  190. SDL_FreeSurface(amountNormal);
  191. SDL_FreeSurface(cellBorders);
  192. SDL_FreeSurface(backgroundWithHexes);
  193. delete bOptions;
  194. delete bSurrender;
  195. delete bFlee;
  196. delete bAutofight;
  197. delete bSpell;
  198. delete bWait;
  199. delete bDefence;
  200. delete bConsoleUp;
  201. delete bConsoleDown;
  202. delete console;
  203. delete resWindow;
  204. delete givenCommand;
  205. delete attackingHero;
  206. delete defendingHero;
  207. SDL_FreeSurface(cellBorder);
  208. SDL_FreeSurface(cellShade);
  209. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  210. delete g->second;
  211. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  212. delete g->second;
  213. }
  214. void CBattleInterface::setPrintCellBorders(bool set)
  215. {
  216. printCellBorders = set;
  217. redrawBackgroundWithHexes(activeStack);
  218. }
  219. void CBattleInterface::setPrintStackRange(bool set)
  220. {
  221. printStackRange = set;
  222. redrawBackgroundWithHexes(activeStack);
  223. }
  224. void CBattleInterface::setPrintMouseShadow(bool set)
  225. {
  226. printMouseShadow = set;
  227. }
  228. void CBattleInterface::activate()
  229. {
  230. KeyInterested::activate();
  231. MotionInterested::activate();
  232. subInt = NULL;
  233. bOptions->activate();
  234. bSurrender->activate();
  235. bFlee->activate();
  236. bAutofight->activate();
  237. bSpell->activate();
  238. bWait->activate();
  239. bDefence->activate();
  240. bConsoleUp->activate();
  241. bConsoleDown->activate();
  242. for(int b=0; b<BFIELD_SIZE; ++b)
  243. {
  244. bfield[b].activate();
  245. }
  246. if(attackingHero)
  247. attackingHero->activate();
  248. if(defendingHero)
  249. defendingHero->activate();
  250. }
  251. void CBattleInterface::deactivate()
  252. {
  253. KeyInterested::deactivate();
  254. MotionInterested::deactivate();
  255. bOptions->deactivate();
  256. bSurrender->deactivate();
  257. bFlee->deactivate();
  258. bAutofight->deactivate();
  259. bSpell->deactivate();
  260. bWait->deactivate();
  261. bDefence->deactivate();
  262. bConsoleUp->deactivate();
  263. bConsoleDown->deactivate();
  264. for(int b=0; b<BFIELD_SIZE; ++b)
  265. {
  266. bfield[b].deactivate();
  267. }
  268. if(attackingHero)
  269. attackingHero->deactivate();
  270. if(defendingHero)
  271. defendingHero->deactivate();
  272. }
  273. void CBattleInterface::show(SDL_Surface * to)
  274. {
  275. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  276. ++animCount;
  277. if(!to) //"evaluating" to
  278. to = screen;
  279. //printing background and hexes
  280. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //show everything with range
  281. {
  282. blitAt(backgroundWithHexes, 0, 0, to);
  283. }
  284. else
  285. {
  286. //showing background
  287. blitAt(background, 0, 0, to);
  288. if(printCellBorders)
  289. {
  290. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, NULL);
  291. }
  292. }
  293. //printing hovered cell
  294. if(printMouseShadow)
  295. {
  296. for(int b=0; b<BFIELD_SIZE; ++b)
  297. {
  298. if(bfield[b].strictHovered && bfield[b].hovered)
  299. {
  300. int x = 14 + ((b/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(b%BFIELD_WIDTH);
  301. int y = 86 + 42 * (b/BFIELD_WIDTH);
  302. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  303. }
  304. }
  305. }
  306. //showing menu background and console
  307. blitAt(menu, 0, 556, to);
  308. console->show(to);
  309. //showing buttons
  310. bOptions->show(to);
  311. bSurrender->show(to);
  312. bFlee->show(to);
  313. bAutofight->show(to);
  314. bSpell->show(to);
  315. bWait->show(to);
  316. bDefence->show(to);
  317. bConsoleUp->show(to);
  318. bConsoleDown->show(to);
  319. //showing hero animations
  320. if(attackingHero)
  321. attackingHero->show(to);
  322. if(defendingHero)
  323. defendingHero->show(to);
  324. ////showing units //a lot of work...
  325. std::vector<int> stackAliveByHex[BFIELD_SIZE];
  326. //double loop because dead stacks should be printed first
  327. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  328. {
  329. if(j->second.alive())
  330. stackAliveByHex[j->second.position].push_back(j->second.ID);
  331. }
  332. std::vector<int> stackDeadByHex[BFIELD_SIZE];
  333. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  334. {
  335. if(!j->second.alive())
  336. stackDeadByHex[j->second.position].push_back(j->second.ID);
  337. }
  338. attackingShowHelper(); // handle attack animation
  339. for(int b=0; b<BFIELD_SIZE; ++b) //showing dead stacks
  340. {
  341. for(int v=0; v<stackDeadByHex[b].size(); ++v)
  342. {
  343. 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()!=5, stackDeadByHex[b][v]==activeStack); //increment always when moving, never if stack died
  344. //printing amount
  345. if(stacks[stackDeadByHex[b][v]].amount > 0) //don't print if stack is not alive
  346. {
  347. int xAdd = stacks[stackDeadByHex[b][v]].attackerOwned ? 220 : 202;
  348. 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));
  349. std::stringstream ss;
  350. ss<<stacks[stackDeadByHex[b][v]].amount;
  351. 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);
  352. }
  353. }
  354. }
  355. for(int b=0; b<BFIELD_SIZE; ++b) //showing alive stacks
  356. {
  357. for(int v=0; v<stackAliveByHex[b].size(); ++v)
  358. {
  359. 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()!=5 && creAnims[stackAliveByHex[b][v]]->getType()!=20 && creAnims[stackAliveByHex[b][v]]->getType()!=21, stackAliveByHex[b][v]==activeStack); //increment always when moving, never if stack died
  360. //printing amount
  361. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  362. {
  363. int xAdd = stacks[stackAliveByHex[b][v]].attackerOwned ? 220 : 202;
  364. 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));
  365. std::stringstream ss;
  366. ss<<stacks[stackAliveByHex[b][v]].amount;
  367. 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);
  368. }
  369. }
  370. }
  371. //units shown
  372. projectileShowHelper(to);//showing projectiles
  373. //showing spell effects
  374. std::vector< std::list<SBattleEffect>::iterator > toErase;
  375. for(std::list<SBattleEffect>::iterator it = battleEffects.begin(); it!=battleEffects.end(); ++it)
  376. {
  377. blitAt(it->anim->ourImages[(it->frame)%it->anim->ourImages.size()].bitmap, it->x, it->y, to);
  378. ++(it->frame);
  379. if(it->frame == it->maxFrame)
  380. toErase.push_back(it);
  381. }
  382. for(int b=0; b<toErase.size(); ++b)
  383. {
  384. delete toErase[b]->anim;
  385. battleEffects.erase(toErase[b]);
  386. }
  387. //showing queue of stacks
  388. if(showStackQueue)
  389. {
  390. int xPos = screen->w/2 - ( stacks.size() * 37 )/2;
  391. int yPos = 10;
  392. std::vector<CStack> stacksSorted;
  393. for(int v=0; v<stacks.size(); ++v)
  394. {
  395. if(stacks[v].alive()) //we don't want dead stacks to be there
  396. stacksSorted.push_back(stacks[v]);
  397. }
  398. std::stable_sort(stacksSorted.begin(), stacksSorted.end(), cmpst2);
  399. int startFrom = -1;
  400. for(int n=0; n<stacksSorted.size(); ++n)
  401. {
  402. if(stacksSorted[n].ID == activeStack)
  403. {
  404. startFrom = n;
  405. break;
  406. }
  407. }
  408. if(startFrom != -1)
  409. {
  410. for(int b=startFrom; b<stacksSorted.size()+startFrom; ++b)
  411. {
  412. SDL_BlitSurface(graphics->smallImgs[-2], NULL, to, &genRect(32, 32, xPos, yPos));
  413. //printing colored border
  414. for(int xFrom = xPos-1; xFrom<xPos+33; ++xFrom)
  415. {
  416. for(int yFrom = yPos-1; yFrom<yPos+33; ++yFrom)
  417. {
  418. if(xFrom == xPos-1 || xFrom == xPos+32 || yFrom == yPos-1 || yFrom == yPos+32)
  419. {
  420. SDL_Color pc;
  421. if(stacksSorted[b % stacksSorted.size()].owner != 255)
  422. {
  423. pc = graphics->playerColors[stacksSorted[b % stacksSorted.size()].owner];
  424. }
  425. else
  426. {
  427. pc = *graphics->neutralColor;
  428. }
  429. CSDL_Ext::SDL_PutPixel(to, xFrom, yFrom, pc.r, pc.g, pc.b);
  430. }
  431. }
  432. }
  433. //colored border printed
  434. SDL_BlitSurface(graphics->smallImgs[stacksSorted[b % stacksSorted.size()].creature->idNumber], NULL, to, &genRect(32, 32, xPos, yPos));
  435. xPos += 37;
  436. }
  437. }
  438. }
  439. //showing window with result of battle
  440. if(resWindow)
  441. {
  442. resWindow->show(to);
  443. }
  444. }
  445. void CBattleInterface::keyPressed(const SDL_KeyboardEvent & key)
  446. {
  447. if(key.keysym.sym == SDLK_q)
  448. showStackQueue = key.state==SDL_PRESSED;
  449. }
  450. void CBattleInterface::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  451. {
  452. if(activeStack>=0 && !spellDestSelectMode)
  453. {
  454. int myNumber = -1; //number of hovered tile
  455. for(int g=0; g<BFIELD_SIZE; ++g)
  456. {
  457. if(bfield[g].hovered && bfield[g].strictHovered)
  458. {
  459. myNumber = g;
  460. break;
  461. }
  462. }
  463. if(myNumber == -1)
  464. {
  465. CGI->curh->changeGraphic(1, 6);
  466. }
  467. else
  468. {
  469. if(std::find(shadedHexes.begin(),shadedHexes.end(),myNumber) == shadedHexes.end())
  470. {
  471. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  472. if(shere)
  473. {
  474. if(shere->owner == LOCPLINT->playerID) //our stack
  475. CGI->curh->changeGraphic(1,5);
  476. else if(LOCPLINT->cb->battleCanShoot(activeStack,myNumber)) //we can shoot enemy
  477. CGI->curh->changeGraphic(1,3);
  478. else if(isTileAttackable(myNumber)) //available enemy (melee attackable)
  479. {
  480. int fromHex = -1;
  481. for(int b=0; b<BFIELD_SIZE; ++b)
  482. if(bfield[b].hovered && !bfield[b].strictHovered)
  483. {
  484. fromHex = b;
  485. break;
  486. }
  487. if(fromHex!=-1 && fromHex%17!=0 && fromHex%17!=16 && vstd::contains(shadedHexes, fromHex))
  488. {
  489. switch(BattleInfo::mutualPosition(fromHex, myNumber))
  490. {
  491. case 0:
  492. CGI->curh->changeGraphic(1,12);
  493. break;
  494. case 1:
  495. CGI->curh->changeGraphic(1,7);
  496. break;
  497. case 2:
  498. CGI->curh->changeGraphic(1,8);
  499. break;
  500. case 3:
  501. CGI->curh->changeGraphic(1,9);
  502. break;
  503. case 4:
  504. CGI->curh->changeGraphic(1,10);
  505. break;
  506. case 5:
  507. CGI->curh->changeGraphic(1,11);
  508. break;
  509. }
  510. }
  511. }
  512. else //unavailable enemy
  513. CGI->curh->changeGraphic(1,0);
  514. }
  515. else //empty unavailable tile
  516. CGI->curh->changeGraphic(1,0);
  517. }
  518. else //available tile
  519. {
  520. if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isFlying())
  521. CGI->curh->changeGraphic(1,2);
  522. else
  523. CGI->curh->changeGraphic(1,1);
  524. }
  525. }
  526. }
  527. else if(spellDestSelectMode)
  528. {
  529. int myNumber = -1; //number of hovered tile
  530. for(int g=0; g<BFIELD_SIZE; ++g)
  531. {
  532. if(bfield[g].hovered && bfield[g].strictHovered)
  533. {
  534. myNumber = g;
  535. break;
  536. }
  537. }
  538. if(myNumber == -1)
  539. {
  540. CGI->curh->changeGraphic(1, 0);
  541. }
  542. else
  543. {
  544. switch(spellSelMode)
  545. {
  546. case 0:
  547. CGI->curh->changeGraphic(3, 0);
  548. break;
  549. case 1:
  550. if(LOCPLINT->cb->battleGetStackByPos(myNumber) && LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(myNumber)->owner )
  551. CGI->curh->changeGraphic(3, 0);
  552. else
  553. CGI->curh->changeGraphic(1, 0);
  554. break;
  555. case 2:
  556. if(LOCPLINT->cb->battleGetStackByPos(myNumber) && LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(myNumber)->owner )
  557. CGI->curh->changeGraphic(3, 0);
  558. else
  559. CGI->curh->changeGraphic(1, 0);
  560. break;
  561. case 3:
  562. if(LOCPLINT->cb->battleGetStackByPos(myNumber))
  563. CGI->curh->changeGraphic(3, 0);
  564. else
  565. CGI->curh->changeGraphic(1, 0);
  566. break;
  567. case 4: //TODO: implement this case
  568. break;
  569. }
  570. }
  571. }
  572. }
  573. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  574. {
  575. if(creAnims[number]==NULL)
  576. return false; //there is no such creature
  577. creAnims[number]->setType(8);
  578. int firstFrame = creAnims[number]->getFrame();
  579. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(8) + firstFrame - 1; ++g)
  580. {
  581. show();
  582. CSDL_Ext::update();
  583. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  584. }
  585. creDir[number] = !creDir[number];
  586. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  587. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  588. creAnims[number]->pos.x = coords.first;
  589. //creAnims[number]->pos.y = coords.second;
  590. if(wideTrick && curs.creature->isDoubleWide())
  591. {
  592. if(curs.attackerOwned)
  593. {
  594. if(!creDir[number])
  595. creAnims[number]->pos.x -= 44;
  596. }
  597. else
  598. {
  599. if(creDir[number])
  600. creAnims[number]->pos.x += 44;
  601. }
  602. }
  603. creAnims[number]->setType(7);
  604. firstFrame = creAnims[number]->getFrame();
  605. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(7) + firstFrame - 1; ++g)
  606. {
  607. show();
  608. CSDL_Ext::update();
  609. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  610. }
  611. creAnims[number]->setType(2);
  612. return true;
  613. }
  614. void CBattleInterface::bOptionsf()
  615. {
  616. CGI->curh->changeGraphic(0,0);
  617. deactivate();
  618. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  619. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  620. optionsWin->activate();
  621. LOCPLINT->objsToBlit.push_back(optionsWin);
  622. }
  623. void CBattleInterface::bSurrenderf()
  624. {
  625. }
  626. void CBattleInterface::bFleef()
  627. {
  628. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  629. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  630. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  631. }
  632. void CBattleInterface::reallyFlee()
  633. {
  634. giveCommand(4,0,0);
  635. CGI->curh->changeGraphic(0, 0);
  636. }
  637. void CBattleInterface::bAutofightf()
  638. {
  639. }
  640. void CBattleInterface::bSpellf()
  641. {
  642. CGI->curh->changeGraphic(0,0);
  643. deactivate();
  644. const CGHeroInstance * chi = NULL;
  645. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  646. chi = attackingHeroInstance;
  647. else
  648. chi = defendingHeroInstance;
  649. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), chi);
  650. spellWindow->activate();
  651. LOCPLINT->objsToBlit.push_back(spellWindow);
  652. }
  653. void CBattleInterface::bWaitf()
  654. {
  655. giveCommand(8,0,activeStack);
  656. }
  657. void CBattleInterface::bDefencef()
  658. {
  659. giveCommand(3,0,activeStack);
  660. }
  661. void CBattleInterface::bConsoleUpf()
  662. {
  663. console->scrollUp();
  664. }
  665. void CBattleInterface::bConsoleDownf()
  666. {
  667. console->scrollDown();
  668. }
  669. void CBattleInterface::newStack(CStack stack)
  670. {
  671. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  672. creAnims[stack.ID]->setType(2);
  673. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  674. }
  675. void CBattleInterface::stackRemoved(CStack stack)
  676. {
  677. delete creAnims[stack.ID];
  678. creAnims.erase(stack.ID);
  679. }
  680. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  681. {
  682. if(creAnims[ID]->getType() != 2)
  683. {
  684. return; //something went wrong
  685. }
  686. if(byShooting) //delay hit animation
  687. {
  688. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  689. while(true)
  690. {
  691. bool found = false;
  692. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  693. {
  694. if(it->creID == attacker.creature->idNumber)
  695. {
  696. found = true;
  697. break;
  698. }
  699. }
  700. if(!found)
  701. break;
  702. else
  703. {
  704. show();
  705. CSDL_Ext::update();
  706. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  707. }
  708. }
  709. }
  710. creAnims[ID]->setType(5); //death
  711. //int firstFrame = creAnims[ID]->getFrame();
  712. int increments = 0;
  713. while(increments < creAnims[ID]->framesInGroup(5)-1)
  714. {
  715. if((animCount%(4/animSpeed))==0)
  716. {
  717. creAnims[ID]->incrementFrame();
  718. ++increments;
  719. }
  720. show();
  721. CSDL_Ext::update();
  722. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  723. }
  724. if(IDby!=-1)
  725. printConsoleAttacked(ID, dmg, killed, IDby);
  726. }
  727. void CBattleInterface::stackActivated(int number)
  728. {
  729. //givenCommand = NULL;
  730. activeStack = number;
  731. myTurn = true;
  732. redrawBackgroundWithHexes(number);
  733. bWait->block(vstd::contains(LOCPLINT->cb->battleGetStackByID(number)->state,WAITING)); //block waiting button if stack has been already waiting
  734. }
  735. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving)
  736. {
  737. bool startMoving = creAnims[number]->type==20;
  738. //a few useful variables
  739. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  740. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  741. int hexWbase = 44, hexHbase = 42;
  742. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  743. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  744. {
  745. CGI->curh->hide();
  746. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  747. {
  748. show();
  749. CSDL_Ext::update();
  750. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  751. if((animCount+1)%(4/animSpeed)==0)
  752. creAnims[number]->incrementFrame();
  753. }
  754. }
  755. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  756. {
  757. switch(mutPos) //reverse unit if necessary
  758. {
  759. case 0: case 4: case 5:
  760. if(creDir[number] == true)
  761. reverseCreature(number, curStackPos, twoTiles);
  762. break;
  763. case 1: case 2: case 3:
  764. if(creDir[number] == false)
  765. reverseCreature(number, curStackPos, twoTiles);
  766. break;
  767. }
  768. //moving instructions
  769. creAnims[number]->setType(0);
  770. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  771. for(int i=0; i<steps; ++i)
  772. {
  773. switch(mutPos)
  774. {
  775. case 0:
  776. posX -= ((float)hexWbase)/(2.0f*steps);
  777. creAnims[number]->pos.x = posX;
  778. posY -= ((float)hexHbase)/((float)steps);
  779. creAnims[number]->pos.y = posY;
  780. break;
  781. case 1:
  782. posX += ((float)hexWbase)/(2.0f*steps);
  783. creAnims[number]->pos.x = posX;
  784. posY -= ((float)hexHbase)/((float)steps);
  785. creAnims[number]->pos.y = posY;
  786. break;
  787. case 2:
  788. posX += ((float)hexWbase)/((float)steps);
  789. creAnims[number]->pos.x = posX;
  790. break;
  791. case 3:
  792. posX += ((float)hexWbase)/(2.0f*steps);
  793. creAnims[number]->pos.x = posX;
  794. posY += ((float)hexHbase)/((float)steps);
  795. creAnims[number]->pos.y = posY;
  796. break;
  797. case 4:
  798. posX -= ((float)hexWbase)/(2.0f*steps);
  799. creAnims[number]->pos.x = posX;
  800. posY += ((float)hexHbase)/((float)steps);
  801. creAnims[number]->pos.y = posY;
  802. break;
  803. case 5:
  804. posX -= ((float)hexWbase)/((float)steps);
  805. creAnims[number]->pos.x = posX;
  806. break;
  807. }
  808. show();
  809. CSDL_Ext::update();
  810. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  811. if((animCount+1)%(4/animSpeed)==0)
  812. creAnims[number]->incrementFrame();
  813. }
  814. }
  815. if(endMoving) //animation of ending move
  816. {
  817. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  818. {
  819. creAnims[number]->setType(21);
  820. for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  821. {
  822. show();
  823. CSDL_Ext::update();
  824. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  825. if((animCount+1)%(4/animSpeed)==0)
  826. creAnims[number]->incrementFrame();
  827. }
  828. }
  829. creAnims[number]->setType(2); //resetting to default
  830. CGI->curh->show();
  831. }
  832. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  833. if(endMoving) //resetting to default
  834. {
  835. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  836. reverseCreature(number, destHex, twoTiles);
  837. }
  838. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  839. creAnims[number]->pos.x = coords.first;
  840. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  841. creAnims[number]->pos.x -= 44;
  842. creAnims[number]->pos.y = coords.second;
  843. }
  844. void CBattleInterface::stacksAreAttacked(std::vector<CBattleInterface::SStackAttackedInfo> attackedInfos)
  845. {
  846. while(true)
  847. {
  848. show();
  849. CSDL_Ext::update();
  850. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  851. //checking break conditions
  852. bool break_loop = true;
  853. for(int g=0; g<attackedInfos.size(); ++g)
  854. {
  855. if(creAnims[attackedInfos[g].ID]->getType() != 2)
  856. break_loop = false;
  857. if(attackingInfo && attackingInfo->IDby == attackedInfos[g].IDby)
  858. break_loop = false;
  859. }
  860. if(break_loop) break;
  861. }
  862. if(attackedInfos.size() == 1 && attackedInfos[0].byShooting) //delay hit animation
  863. {
  864. CStack attacker = *LOCPLINT->cb->battleGetStackByID(attackedInfos[0].IDby);
  865. while(true)
  866. {
  867. bool found = false;
  868. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  869. {
  870. if(it->creID == attacker.creature->idNumber)
  871. {
  872. found = true;
  873. break;
  874. }
  875. }
  876. if(!found)
  877. break;
  878. else
  879. {
  880. show();
  881. CSDL_Ext::update();
  882. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  883. }
  884. }
  885. }
  886. std::vector<int> animLengths;
  887. int maxLen = 0;
  888. for(int g=0; g<attackedInfos.size(); ++g)
  889. {
  890. creAnims[attackedInfos[g].ID]->setType(3); //getting hit
  891. animLengths.push_back( creAnims[attackedInfos[g].ID]->framesInGroup(3) );
  892. if(creAnims[attackedInfos[g].ID]->framesInGroup(3) > maxLen)
  893. {
  894. maxLen = creAnims[attackedInfos[g].ID]->framesInGroup(3);
  895. }
  896. }
  897. for(int i=0; i<maxLen; ++i)
  898. {
  899. show();
  900. CSDL_Ext::update();
  901. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  902. /*if((animCount+1)%(4/animSpeed)==0)
  903. creAnims[ID]->incrementFrame();*/
  904. for(int g=0; g<attackedInfos.size(); ++g)
  905. {
  906. if(i>=animLengths[g] && creAnims[attackedInfos[g].ID]->getType() == 3)
  907. creAnims[attackedInfos[g].ID]->setType(2);
  908. }
  909. }
  910. for(int g=0; g<attackedInfos.size(); ++g)
  911. {
  912. if(creAnims[attackedInfos[g].ID]->getType() == 3)
  913. creAnims[attackedInfos[g].ID]->setType(2);
  914. }
  915. for(int g=0; g<attackedInfos.size(); ++g)
  916. {
  917. if(attackedInfos[g].IDby!=-1)
  918. printConsoleAttacked(attackedInfos[g].ID, attackedInfos[g].dmg, attackedInfos[g].killed, attackedInfos[g].IDby);
  919. }
  920. }
  921. void CBattleInterface::stackAttacking(int ID, int dest)
  922. {
  923. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  924. {
  925. show();
  926. CSDL_Ext::update();
  927. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  928. }
  929. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  930. int reversedShift = 0; //shift of attacking stack's position due to reversing
  931. if(aStack.attackerOwned)
  932. {
  933. if(aStack.creature->isDoubleWide())
  934. {
  935. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  936. {
  937. case 0:
  938. //reverseCreature(ID, aStack.position, true);
  939. break;
  940. case 1:
  941. break;
  942. case 2:
  943. break;
  944. case 3:
  945. break;
  946. case 4:
  947. //reverseCreature(ID, aStack.position, true);
  948. break;
  949. case 5:
  950. reverseCreature(ID, aStack.position, true);
  951. break;
  952. case -1:
  953. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  954. {
  955. reverseCreature(ID, aStack.position, true);
  956. reversedShift = (aStack.attackerOwned ? -1 : 1);
  957. }
  958. break;
  959. }
  960. }
  961. else //else for if(aStack.creature->isDoubleWide())
  962. {
  963. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  964. {
  965. case 0:
  966. reverseCreature(ID, aStack.position, true);
  967. break;
  968. case 1:
  969. break;
  970. case 2:
  971. break;
  972. case 3:
  973. break;
  974. case 4:
  975. reverseCreature(ID, aStack.position, true);
  976. break;
  977. case 5:
  978. reverseCreature(ID, aStack.position, true);
  979. break;
  980. }
  981. }
  982. }
  983. else //if(aStack.attackerOwned)
  984. {
  985. if(aStack.creature->isDoubleWide())
  986. {
  987. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  988. {
  989. case 0:
  990. //reverseCreature(ID, aStack.position, true);
  991. break;
  992. case 1:
  993. break;
  994. case 2:
  995. reverseCreature(ID, aStack.position, true);
  996. break;
  997. case 3:
  998. break;
  999. case 4:
  1000. //reverseCreature(ID, aStack.position, true);
  1001. break;
  1002. case 5:
  1003. //reverseCreature(ID, aStack.position, true);
  1004. break;
  1005. case -1:
  1006. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1007. {
  1008. reverseCreature(ID, aStack.position, true);
  1009. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1010. }
  1011. break;
  1012. }
  1013. }
  1014. else //else for if(aStack.creature->isDoubleWide())
  1015. {
  1016. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1017. {
  1018. case 0:
  1019. //reverseCreature(ID, aStack.position, true);
  1020. break;
  1021. case 1:
  1022. reverseCreature(ID, aStack.position, true);
  1023. break;
  1024. case 2:
  1025. reverseCreature(ID, aStack.position, true);
  1026. break;
  1027. case 3:
  1028. reverseCreature(ID, aStack.position, true);
  1029. break;
  1030. case 4:
  1031. //reverseCreature(ID, aStack.position, true);
  1032. break;
  1033. case 5:
  1034. //reverseCreature(ID, aStack.position, true);
  1035. break;
  1036. }
  1037. }
  1038. }
  1039. attackingInfo = new CAttHelper;
  1040. attackingInfo->dest = dest;
  1041. attackingInfo->frame = 0;
  1042. attackingInfo->hitCount = 0;
  1043. attackingInfo->ID = ID;
  1044. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  1045. attackingInfo->reversing = false;
  1046. attackingInfo->posShiftDueToDist = reversedShift;
  1047. attackingInfo->shooting = false;
  1048. switch(BattleInfo::mutualPosition(aStack.position + reversedShift, dest)) //attack direction
  1049. {
  1050. case 0:
  1051. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1052. break;
  1053. case 1:
  1054. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1055. break;
  1056. case 2:
  1057. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1058. break;
  1059. case 3:
  1060. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1061. break;
  1062. case 4:
  1063. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1064. break;
  1065. case 5:
  1066. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1067. break;
  1068. default:
  1069. tlog1<<"Critical Error! Wrong dest in stackAttacking!"<<std::endl;
  1070. }
  1071. }
  1072. void CBattleInterface::newRound(int number)
  1073. {
  1074. console->addText(CGI->generaltexth->allTexts[412]);
  1075. }
  1076. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  1077. {
  1078. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  1079. ba->actionType = action;
  1080. ba->destinationTile = tile;
  1081. ba->stackNumber = stack;
  1082. ba->additionalInfo = additional;
  1083. givenCommand->setn(ba);
  1084. myTurn = false;
  1085. activeStack = -1;
  1086. }
  1087. bool CBattleInterface::isTileAttackable(const int & number) const
  1088. {
  1089. for(int b=0; b<shadedHexes.size(); ++b)
  1090. {
  1091. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  1092. return true;
  1093. }
  1094. return false;
  1095. }
  1096. void CBattleInterface::hexLclicked(int whichOne)
  1097. {
  1098. if((whichOne%BFIELD_WIDTH)!=0 && (whichOne%BFIELD_WIDTH)!=(BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  1099. {
  1100. if(!myTurn)
  1101. return; //we are not permit to do anything
  1102. if(spellDestSelectMode)
  1103. {
  1104. //checking destination
  1105. bool allowCasting = true;
  1106. switch(spellSelMode)
  1107. {
  1108. case 1:
  1109. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1110. allowCasting = false;
  1111. break;
  1112. case 2:
  1113. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1114. allowCasting = false;
  1115. break;
  1116. case 3:
  1117. if(!LOCPLINT->cb->battleGetStackByPos(whichOne))
  1118. allowCasting = false;
  1119. break;
  1120. case 4: //TODO: implement this case
  1121. break;
  1122. }
  1123. //destination checked
  1124. if(allowCasting)
  1125. {
  1126. spellToCast->destinationTile = whichOne;
  1127. LOCPLINT->cb->battleMakeAction(spellToCast);
  1128. delete spellToCast;
  1129. spellToCast = NULL;
  1130. spellDestSelectMode = false;
  1131. CGI->curh->changeGraphic(1, 6);
  1132. }
  1133. }
  1134. else
  1135. {
  1136. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  1137. if(!dest || !dest->alive()) //no creature at that tile
  1138. {
  1139. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  1140. {
  1141. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1142. giveCommand(2,whichOne,activeStack);
  1143. }
  1144. }
  1145. else if(dest->owner != attackingHeroInstance->tempOwner
  1146. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne) ) //shooting
  1147. {
  1148. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1149. giveCommand(7,whichOne,activeStack);
  1150. }
  1151. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  1152. {
  1153. switch(CGI->curh->number)
  1154. {
  1155. case 12:
  1156. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1157. break;
  1158. case 7:
  1159. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1160. break;
  1161. case 8:
  1162. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1163. break;
  1164. case 9:
  1165. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1166. break;
  1167. case 10:
  1168. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1169. break;
  1170. case 11:
  1171. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1172. break;
  1173. }
  1174. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1175. }
  1176. }
  1177. }
  1178. }
  1179. void CBattleInterface::stackIsShooting(int ID, int dest)
  1180. {
  1181. if(attackingInfo != NULL)
  1182. {
  1183. return; //something went wrong
  1184. }
  1185. //projectile
  1186. float projectileAngle; //in radians; if positive, projectiles goes up
  1187. 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)
  1188. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1189. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1190. if(fromHex < dest)
  1191. projectileAngle = -projectileAngle;
  1192. SProjectileInfo spi;
  1193. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1194. spi.step = 0;
  1195. spi.frameNum = 0;
  1196. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1197. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1198. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1199. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1200. if(projectileAngle > straightAngle) //upper shot
  1201. {
  1202. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1203. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1204. }
  1205. else if(projectileAngle < -straightAngle) //lower shot
  1206. {
  1207. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1208. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1209. }
  1210. else //straight shot
  1211. {
  1212. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1213. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1214. }
  1215. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1216. if(spi.lastStep == 0)
  1217. spi.lastStep = 1;
  1218. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1219. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1220. //set starting frame
  1221. if(spi.spin)
  1222. {
  1223. spi.frameNum = 0;
  1224. }
  1225. else
  1226. {
  1227. 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);
  1228. }
  1229. //set delay
  1230. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1231. projectiles.push_back(spi);
  1232. //attack aniamtion
  1233. attackingInfo = new CAttHelper;
  1234. attackingInfo->dest = dest;
  1235. attackingInfo->frame = 0;
  1236. attackingInfo->hitCount = 0;
  1237. attackingInfo->ID = ID;
  1238. attackingInfo->reversing = false;
  1239. attackingInfo->posShiftDueToDist = 0;
  1240. attackingInfo->shooting = true;
  1241. if(projectileAngle > straightAngle) //upper shot
  1242. attackingInfo->shootingGroup = 14;
  1243. else if(projectileAngle < -straightAngle) //lower shot
  1244. attackingInfo->shootingGroup = 16;
  1245. else //straight shot
  1246. attackingInfo->shootingGroup = 15;
  1247. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1248. }
  1249. void CBattleInterface::battleFinished(const BattleResult& br)
  1250. {
  1251. deactivate();
  1252. CGI->curh->changeGraphic(0,0);
  1253. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1254. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1255. resWindow->activate();
  1256. }
  1257. void CBattleInterface::spellCasted(SpellCasted * sc)
  1258. {
  1259. std::vector< std::string > anims; //for magic arrow and ice bolt
  1260. switch(sc->id)
  1261. {
  1262. case 15: //magic arrow
  1263. {
  1264. //initialization of anims
  1265. anims.push_back("C20SPX0.DEF"); anims.push_back("C20SPX1.DEF"); anims.push_back("C20SPX2.DEF"); anims.push_back("C20SPX3.DEF"); anims.push_back("C20SPX4.DEF");
  1266. }
  1267. case 16: //ice bolt
  1268. {
  1269. if(anims.size() == 0) //initialiaztion of anims
  1270. {
  1271. anims.push_back("C08SPW0.DEF"); anims.push_back("C08SPW1.DEF"); anims.push_back("C08SPW2.DEF"); anims.push_back("C08SPW3.DEF"); anims.push_back("C08SPW4.DEF");
  1272. }
  1273. } //end of ice bolt only part
  1274. { //common ice bolt and magic arrow part
  1275. //initial variables
  1276. std::string animToDisplay;
  1277. std::pair<int, int> srccoord = sc->side ? std::make_pair(770, 60) : std::make_pair(30, 60);
  1278. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile)->creature); //position attacked by arrow
  1279. destcoord.first += 250; destcoord.second += 240;
  1280. //animation angle
  1281. float angle = atan2(float(destcoord.first - srccoord.first), float(destcoord.second - srccoord.second));
  1282. //choosing animation by angle
  1283. if(angle > 1.50)
  1284. animToDisplay = anims[0];
  1285. else if(angle > 1.20)
  1286. animToDisplay = anims[1];
  1287. else if(angle > 0.90)
  1288. animToDisplay = anims[2];
  1289. else if(angle > 0.60)
  1290. animToDisplay = anims[3];
  1291. else
  1292. animToDisplay = anims[4];
  1293. //displaying animation
  1294. int steps = sqrt((float)((destcoord.first - srccoord.first)*(destcoord.first - srccoord.first) + (destcoord.second - srccoord.second) * (destcoord.second - srccoord.second))) / 40;
  1295. if(steps <= 0)
  1296. steps = 1;
  1297. CDefHandler * animDef = CDefHandler::giveDef(animToDisplay);
  1298. int dx = (destcoord.first - srccoord.first - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.second - srccoord.second - animDef->ourImages[0].bitmap->h)/steps;
  1299. for(int g=0; g<steps; ++g)
  1300. {
  1301. show();
  1302. SDL_Rect & srcr = animDef->ourImages[g%animDef->ourImages.size()].bitmap->clip_rect;
  1303. SDL_Rect dstr = genRect(srcr.h, srcr.w, srccoord.first + g*dx, srccoord.second + g*dy);
  1304. SDL_BlitSurface(animDef->ourImages[g%animDef->ourImages.size()].bitmap, &srcr, screen, &dstr);
  1305. CSDL_Ext::update();
  1306. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1307. }
  1308. int b=0;
  1309. break; //for 15 and 16 cases
  1310. }
  1311. case 17: //lightning bolt
  1312. {
  1313. displayEffect(1, sc->tile);
  1314. break;
  1315. }
  1316. case 18: //implosion
  1317. {
  1318. displayEffect(10, sc->tile);
  1319. break;
  1320. }
  1321. case 53: //haste
  1322. {
  1323. displayEffect(31, sc->tile);
  1324. break;
  1325. }
  1326. case 54: //slow
  1327. {
  1328. displayEffect(19, sc->tile);
  1329. break;
  1330. }
  1331. case 61: //forgetfulness
  1332. {
  1333. displayEffect(42, sc->tile);
  1334. break;
  1335. }
  1336. }
  1337. }
  1338. void CBattleInterface::castThisSpell(int spellID)
  1339. {
  1340. BattleAction * ba = new BattleAction;
  1341. ba->actionType = 1;
  1342. ba->additionalInfo = spellID; //spell number
  1343. ba->destinationTile = -1;
  1344. ba->stackNumber = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? -1 : -2;
  1345. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1346. spellToCast = ba;
  1347. spellDestSelectMode = true;
  1348. //choosing possible tragets
  1349. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? attackingHeroInstance : attackingHeroInstance;
  1350. spellSelMode = 0;
  1351. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET") != std::string::npos)
  1352. {
  1353. spellSelMode = 3;
  1354. }
  1355. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET_2") != std::string::npos)
  1356. {
  1357. if(castingHero && castingHero->getSpellSecLevel(spellID) < 3)
  1358. spellSelMode = 3;
  1359. else //TODO: no destination chould apply in this case
  1360. {
  1361. }
  1362. }
  1363. CGI->curh->changeGraphic(3, 0);
  1364. }
  1365. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  1366. {
  1367. if(graphics->battleACToDef[effect].size() != 0)
  1368. {
  1369. SBattleEffect be;
  1370. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1371. be.frame = 0;
  1372. be.maxFrame = be.anim->ourImages.size();
  1373. be.x = 22 * ( ((destTile/BFIELD_WIDTH) + 1)%2 ) + 44 * (destTile % BFIELD_WIDTH) + 45;
  1374. be.y = 105 + 42 * (destTile/BFIELD_WIDTH);
  1375. if(effect != 1)
  1376. {
  1377. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1378. be.y -= be.anim->ourImages[0].bitmap->h/2;
  1379. }
  1380. else if(effect == 1)
  1381. {
  1382. be.x -= be.anim->ourImages[0].bitmap->w;
  1383. be.y -= be.anim->ourImages[0].bitmap->h;
  1384. }
  1385. battleEffects.push_back(be);
  1386. }
  1387. //battleEffects
  1388. }
  1389. void CBattleInterface::setAnimSpeed(int set)
  1390. {
  1391. animSpeed = set;
  1392. }
  1393. int CBattleInterface::getAnimSpeed() const
  1394. {
  1395. return animSpeed;
  1396. }
  1397. float CBattleInterface::getAnimSpeedMultiplier() const
  1398. {
  1399. switch(animSpeed)
  1400. {
  1401. case 1:
  1402. return 3.5f;
  1403. case 2:
  1404. return 2.2f;
  1405. case 4:
  1406. return 1.0f;
  1407. default:
  1408. return 0.0f;
  1409. }
  1410. }
  1411. void CBattleInterface::attackingShowHelper()
  1412. {
  1413. if(attackingInfo && !attackingInfo->reversing)
  1414. {
  1415. if(attackingInfo->frame == 0)
  1416. {
  1417. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1418. if(attackingInfo->shooting)
  1419. {
  1420. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1421. }
  1422. else
  1423. {
  1424. if(aStack.creature->isDoubleWide())
  1425. {
  1426. switch(BattleInfo::mutualPosition(aStack.position+attackingInfo->posShiftDueToDist, attackingInfo->dest)) //attack direction
  1427. {
  1428. case 0:
  1429. creAnims[attackingInfo->ID]->setType(11);
  1430. break;
  1431. case 1:
  1432. creAnims[attackingInfo->ID]->setType(11);
  1433. break;
  1434. case 2:
  1435. creAnims[attackingInfo->ID]->setType(12);
  1436. break;
  1437. case 3:
  1438. creAnims[attackingInfo->ID]->setType(13);
  1439. break;
  1440. case 4:
  1441. creAnims[attackingInfo->ID]->setType(13);
  1442. break;
  1443. case 5:
  1444. creAnims[attackingInfo->ID]->setType(12);
  1445. break;
  1446. }
  1447. }
  1448. else //else for if(aStack.creature->isDoubleWide())
  1449. {
  1450. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1451. {
  1452. case 0:
  1453. creAnims[attackingInfo->ID]->setType(11);
  1454. break;
  1455. case 1:
  1456. creAnims[attackingInfo->ID]->setType(11);
  1457. break;
  1458. case 2:
  1459. creAnims[attackingInfo->ID]->setType(12);
  1460. break;
  1461. case 3:
  1462. creAnims[attackingInfo->ID]->setType(13);
  1463. break;
  1464. case 4:
  1465. creAnims[attackingInfo->ID]->setType(13);
  1466. break;
  1467. case 5:
  1468. creAnims[attackingInfo->ID]->setType(12);
  1469. break;
  1470. }
  1471. }
  1472. }
  1473. }
  1474. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1475. {
  1476. attackingInfo->reversing = true;
  1477. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1478. if(aStackp == NULL)
  1479. return;
  1480. CStack aStack = *aStackp;
  1481. if(aStack.attackerOwned)
  1482. {
  1483. if(aStack.creature->isDoubleWide())
  1484. {
  1485. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1486. {
  1487. case 0:
  1488. //reverseCreature(ID, aStack.position, true);
  1489. break;
  1490. case 1:
  1491. break;
  1492. case 2:
  1493. break;
  1494. case 3:
  1495. break;
  1496. case 4:
  1497. //reverseCreature(ID, aStack.position, true);
  1498. break;
  1499. case 5:
  1500. reverseCreature(attackingInfo->ID, aStack.position, true);
  1501. break;
  1502. case -1:
  1503. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1504. {
  1505. reverseCreature(attackingInfo->ID, aStack.position, true);
  1506. }
  1507. break;
  1508. }
  1509. }
  1510. else //else for if(aStack.creature->isDoubleWide())
  1511. {
  1512. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1513. {
  1514. case 0:
  1515. reverseCreature(attackingInfo->ID, aStack.position, true);
  1516. break;
  1517. case 1:
  1518. break;
  1519. case 2:
  1520. break;
  1521. case 3:
  1522. break;
  1523. case 4:
  1524. reverseCreature(attackingInfo->ID, aStack.position, true);
  1525. break;
  1526. case 5:
  1527. reverseCreature(attackingInfo->ID, aStack.position, true);
  1528. break;
  1529. }
  1530. }
  1531. }
  1532. else //if(aStack.attackerOwned)
  1533. {
  1534. if(aStack.creature->isDoubleWide())
  1535. {
  1536. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1537. {
  1538. case 0:
  1539. //reverseCreature(ID, aStack.position, true);
  1540. break;
  1541. case 1:
  1542. break;
  1543. case 2:
  1544. reverseCreature(attackingInfo->ID, aStack.position, true);
  1545. break;
  1546. case 3:
  1547. break;
  1548. case 4:
  1549. //reverseCreature(ID, aStack.position, true);
  1550. break;
  1551. case 5:
  1552. //reverseCreature(ID, aStack.position, true);
  1553. break;
  1554. case -1:
  1555. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1556. {
  1557. reverseCreature(attackingInfo->ID, aStack.position, true);
  1558. }
  1559. break;
  1560. }
  1561. }
  1562. else //else for if(aStack.creature->isDoubleWide())
  1563. {
  1564. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1565. {
  1566. case 0:
  1567. //reverseCreature(ID, aStack.position, true);
  1568. break;
  1569. case 1:
  1570. reverseCreature(attackingInfo->ID, aStack.position, true);
  1571. break;
  1572. case 2:
  1573. reverseCreature(attackingInfo->ID, aStack.position, true);
  1574. break;
  1575. case 3:
  1576. reverseCreature(attackingInfo->ID, aStack.position, true);
  1577. break;
  1578. case 4:
  1579. //reverseCreature(ID, aStack.position, true);
  1580. break;
  1581. case 5:
  1582. //reverseCreature(ID, aStack.position, true);
  1583. break;
  1584. }
  1585. }
  1586. }
  1587. attackingInfo->reversing = false;
  1588. creAnims[attackingInfo->ID]->setType(2);
  1589. delete attackingInfo;
  1590. attackingInfo = NULL;
  1591. }
  1592. if(attackingInfo)
  1593. {
  1594. attackingInfo->hitCount++;
  1595. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1596. attackingInfo->frame++;
  1597. }
  1598. }
  1599. }
  1600. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1601. {
  1602. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1603. //preparating background graphic with hexes and shaded hexes
  1604. blitAt(background, 0, 0, backgroundWithHexes);
  1605. if(printCellBorders)
  1606. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1607. if(printStackRange)
  1608. {
  1609. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1610. {
  1611. int i = shadedHexes[m]/17; //row
  1612. int j = shadedHexes[m]%17-1; //column
  1613. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1614. int y = 86 + 42 * i;
  1615. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1616. }
  1617. }
  1618. }
  1619. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1620. {
  1621. char tabh[200];
  1622. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1623. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1624. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1625. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1626. dmg);
  1627. if(killed > 0)
  1628. {
  1629. if(killed > 1)
  1630. {
  1631. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1632. }
  1633. else //killed == 1
  1634. {
  1635. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1636. }
  1637. }
  1638. console->addText(std::string(tabh));
  1639. }
  1640. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1641. {
  1642. if(to == NULL)
  1643. to = screen;
  1644. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1645. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1646. {
  1647. if(it->animStartDelay>0)
  1648. {
  1649. --(it->animStartDelay);
  1650. continue;
  1651. }
  1652. SDL_Rect dst;
  1653. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1654. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1655. dst.x = it->x;
  1656. dst.y = it->y;
  1657. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1658. //actualizing projectile
  1659. ++it->step;
  1660. if(it->step == it->lastStep)
  1661. {
  1662. toBeDeleted.insert(toBeDeleted.end(), it);
  1663. }
  1664. else
  1665. {
  1666. it->x += it->dx;
  1667. it->y += it->dy;
  1668. if(it->spin)
  1669. {
  1670. ++(it->frameNum);
  1671. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1672. }
  1673. }
  1674. }
  1675. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1676. {
  1677. projectiles.erase(*it);
  1678. }
  1679. }
  1680. void CBattleHero::show(SDL_Surface *to)
  1681. {
  1682. //animation of flag
  1683. if(flip)
  1684. {
  1685. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1686. }
  1687. else
  1688. {
  1689. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1690. }
  1691. ++flagAnimCount;
  1692. if(flagAnimCount%4==0)
  1693. {
  1694. ++flagAnim;
  1695. flagAnim %= flag->ourImages.size();
  1696. }
  1697. //animation of hero
  1698. int tick=-1;
  1699. for(int i=0; i<dh->ourImages.size(); ++i)
  1700. {
  1701. if(dh->ourImages[i].groupNumber==phase)
  1702. ++tick;
  1703. if(tick==image)
  1704. {
  1705. SDL_Rect posb = pos;
  1706. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1707. ++image;
  1708. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  1709. {
  1710. image = 0;
  1711. }
  1712. break;
  1713. }
  1714. }
  1715. }
  1716. void CBattleHero::activate()
  1717. {
  1718. ClickableL::activate();
  1719. }
  1720. void CBattleHero::deactivate()
  1721. {
  1722. ClickableL::deactivate();
  1723. }
  1724. void CBattleHero::setPhase(int newPhase)
  1725. {
  1726. phase = newPhase;
  1727. image = 0;
  1728. }
  1729. void CBattleHero::clickLeft(boost::logic::tribool down)
  1730. {
  1731. if(!down && myHero)
  1732. {
  1733. for(int it=0; it<BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  1734. {
  1735. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  1736. return;
  1737. }
  1738. CGI->curh->changeGraphic(0,0);
  1739. LOCPLINT->curint->deactivate();
  1740. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1741. spellWindow->activate();
  1742. LOCPLINT->objsToBlit.push_back(spellWindow);
  1743. }
  1744. }
  1745. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, unsigned char player, const CGHeroInstance * hero, const CBattleInterface * owner): phase(phaseG), image(imageG), flip(flipG), flagAnim(0), myHero(hero), myOwner(owner)
  1746. {
  1747. dh = CDefHandler::giveDef( defName );
  1748. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1749. {
  1750. if(flip)
  1751. {
  1752. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1753. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1754. dh->ourImages[i].bitmap = hlp;
  1755. }
  1756. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1757. }
  1758. dh->alphaTransformed = true;
  1759. if(flip)
  1760. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1761. else
  1762. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1763. //coloring flag and adding transparency
  1764. for(int i=0; i<flag->ourImages.size(); ++i)
  1765. {
  1766. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1767. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1768. }
  1769. }
  1770. CBattleHero::~CBattleHero()
  1771. {
  1772. delete dh;
  1773. delete flag;
  1774. }
  1775. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1776. {
  1777. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1778. ret.second = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
  1779. //counting x
  1780. if(attacker)
  1781. {
  1782. ret.first = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  1783. }
  1784. else
  1785. {
  1786. ret.first = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  1787. }
  1788. //shifting position for double - hex creatures
  1789. if(creature && creature->isDoubleWide())
  1790. {
  1791. if(attacker)
  1792. {
  1793. ret.first -= 42;
  1794. }
  1795. else
  1796. {
  1797. ret.first += 42;
  1798. }
  1799. }
  1800. //returning
  1801. return ret;
  1802. }
  1803. void CBattleHex::activate()
  1804. {
  1805. Hoverable::activate();
  1806. MotionInterested::activate();
  1807. ClickableL::activate();
  1808. ClickableR::activate();
  1809. }
  1810. void CBattleHex::deactivate()
  1811. {
  1812. Hoverable::deactivate();
  1813. MotionInterested::deactivate();
  1814. ClickableL::deactivate();
  1815. ClickableR::deactivate();
  1816. }
  1817. void CBattleHex::hover(bool on)
  1818. {
  1819. hovered = on;
  1820. Hoverable::hover(on);
  1821. if(!on && setAlterText)
  1822. {
  1823. myInterface->console->alterTxt = std::string();
  1824. setAlterText = false;
  1825. }
  1826. }
  1827. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1828. {
  1829. }
  1830. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1831. {
  1832. if(myInterface->cellShade)
  1833. {
  1834. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1835. {
  1836. strictHovered = false;
  1837. }
  1838. else //hovered pixel is inside hex
  1839. {
  1840. strictHovered = true;
  1841. }
  1842. }
  1843. if(hovered && strictHovered) //print attacked creature to console
  1844. {
  1845. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1846. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1847. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1848. {
  1849. char tabh[160];
  1850. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1851. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1852. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1853. myInterface->console->alterTxt = std::string(tabh);
  1854. setAlterText = true;
  1855. }
  1856. }
  1857. else if(setAlterText)
  1858. {
  1859. myInterface->console->alterTxt = std::string();
  1860. setAlterText = false;
  1861. }
  1862. }
  1863. void CBattleHex::clickLeft(boost::logic::tribool down)
  1864. {
  1865. if(!down && hovered && strictHovered) //we've been really clicked!
  1866. {
  1867. myInterface->hexLclicked(myNumber);
  1868. }
  1869. }
  1870. void CBattleHex::clickRight(boost::logic::tribool down)
  1871. {
  1872. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1873. if(hovered && strictHovered && stID!=-1)
  1874. {
  1875. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1876. if(!myst.alive()) return;
  1877. StackState *pom = NULL;
  1878. if(down)
  1879. {
  1880. pom = new StackState();
  1881. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1882. if(h)
  1883. {
  1884. pom->attackBonus = h->getPrimSkillLevel(0);
  1885. pom->defenseBonus = h->getPrimSkillLevel(1);
  1886. pom->luck = h->getCurrentLuck();
  1887. pom->morale = h->getCurrentMorale();
  1888. pom->shotsLeft = myst.shots;
  1889. }
  1890. pom->currentHealth = myst.firstHPleft;
  1891. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1892. ->activate();
  1893. }
  1894. delete pom;
  1895. }
  1896. }
  1897. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1898. {
  1899. }
  1900. CBattleConsole::~CBattleConsole()
  1901. {
  1902. texts.clear();
  1903. }
  1904. void CBattleConsole::show(SDL_Surface * to)
  1905. {
  1906. if(alterTxt.size())
  1907. {
  1908. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1909. }
  1910. else if(texts.size())
  1911. {
  1912. if(texts.size()==1)
  1913. {
  1914. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1915. }
  1916. else
  1917. {
  1918. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1919. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1920. }
  1921. }
  1922. }
  1923. bool CBattleConsole::addText(const std::string & text)
  1924. {
  1925. if(text.size()>70)
  1926. return false; //text too long!
  1927. int firstInToken = 0;
  1928. for(int i=0; i<text.size(); ++i) //tokenize
  1929. {
  1930. if(text[i] == 10)
  1931. {
  1932. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1933. firstInToken = i+1;
  1934. }
  1935. }
  1936. texts.push_back( text.substr(firstInToken, text.size()) );
  1937. lastShown = texts.size()-1;
  1938. return true;
  1939. }
  1940. void CBattleConsole::eraseText(unsigned int pos)
  1941. {
  1942. if(pos < texts.size())
  1943. {
  1944. texts.erase(texts.begin() + pos);
  1945. if(lastShown == texts.size())
  1946. --lastShown;
  1947. }
  1948. }
  1949. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  1950. {
  1951. if(pos >= texts.size()) //no such pos
  1952. return;
  1953. texts[pos] = text;
  1954. }
  1955. void CBattleConsole::scrollUp(unsigned int by)
  1956. {
  1957. if(lastShown > by)
  1958. lastShown -= by;
  1959. }
  1960. void CBattleConsole::scrollDown(unsigned int by)
  1961. {
  1962. if(lastShown + by < texts.size())
  1963. lastShown += by;
  1964. }
  1965. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1966. {
  1967. this->pos = pos;
  1968. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1969. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1970. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1971. SDL_FreeSurface(background);
  1972. background = pom;
  1973. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", SDLK_RETURN);
  1974. if(br.winner==0) //attacker won
  1975. {
  1976. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1977. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1978. }
  1979. else //if(br.winner==1)
  1980. {
  1981. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1982. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1983. }
  1984. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1985. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1986. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1987. std::string attackerName, defenderName;
  1988. if(owner->attackingHeroInstance) //a hero attacked
  1989. {
  1990. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1991. //setting attackerName
  1992. attackerName = owner->attackingHeroInstance->name;
  1993. }
  1994. else //a monster attacked
  1995. {
  1996. int bestMonsterID = -1;
  1997. int bestPower = 0;
  1998. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1999. {
  2000. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  2001. {
  2002. bestPower = CGI->creh->creatures[it->first].AIValue;
  2003. bestMonsterID = it->first;
  2004. }
  2005. }
  2006. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  2007. //setting attackerName
  2008. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  2009. }
  2010. if(owner->defendingHeroInstance) //a hero defended
  2011. {
  2012. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  2013. //setting defenderName
  2014. defenderName = owner->defendingHeroInstance->name;
  2015. }
  2016. else //a monster defended
  2017. {
  2018. int bestMonsterID = -1;
  2019. int bestPower = 0;
  2020. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  2021. {
  2022. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  2023. {
  2024. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  2025. bestMonsterID = it->second.first;
  2026. }
  2027. }
  2028. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  2029. //setting defenderName
  2030. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  2031. }
  2032. //printing attacker and defender's names
  2033. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  2034. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  2035. //printing casualities
  2036. for(int step = 0; step < 2; ++step)
  2037. {
  2038. if(br.casualties[step].size()==0)
  2039. {
  2040. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  2041. }
  2042. else
  2043. {
  2044. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  2045. int yPos = 344 + step*97;
  2046. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  2047. {
  2048. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  2049. std::stringstream amount;
  2050. amount<<it->second;
  2051. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  2052. xPos += 42;
  2053. }
  2054. }
  2055. }
  2056. //printing result description
  2057. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  2058. switch(br.result)
  2059. {
  2060. case 0: //normal victory
  2061. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2062. {
  2063. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  2064. }
  2065. else
  2066. {
  2067. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  2068. }
  2069. break;
  2070. case 1: //flee
  2071. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2072. {
  2073. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  2074. }
  2075. else
  2076. {
  2077. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  2078. }
  2079. break;
  2080. case 2: //surrender
  2081. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2082. {
  2083. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  2084. }
  2085. else
  2086. {
  2087. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  2088. }
  2089. break;
  2090. }
  2091. }
  2092. CBattleReslutWindow::~CBattleReslutWindow()
  2093. {
  2094. SDL_FreeSurface(background);
  2095. }
  2096. void CBattleReslutWindow::activate()
  2097. {
  2098. LOCPLINT->showingDialog->set(true);
  2099. exit->activate();
  2100. }
  2101. void CBattleReslutWindow::deactivate()
  2102. {
  2103. exit->deactivate();
  2104. }
  2105. void CBattleReslutWindow::show(SDL_Surface *to)
  2106. {
  2107. //evaluating to
  2108. if(!to)
  2109. to = screen;
  2110. SDL_BlitSurface(background, NULL, to, &pos);
  2111. exit->show(to);
  2112. }
  2113. void CBattleReslutWindow::bExitf()
  2114. {
  2115. LOCPLINT->battleResultQuited();
  2116. }
  2117. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  2118. {
  2119. pos = position;
  2120. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  2121. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2122. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[427].first)(3,CGI->generaltexth->zelp[427].first), CGI->generaltexth->zelp[427].second, false, "sysopchk.def", NULL, 185, 140, false);
  2123. viewGrid->select(owner->printCellBorders);
  2124. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[428].first)(3,CGI->generaltexth->zelp[428].first), CGI->generaltexth->zelp[428].second, false, "sysopchk.def", NULL, 185, 173, false);
  2125. movementShadow->select(owner->printStackRange);
  2126. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[429].first)(3,CGI->generaltexth->zelp[429].first), CGI->generaltexth->zelp[429].second, false, "sysopchk.def", NULL, 185, 207, false);
  2127. mouseShadow->select(owner->printMouseShadow);
  2128. animSpeeds = new CHighlightableButtonsGroup(0);
  2129. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def",188, 309, 1);
  2130. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def",252, 309, 2);
  2131. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",315, 309, 4);
  2132. animSpeeds->select(owner->getAnimSpeed(), 1);
  2133. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  2134. setToDefault = new AdventureMapButton (CGI->generaltexth->zelp[392].first, CGI->generaltexth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def");
  2135. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  2136. exit = new AdventureMapButton (CGI->generaltexth->zelp[393].first, CGI->generaltexth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def",SDLK_RETURN);
  2137. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  2138. //printing texts to background
  2139. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  2140. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  2141. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  2142. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  2143. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  2144. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  2145. //auto - combat options
  2146. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  2147. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  2148. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  2149. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  2150. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  2151. //creature info
  2152. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  2153. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  2154. //general options
  2155. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  2156. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  2157. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  2158. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  2159. //texts printed
  2160. }
  2161. CBattleOptionsWindow::~CBattleOptionsWindow()
  2162. {
  2163. SDL_FreeSurface(background);
  2164. delete setToDefault;
  2165. delete exit;
  2166. delete viewGrid;
  2167. delete movementShadow;
  2168. delete animSpeeds;
  2169. delete mouseShadow;
  2170. }
  2171. void CBattleOptionsWindow::activate()
  2172. {
  2173. setToDefault->activate();
  2174. exit->activate();
  2175. viewGrid->activate();
  2176. movementShadow->activate();
  2177. animSpeeds->activate();
  2178. mouseShadow->activate();
  2179. }
  2180. void CBattleOptionsWindow::deactivate()
  2181. {
  2182. setToDefault->deactivate();
  2183. exit->deactivate();
  2184. viewGrid->deactivate();
  2185. movementShadow->deactivate();
  2186. animSpeeds->deactivate();
  2187. mouseShadow->deactivate();
  2188. }
  2189. void CBattleOptionsWindow::show(SDL_Surface *to)
  2190. {
  2191. if(!to) //"evaluating" to
  2192. to = screen;
  2193. SDL_BlitSurface(background, NULL, to, &pos);
  2194. setToDefault->show(to);
  2195. exit->show(to);
  2196. viewGrid->show(to);
  2197. movementShadow->show(to);
  2198. animSpeeds->show(to);
  2199. mouseShadow->show(to);
  2200. }
  2201. void CBattleOptionsWindow::bDefaultf()
  2202. {
  2203. }
  2204. void CBattleOptionsWindow::bExitf()
  2205. {
  2206. deactivate();
  2207. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  2208. {
  2209. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  2210. {
  2211. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  2212. break;
  2213. }
  2214. }
  2215. delete this;
  2216. LOCPLINT->curint->activate();
  2217. }