CBattleInterface.cpp 87 KB

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