CBattleInterface.cpp 77 KB

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