CBattleInterface.cpp 87 KB

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