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