CBattleInterface.cpp 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859
  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!=5 && animType!=20 && 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. while(!creAnims[number]->onLastFrameInGroup())
  808. {
  809. show();
  810. CSDL_Ext::update();
  811. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  812. }
  813. creDir[number] = !creDir[number];
  814. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  815. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  816. creAnims[number]->pos.x = coords.first;
  817. //creAnims[number]->pos.y = coords.second;
  818. if(wideTrick && curs.creature->isDoubleWide())
  819. {
  820. if(curs.attackerOwned)
  821. {
  822. if(!creDir[number])
  823. creAnims[number]->pos.x -= 44;
  824. }
  825. else
  826. {
  827. if(creDir[number])
  828. creAnims[number]->pos.x += 44;
  829. }
  830. }
  831. creAnims[number]->setType(7);
  832. //firstFrame = creAnims[number]->getFrame();
  833. //for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(7) + firstFrame - 1; ++g)
  834. while(!creAnims[number]->onLastFrameInGroup())
  835. {
  836. show();
  837. CSDL_Ext::update();
  838. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  839. }
  840. creAnims[number]->setType(2);
  841. return true;
  842. }
  843. void CBattleInterface::handleStartMoving(int number)
  844. {
  845. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  846. {
  847. show();
  848. CSDL_Ext::update();
  849. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  850. if((animCount+1)%(4/animSpeed)==0)
  851. creAnims[number]->incrementFrame();
  852. }
  853. }
  854. void CBattleInterface::bOptionsf()
  855. {
  856. CGI->curh->changeGraphic(0,0);
  857. deactivate();
  858. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  859. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  860. optionsWin->activate();
  861. LOCPLINT->objsToBlit.push_back(optionsWin);
  862. }
  863. void CBattleInterface::bSurrenderf()
  864. {
  865. }
  866. void CBattleInterface::bFleef()
  867. {
  868. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  869. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  870. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  871. }
  872. void CBattleInterface::reallyFlee()
  873. {
  874. giveCommand(4,0,0);
  875. CGI->curh->changeGraphic(0, 0);
  876. }
  877. void CBattleInterface::bAutofightf()
  878. {
  879. }
  880. void CBattleInterface::bSpellf()
  881. {
  882. CGI->curh->changeGraphic(0,0);
  883. deactivate();
  884. const CGHeroInstance * chi = NULL;
  885. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  886. chi = attackingHeroInstance;
  887. else
  888. chi = defendingHeroInstance;
  889. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), chi);
  890. spellWindow->activate();
  891. LOCPLINT->objsToBlit.push_back(spellWindow);
  892. }
  893. void CBattleInterface::bWaitf()
  894. {
  895. giveCommand(8,0,activeStack);
  896. }
  897. void CBattleInterface::bDefencef()
  898. {
  899. giveCommand(3,0,activeStack);
  900. }
  901. void CBattleInterface::bConsoleUpf()
  902. {
  903. console->scrollUp();
  904. }
  905. void CBattleInterface::bConsoleDownf()
  906. {
  907. console->scrollDown();
  908. }
  909. void CBattleInterface::newStack(CStack stack)
  910. {
  911. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  912. creAnims[stack.ID]->setType(2);
  913. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  914. }
  915. void CBattleInterface::stackRemoved(CStack stack)
  916. {
  917. delete creAnims[stack.ID];
  918. creAnims.erase(stack.ID);
  919. }
  920. void CBattleInterface::stackActivated(int number)
  921. {
  922. //givenCommand = NULL;
  923. activeStack = number;
  924. myTurn = true;
  925. redrawBackgroundWithHexes(number);
  926. bWait->block(vstd::contains(LOCPLINT->cb->battleGetStackByID(number)->state,WAITING)); //block waiting button if stack has been already waiting
  927. //block cast spell button if hero doesn't have a spellbook
  928. if(attackingHeroInstance && attackingHeroInstance->tempOwner==LOCPLINT->cb->battleGetStackByID(number)->owner)
  929. {
  930. bSpell->block(!attackingHeroInstance->getArt(17));
  931. }
  932. else if(defendingHeroInstance && defendingHeroInstance->tempOwner==LOCPLINT->cb->battleGetStackByID(number)->owner)
  933. {
  934. bSpell->block(!defendingHeroInstance->getArt(17));
  935. }
  936. }
  937. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving, int distance)
  938. {
  939. bool startMoving = creAnims[number]->getType()==20;
  940. //a few useful variables
  941. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  942. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  943. int hexWbase = 44, hexHbase = 42;
  944. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  945. CStack * movedStack = LOCPLINT->cb->battleGetStackByID(number);
  946. std::pair<int, int> begPosition = CBattleHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack->creature);
  947. std::pair<int, int> endPosition = CBattleHex::getXYUnitAnim(destHex, movedStack->attackerOwned, movedStack->creature);
  948. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  949. {
  950. handleStartMoving(number);
  951. }
  952. if(moveStarted)
  953. {
  954. CGI->curh->hide();
  955. creAnims[number]->setType(0);
  956. moveStarted = false;
  957. }
  958. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  959. float stepX=0.0, stepY=0.0; //how far stack is moved in one frame; calculated later
  960. //reverse unit if necessary
  961. if((begPosition.first > endPosition.first) && creDir[number] == true)
  962. {
  963. reverseCreature(number, curStackPos, twoTiles);
  964. }
  965. else if ((begPosition.first < endPosition.first) && creDir[number] == false)
  966. {
  967. reverseCreature(number, curStackPos, twoTiles);
  968. }
  969. if(creAnims[number]->getType() != 0)
  970. {
  971. creAnims[number]->setType(0);
  972. }
  973. //unit reversed
  974. //step shift calculation
  975. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  976. if(mutPos == -1 && movedStack->creature->isFlying())
  977. {
  978. steps *= distance;
  979. stepX = (endPosition.first - (float)begPosition.first)/steps;
  980. stepY = (endPosition.second - (float)begPosition.second)/steps;
  981. }
  982. else
  983. {
  984. switch(mutPos)
  985. {
  986. case 0:
  987. stepX = (-1.0)*((float)hexWbase)/(2.0f*steps);
  988. stepY = (-1.0)*((float)hexHbase)/((float)steps);
  989. break;
  990. case 1:
  991. stepX = ((float)hexWbase)/(2.0f*steps);
  992. stepY = (-1.0)*((float)hexHbase)/((float)steps);
  993. break;
  994. case 2:
  995. stepX = ((float)hexWbase)/((float)steps);
  996. stepY = 0.0;
  997. break;
  998. case 3:
  999. stepX = ((float)hexWbase)/(2.0f*steps);
  1000. stepY = ((float)hexHbase)/((float)steps);
  1001. break;
  1002. case 4:
  1003. stepX = (-1.0)*((float)hexWbase)/(2.0f*steps);
  1004. stepY = ((float)hexHbase)/((float)steps);
  1005. break;
  1006. case 5:
  1007. stepX = (-1.0)*((float)hexWbase)/((float)steps);
  1008. stepY = 0.0;
  1009. break;
  1010. }
  1011. }
  1012. //step shifts calculated
  1013. //switch(mutPos) //reverse unit if necessary
  1014. //{
  1015. //case 0: case 4: case 5:
  1016. // if(creDir[number] == true)
  1017. // reverseCreature(number, curStackPos, twoTiles);
  1018. // break;
  1019. //case 1: case 2: case 3:
  1020. // if(creDir[number] == false)
  1021. // reverseCreature(number, curStackPos, twoTiles);
  1022. // break;
  1023. //}
  1024. //moving instructions
  1025. for(int i=0; i<steps; ++i)
  1026. {
  1027. posX += stepX;
  1028. creAnims[number]->pos.x = posX;
  1029. posY += stepY;
  1030. creAnims[number]->pos.y = posY;
  1031. show();
  1032. CSDL_Ext::update();
  1033. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1034. }
  1035. //unit moved
  1036. if(endMoving) //animation of ending move
  1037. {
  1038. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  1039. {
  1040. creAnims[number]->setType(21);
  1041. //for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  1042. while(!creAnims[number]->onLastFrameInGroup())
  1043. {
  1044. show();
  1045. CSDL_Ext::update();
  1046. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1047. }
  1048. }
  1049. creAnims[number]->setType(2); //resetting to default
  1050. CGI->curh->show();
  1051. }
  1052. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  1053. if(endMoving) //resetting to default
  1054. {
  1055. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  1056. reverseCreature(number, destHex, twoTiles);
  1057. }
  1058. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  1059. creAnims[number]->pos.x = coords.first;
  1060. if(!endMoving && twoTiles && (curs.owner == attackingHeroInstance->tempOwner) && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big attacker creature is reversed
  1061. creAnims[number]->pos.x -= 44;
  1062. else if(!endMoving && twoTiles && (curs.owner != attackingHeroInstance->tempOwner) && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big defender creature is reversed
  1063. creAnims[number]->pos.x += 44;
  1064. creAnims[number]->pos.y = coords.second;
  1065. }
  1066. void CBattleInterface::stacksAreAttacked(std::vector<CBattleInterface::SStackAttackedInfo> attackedInfos)
  1067. {
  1068. //restoring default state of battleWindow by calling show func
  1069. while(true)
  1070. {
  1071. show();
  1072. CSDL_Ext::update();
  1073. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1074. //checking break conditions
  1075. bool break_loop = true;
  1076. for(size_t g=0; g<attackedInfos.size(); ++g)
  1077. {
  1078. if(creAnims[attackedInfos[g].ID]->getType() != 2)
  1079. {
  1080. break_loop = false;
  1081. }
  1082. if(attackingInfo && attackingInfo->IDby == attackedInfos[g].IDby)
  1083. {
  1084. break_loop = false;
  1085. }
  1086. }
  1087. if(break_loop) break;
  1088. }
  1089. if(attackedInfos.size() == 1 && attackedInfos[0].byShooting) //delay hit animation
  1090. {
  1091. CStack attacker = *LOCPLINT->cb->battleGetStackByID(attackedInfos[0].IDby);
  1092. while(true)
  1093. {
  1094. bool found = false;
  1095. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  1096. {
  1097. if(it->creID == attacker.creature->idNumber)
  1098. {
  1099. found = true;
  1100. break;
  1101. }
  1102. }
  1103. if(!found)
  1104. break;
  1105. else
  1106. {
  1107. show();
  1108. CSDL_Ext::update();
  1109. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1110. }
  1111. }
  1112. }
  1113. //initializing
  1114. int maxLen = 0;
  1115. for(size_t g=0; g<attackedInfos.size(); ++g)
  1116. {
  1117. if(attackedInfos[g].killed)
  1118. {
  1119. creAnims[attackedInfos[g].ID]->setType(5); //death
  1120. }
  1121. else
  1122. {
  1123. creAnims[attackedInfos[g].ID]->setType(3); //getting hit
  1124. }
  1125. }
  1126. //main showing loop
  1127. bool continueLoop = true;
  1128. while(continueLoop)
  1129. {
  1130. show();
  1131. CSDL_Ext::update();
  1132. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1133. for(size_t g=0; g<attackedInfos.size(); ++g)
  1134. {
  1135. if((animCount+1)%(4/animSpeed)==0 && !creAnims[attackedInfos[g].ID]->onLastFrameInGroup())
  1136. {
  1137. creAnims[attackedInfos[g].ID]->incrementFrame();
  1138. }
  1139. if(creAnims[attackedInfos[g].ID]->onLastFrameInGroup() && creAnims[attackedInfos[g].ID]->getType() == 3)
  1140. creAnims[attackedInfos[g].ID]->setType(2);
  1141. }
  1142. bool isAnotherOne = false; //if true, there is a stack whose hit/death anim must be continued
  1143. for(size_t g=0; g<attackedInfos.size(); ++g)
  1144. {
  1145. if(!creAnims[attackedInfos[g].ID]->onLastFrameInGroup())
  1146. {
  1147. isAnotherOne = true;
  1148. break;
  1149. }
  1150. }
  1151. if(!isAnotherOne)
  1152. continueLoop = false;
  1153. }
  1154. //restoring animType
  1155. for(size_t g=0; g<attackedInfos.size(); ++g)
  1156. {
  1157. if(creAnims[attackedInfos[g].ID]->getType() == 3)
  1158. creAnims[attackedInfos[g].ID]->setType(2);
  1159. }
  1160. //printing info to console
  1161. for(size_t g=0; g<attackedInfos.size(); ++g)
  1162. {
  1163. if(attackedInfos[g].IDby!=-1)
  1164. printConsoleAttacked(attackedInfos[g].ID, attackedInfos[g].dmg, attackedInfos[g].amountKilled, attackedInfos[g].IDby);
  1165. }
  1166. }
  1167. void CBattleInterface::stackAttacking(int ID, int dest)
  1168. {
  1169. if(attackingInfo == NULL && creAnims[ID]->getType() == 20)
  1170. {
  1171. handleStartMoving(ID);
  1172. creAnims[ID]->setType(2);
  1173. }
  1174. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  1175. {
  1176. show();
  1177. CSDL_Ext::update();
  1178. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1179. }
  1180. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  1181. int reversedShift = 0; //shift of attacking stack's position due to reversing
  1182. if(aStack.attackerOwned)
  1183. {
  1184. if(aStack.creature->isDoubleWide())
  1185. {
  1186. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1187. {
  1188. case 0:
  1189. //reverseCreature(ID, aStack.position, true);
  1190. break;
  1191. case 1:
  1192. break;
  1193. case 2:
  1194. break;
  1195. case 3:
  1196. break;
  1197. case 4:
  1198. //reverseCreature(ID, aStack.position, true);
  1199. break;
  1200. case 5:
  1201. reverseCreature(ID, aStack.position, true);
  1202. break;
  1203. case -1:
  1204. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1205. {
  1206. reverseCreature(ID, aStack.position, true);
  1207. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1208. }
  1209. break;
  1210. }
  1211. }
  1212. else //else for if(aStack.creature->isDoubleWide())
  1213. {
  1214. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1215. {
  1216. case 0:
  1217. reverseCreature(ID, aStack.position, true);
  1218. break;
  1219. case 1:
  1220. break;
  1221. case 2:
  1222. break;
  1223. case 3:
  1224. break;
  1225. case 4:
  1226. reverseCreature(ID, aStack.position, true);
  1227. break;
  1228. case 5:
  1229. reverseCreature(ID, aStack.position, true);
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. else //if(aStack.attackerOwned)
  1235. {
  1236. if(aStack.creature->isDoubleWide())
  1237. {
  1238. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1239. {
  1240. case 0:
  1241. //reverseCreature(ID, aStack.position, true);
  1242. break;
  1243. case 1:
  1244. break;
  1245. case 2:
  1246. reverseCreature(ID, aStack.position, true);
  1247. break;
  1248. case 3:
  1249. break;
  1250. case 4:
  1251. //reverseCreature(ID, aStack.position, true);
  1252. break;
  1253. case 5:
  1254. //reverseCreature(ID, aStack.position, true);
  1255. break;
  1256. case -1:
  1257. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1258. {
  1259. reverseCreature(ID, aStack.position, true);
  1260. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1261. }
  1262. break;
  1263. }
  1264. }
  1265. else //else for if(aStack.creature->isDoubleWide())
  1266. {
  1267. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1268. {
  1269. case 0:
  1270. //reverseCreature(ID, aStack.position, true);
  1271. break;
  1272. case 1:
  1273. reverseCreature(ID, aStack.position, true);
  1274. break;
  1275. case 2:
  1276. reverseCreature(ID, aStack.position, true);
  1277. break;
  1278. case 3:
  1279. reverseCreature(ID, aStack.position, true);
  1280. break;
  1281. case 4:
  1282. //reverseCreature(ID, aStack.position, true);
  1283. break;
  1284. case 5:
  1285. //reverseCreature(ID, aStack.position, true);
  1286. break;
  1287. }
  1288. }
  1289. }
  1290. attackingInfo = new CAttHelper;
  1291. attackingInfo->dest = dest;
  1292. attackingInfo->frame = 0;
  1293. attackingInfo->hitCount = 0;
  1294. attackingInfo->ID = ID;
  1295. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  1296. attackingInfo->reversing = false;
  1297. attackingInfo->posShiftDueToDist = reversedShift;
  1298. attackingInfo->shooting = false;
  1299. switch(BattleInfo::mutualPosition(aStack.position + reversedShift, dest)) //attack direction
  1300. {
  1301. case 0:
  1302. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1303. break;
  1304. case 1:
  1305. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1306. break;
  1307. case 2:
  1308. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1309. break;
  1310. case 3:
  1311. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1312. break;
  1313. case 4:
  1314. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1315. break;
  1316. case 5:
  1317. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1318. break;
  1319. default:
  1320. tlog1<<"Critical Error! Wrong dest in stackAttacking! dest: "<<dest<<" attacking stack pos: "<<aStack.position<<" reversed shift: "<<reversedShift<<std::endl;
  1321. }
  1322. }
  1323. void CBattleInterface::newRound(int number)
  1324. {
  1325. console->addText(CGI->generaltexth->allTexts[412]);
  1326. }
  1327. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  1328. {
  1329. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  1330. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1331. ba->actionType = action;
  1332. ba->destinationTile = tile;
  1333. ba->stackNumber = stack;
  1334. ba->additionalInfo = additional;
  1335. givenCommand->setn(ba);
  1336. myTurn = false;
  1337. activeStack = -1;
  1338. }
  1339. bool CBattleInterface::isTileAttackable(const int & number) const
  1340. {
  1341. for(size_t b=0; b<shadedHexes.size(); ++b)
  1342. {
  1343. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  1344. return true;
  1345. }
  1346. return false;
  1347. }
  1348. void CBattleInterface::hexLclicked(int whichOne)
  1349. {
  1350. if((whichOne%BFIELD_WIDTH)!=0 && (whichOne%BFIELD_WIDTH)!=(BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  1351. {
  1352. if(!myTurn)
  1353. return; //we are not permit to do anything
  1354. if(spellDestSelectMode)
  1355. {
  1356. //checking destination
  1357. bool allowCasting = true;
  1358. switch(spellSelMode)
  1359. {
  1360. case 1:
  1361. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1362. allowCasting = false;
  1363. break;
  1364. case 2:
  1365. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1366. allowCasting = false;
  1367. break;
  1368. case 3:
  1369. if(!LOCPLINT->cb->battleGetStackByPos(whichOne))
  1370. allowCasting = false;
  1371. break;
  1372. case 4: //TODO: implement this case
  1373. break;
  1374. }
  1375. //destination checked
  1376. if(allowCasting)
  1377. {
  1378. spellToCast->destinationTile = whichOne;
  1379. LOCPLINT->cb->battleMakeAction(spellToCast);
  1380. delete spellToCast;
  1381. spellToCast = NULL;
  1382. spellDestSelectMode = false;
  1383. CGI->curh->changeGraphic(1, 6);
  1384. }
  1385. }
  1386. else
  1387. {
  1388. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  1389. if(!dest || !dest->alive()) //no creature at that tile
  1390. {
  1391. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  1392. {
  1393. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1394. if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isDoubleWide())
  1395. {
  1396. std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
  1397. int shiftedDest = whichOne + (LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned ? 1 : -1);
  1398. if(vstd::contains(acc, whichOne))
  1399. giveCommand(2,whichOne,activeStack);
  1400. else if(vstd::contains(acc, shiftedDest))
  1401. giveCommand(2,shiftedDest,activeStack);
  1402. }
  1403. else
  1404. {
  1405. giveCommand(2,whichOne,activeStack);
  1406. }
  1407. }
  1408. }
  1409. else if(dest->owner != attackingHeroInstance->tempOwner
  1410. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne) ) //shooting
  1411. {
  1412. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1413. giveCommand(7,whichOne,activeStack);
  1414. }
  1415. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  1416. {
  1417. switch(CGI->curh->number)
  1418. {
  1419. case 12:
  1420. giveCommand(6,whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH+1 ),activeStack,whichOne);
  1421. break;
  1422. case 7:
  1423. giveCommand(6,whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH-1 : BFIELD_WIDTH ),activeStack,whichOne);
  1424. break;
  1425. case 8:
  1426. if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isDoubleWide() && !LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
  1427. {
  1428. std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
  1429. if(vstd::contains(acc, whichOne))
  1430. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1431. else
  1432. giveCommand(6,whichOne - 2,activeStack,whichOne);
  1433. }
  1434. else
  1435. {
  1436. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1437. }
  1438. break;
  1439. case 9:
  1440. giveCommand(6,whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH+1 : BFIELD_WIDTH ),activeStack,whichOne);
  1441. break;
  1442. case 10:
  1443. giveCommand(6,whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH-1 ),activeStack,whichOne);
  1444. break;
  1445. case 11:
  1446. if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isDoubleWide() && LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
  1447. {
  1448. std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
  1449. if(vstd::contains(acc, whichOne))
  1450. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1451. else
  1452. giveCommand(6,whichOne + 2,activeStack,whichOne);
  1453. }
  1454. else
  1455. {
  1456. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1457. }
  1458. break;
  1459. }
  1460. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1461. }
  1462. }
  1463. }
  1464. }
  1465. void CBattleInterface::stackIsShooting(int ID, int dest)
  1466. {
  1467. if(attackingInfo != NULL)
  1468. {
  1469. return; //something went wrong
  1470. }
  1471. //projectile
  1472. float projectileAngle; //in radians; if positive, projectiles goes up
  1473. 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)
  1474. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1475. projectileAngle = atan2(float(abs(dest - fromHex)/BFIELD_WIDTH), float(abs(dest - fromHex)%BFIELD_WIDTH));
  1476. if(fromHex < dest)
  1477. projectileAngle = -projectileAngle;
  1478. SProjectileInfo spi;
  1479. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1480. spi.reverse = !LOCPLINT->cb->battleGetStackByID(ID)->attackerOwned;
  1481. spi.step = 0;
  1482. spi.frameNum = 0;
  1483. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1484. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1485. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1486. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1487. if(projectileAngle > straightAngle) //upper shot
  1488. {
  1489. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1490. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1491. }
  1492. else if(projectileAngle < -straightAngle) //lower shot
  1493. {
  1494. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1495. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1496. }
  1497. else //straight shot
  1498. {
  1499. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1500. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1501. }
  1502. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1503. if(spi.lastStep == 0)
  1504. spi.lastStep = 1;
  1505. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1506. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1507. //set starting frame
  1508. if(spi.spin)
  1509. {
  1510. spi.frameNum = 0;
  1511. }
  1512. else
  1513. {
  1514. 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);
  1515. }
  1516. //set delay
  1517. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1518. projectiles.push_back(spi);
  1519. //attack aniamtion
  1520. attackingInfo = new CAttHelper;
  1521. attackingInfo->dest = dest;
  1522. attackingInfo->frame = 0;
  1523. attackingInfo->hitCount = 0;
  1524. attackingInfo->ID = ID;
  1525. attackingInfo->reversing = false;
  1526. attackingInfo->posShiftDueToDist = 0;
  1527. attackingInfo->shooting = true;
  1528. if(projectileAngle > straightAngle) //upper shot
  1529. attackingInfo->shootingGroup = 14;
  1530. else if(projectileAngle < -straightAngle) //lower shot
  1531. attackingInfo->shootingGroup = 16;
  1532. else //straight shot
  1533. attackingInfo->shootingGroup = 15;
  1534. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1535. }
  1536. void CBattleInterface::battleFinished(const BattleResult& br)
  1537. {
  1538. deactivate();
  1539. CGI->curh->changeGraphic(0,0);
  1540. SDL_Rect temp_rect = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  1541. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1542. resWindow->activate();
  1543. }
  1544. void CBattleInterface::spellCasted(SpellCasted * sc)
  1545. {
  1546. std::vector< std::string > anims; //for magic arrow and ice bolt
  1547. switch(sc->id)
  1548. {
  1549. case 15: //magic arrow
  1550. {
  1551. //initialization of anims
  1552. 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");
  1553. }
  1554. case 16: //ice bolt
  1555. {
  1556. if(anims.size() == 0) //initialiaztion of anims
  1557. {
  1558. 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");
  1559. }
  1560. } //end of ice bolt only part
  1561. { //common ice bolt and magic arrow part
  1562. //initial variables
  1563. std::string animToDisplay;
  1564. std::pair<int, int> srccoord = sc->side ? std::make_pair(770, 60) : std::make_pair(30, 60);
  1565. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile)->creature); //position attacked by arrow
  1566. destcoord.first += 250; destcoord.second += 240;
  1567. //animation angle
  1568. float angle = atan2(float(destcoord.first - srccoord.first), float(destcoord.second - srccoord.second));
  1569. //choosing animation by angle
  1570. if(angle > 1.50)
  1571. animToDisplay = anims[0];
  1572. else if(angle > 1.20)
  1573. animToDisplay = anims[1];
  1574. else if(angle > 0.90)
  1575. animToDisplay = anims[2];
  1576. else if(angle > 0.60)
  1577. animToDisplay = anims[3];
  1578. else
  1579. animToDisplay = anims[4];
  1580. //displaying animation
  1581. int steps = sqrt((float)((destcoord.first - srccoord.first)*(destcoord.first - srccoord.first) + (destcoord.second - srccoord.second) * (destcoord.second - srccoord.second))) / 40;
  1582. if(steps <= 0)
  1583. steps = 1;
  1584. CDefHandler * animDef = CDefHandler::giveDef(animToDisplay);
  1585. int dx = (destcoord.first - srccoord.first - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.second - srccoord.second - animDef->ourImages[0].bitmap->h)/steps;
  1586. SDL_Rect buf;
  1587. SDL_GetClipRect(screen, &buf);
  1588. SDL_SetClipRect(screen, &pos); //setting rect we can blit to
  1589. for(int g=0; g<steps; ++g)
  1590. {
  1591. show();
  1592. SDL_Rect & srcr = animDef->ourImages[g%animDef->ourImages.size()].bitmap->clip_rect;
  1593. SDL_Rect dstr = genRect(srcr.h, srcr.w, srccoord.first + g*dx, srccoord.second + g*dy);
  1594. SDL_BlitSurface(animDef->ourImages[g%animDef->ourImages.size()].bitmap, &srcr, screen, &dstr);
  1595. CSDL_Ext::update();
  1596. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1597. }
  1598. SDL_SetClipRect(screen, &buf); //restoring previous clip rect
  1599. int b=0; //TODO use me
  1600. break; //for 15 and 16 cases
  1601. }
  1602. case 17: //lightning bolt
  1603. {
  1604. displayEffect(1, sc->tile);
  1605. break;
  1606. }
  1607. case 18: //implosion
  1608. {
  1609. displayEffect(10, sc->tile);
  1610. break;
  1611. }
  1612. case 27: //shield
  1613. {
  1614. displayEffect(27, sc->tile);
  1615. break;
  1616. }
  1617. case 28: //air shield
  1618. {
  1619. displayEffect(2, sc->tile);
  1620. break;
  1621. }
  1622. case 41: //bless
  1623. {
  1624. displayEffect(36, sc->tile);
  1625. break;
  1626. }
  1627. case 42: //curse
  1628. {
  1629. displayEffect(40, sc->tile);
  1630. break;
  1631. }
  1632. case 43: //bloodlust
  1633. {
  1634. displayEffect(4, sc->tile);
  1635. //TODO: give better animation for this spell
  1636. break;
  1637. }
  1638. case 45: //weakness
  1639. {
  1640. displayEffect(56, sc->tile);
  1641. //TODO: give better animation for this spell
  1642. break;
  1643. }
  1644. case 46: //stone skin
  1645. {
  1646. displayEffect(54, sc->tile);
  1647. break;
  1648. }
  1649. case 48: //prayer
  1650. {
  1651. displayEffect(0, sc->tile);
  1652. break;
  1653. }
  1654. case 49: //mirth
  1655. {
  1656. displayEffect(20, sc->tile);
  1657. break;
  1658. }
  1659. case 50: //sorrow
  1660. {
  1661. displayEffect(30, sc->tile);
  1662. break;
  1663. }
  1664. case 51: //fortune
  1665. {
  1666. displayEffect(18, sc->tile);
  1667. break;
  1668. }
  1669. case 52: //misfortune
  1670. {
  1671. displayEffect(48, sc->tile);
  1672. break;
  1673. }
  1674. case 53: //haste
  1675. {
  1676. displayEffect(31, sc->tile);
  1677. break;
  1678. }
  1679. case 54: //slow
  1680. {
  1681. displayEffect(19, sc->tile);
  1682. break;
  1683. }
  1684. case 56: //frenzy
  1685. {
  1686. displayEffect(17, sc->tile);
  1687. break;
  1688. }
  1689. case 61: //forgetfulness
  1690. {
  1691. displayEffect(42, sc->tile);
  1692. break;
  1693. }
  1694. }
  1695. }
  1696. void CBattleInterface::castThisSpell(int spellID)
  1697. {
  1698. BattleAction * ba = new BattleAction;
  1699. ba->actionType = 1;
  1700. ba->additionalInfo = spellID; //spell number
  1701. ba->destinationTile = -1;
  1702. ba->stackNumber = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? -1 : -2;
  1703. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1704. spellToCast = ba;
  1705. spellDestSelectMode = true;
  1706. //choosing possible tragets
  1707. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? attackingHeroInstance : attackingHeroInstance;
  1708. spellSelMode = 0;
  1709. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET") != std::string::npos)
  1710. {
  1711. switch(CGI->spellh->spells[spellID].positiveness)
  1712. {
  1713. case -1 :
  1714. spellSelMode = 2;
  1715. break;
  1716. case 0:
  1717. spellSelMode = 3;
  1718. break;
  1719. case 1:
  1720. spellSelMode = 1;
  1721. break;
  1722. }
  1723. }
  1724. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET_2") != std::string::npos)
  1725. {
  1726. if(castingHero && castingHero->getSpellSecLevel(spellID) < 3)
  1727. {
  1728. switch(CGI->spellh->spells[spellID].positiveness)
  1729. {
  1730. case -1 :
  1731. spellSelMode = 2;
  1732. break;
  1733. case 0:
  1734. spellSelMode = 3;
  1735. break;
  1736. case 1:
  1737. spellSelMode = 1;
  1738. break;
  1739. }
  1740. }
  1741. else
  1742. {
  1743. spellSelMode = -1;
  1744. }
  1745. }
  1746. if(spellSelMode == -1) //user does not have to select location
  1747. {
  1748. spellToCast->destinationTile = -1;
  1749. LOCPLINT->cb->battleMakeAction(spellToCast);
  1750. delete spellToCast;
  1751. spellToCast = NULL;
  1752. spellDestSelectMode = false;
  1753. CGI->curh->changeGraphic(1, 6);
  1754. }
  1755. else
  1756. {
  1757. CGI->curh->changeGraphic(3, 0);
  1758. }
  1759. }
  1760. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  1761. {
  1762. if(graphics->battleACToDef[effect].size() != 0)
  1763. {
  1764. SBattleEffect be;
  1765. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1766. be.frame = 0;
  1767. be.maxFrame = be.anim->ourImages.size();
  1768. be.x = 22 * ( ((destTile/BFIELD_WIDTH) + 1)%2 ) + 44 * (destTile % BFIELD_WIDTH) + 45;
  1769. be.y = 105 + 42 * (destTile/BFIELD_WIDTH);
  1770. if(effect != 1 && effect != 0)
  1771. {
  1772. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1773. be.y -= be.anim->ourImages[0].bitmap->h/2;
  1774. }
  1775. else if(effect == 1)
  1776. {
  1777. be.x -= be.anim->ourImages[0].bitmap->w;
  1778. be.y -= be.anim->ourImages[0].bitmap->h;
  1779. }
  1780. else if (effect == 0)
  1781. {
  1782. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1783. be.y -= be.anim->ourImages[0].bitmap->h;
  1784. }
  1785. battleEffects.push_back(be);
  1786. }
  1787. //battleEffects
  1788. }
  1789. void CBattleInterface::setAnimSpeed(int set)
  1790. {
  1791. animSpeed = set;
  1792. }
  1793. int CBattleInterface::getAnimSpeed() const
  1794. {
  1795. return animSpeed;
  1796. }
  1797. float CBattleInterface::getAnimSpeedMultiplier() const
  1798. {
  1799. switch(animSpeed)
  1800. {
  1801. case 1:
  1802. return 3.5f;
  1803. case 2:
  1804. return 2.2f;
  1805. case 4:
  1806. return 1.0f;
  1807. default:
  1808. return 0.0f;
  1809. }
  1810. }
  1811. void CBattleInterface::attackingShowHelper()
  1812. {
  1813. if(attackingInfo && !attackingInfo->reversing)
  1814. {
  1815. if(attackingInfo->frame == 0)
  1816. {
  1817. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1818. if(attackingInfo->shooting)
  1819. {
  1820. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1821. }
  1822. else
  1823. {
  1824. if(aStack.creature->isDoubleWide())
  1825. {
  1826. switch(BattleInfo::mutualPosition(aStack.position+attackingInfo->posShiftDueToDist, attackingInfo->dest)) //attack direction
  1827. {
  1828. case 0:
  1829. creAnims[attackingInfo->ID]->setType(11);
  1830. break;
  1831. case 1:
  1832. creAnims[attackingInfo->ID]->setType(11);
  1833. break;
  1834. case 2:
  1835. creAnims[attackingInfo->ID]->setType(12);
  1836. break;
  1837. case 3:
  1838. creAnims[attackingInfo->ID]->setType(13);
  1839. break;
  1840. case 4:
  1841. creAnims[attackingInfo->ID]->setType(13);
  1842. break;
  1843. case 5:
  1844. creAnims[attackingInfo->ID]->setType(12);
  1845. break;
  1846. }
  1847. }
  1848. else //else for if(aStack.creature->isDoubleWide())
  1849. {
  1850. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1851. {
  1852. case 0:
  1853. creAnims[attackingInfo->ID]->setType(11);
  1854. break;
  1855. case 1:
  1856. creAnims[attackingInfo->ID]->setType(11);
  1857. break;
  1858. case 2:
  1859. creAnims[attackingInfo->ID]->setType(12);
  1860. break;
  1861. case 3:
  1862. creAnims[attackingInfo->ID]->setType(13);
  1863. break;
  1864. case 4:
  1865. creAnims[attackingInfo->ID]->setType(13);
  1866. break;
  1867. case 5:
  1868. creAnims[attackingInfo->ID]->setType(12);
  1869. break;
  1870. }
  1871. }
  1872. }
  1873. }
  1874. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1875. {
  1876. attackingInfo->reversing = true;
  1877. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1878. if(aStackp == NULL)
  1879. return;
  1880. CStack aStack = *aStackp;
  1881. if(aStack.attackerOwned)
  1882. {
  1883. if(aStack.creature->isDoubleWide())
  1884. {
  1885. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1886. {
  1887. case 0:
  1888. //reverseCreature(ID, aStack.position, true);
  1889. break;
  1890. case 1:
  1891. break;
  1892. case 2:
  1893. break;
  1894. case 3:
  1895. break;
  1896. case 4:
  1897. //reverseCreature(ID, aStack.position, true);
  1898. break;
  1899. case 5:
  1900. reverseCreature(attackingInfo->ID, aStack.position, true);
  1901. break;
  1902. case -1:
  1903. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1904. {
  1905. reverseCreature(attackingInfo->ID, aStack.position, true);
  1906. }
  1907. break;
  1908. }
  1909. }
  1910. else //else for if(aStack.creature->isDoubleWide())
  1911. {
  1912. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1913. {
  1914. case 0:
  1915. reverseCreature(attackingInfo->ID, aStack.position, true);
  1916. break;
  1917. case 1:
  1918. break;
  1919. case 2:
  1920. break;
  1921. case 3:
  1922. break;
  1923. case 4:
  1924. reverseCreature(attackingInfo->ID, aStack.position, true);
  1925. break;
  1926. case 5:
  1927. reverseCreature(attackingInfo->ID, aStack.position, true);
  1928. break;
  1929. }
  1930. }
  1931. }
  1932. else //if(aStack.attackerOwned)
  1933. {
  1934. if(aStack.creature->isDoubleWide())
  1935. {
  1936. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1937. {
  1938. case 0:
  1939. //reverseCreature(ID, aStack.position, true);
  1940. break;
  1941. case 1:
  1942. break;
  1943. case 2:
  1944. reverseCreature(attackingInfo->ID, aStack.position, true);
  1945. break;
  1946. case 3:
  1947. break;
  1948. case 4:
  1949. //reverseCreature(ID, aStack.position, true);
  1950. break;
  1951. case 5:
  1952. //reverseCreature(ID, aStack.position, true);
  1953. break;
  1954. case -1:
  1955. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1956. {
  1957. reverseCreature(attackingInfo->ID, aStack.position, true);
  1958. }
  1959. break;
  1960. }
  1961. }
  1962. else //else for if(aStack.creature->isDoubleWide())
  1963. {
  1964. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1965. {
  1966. case 0:
  1967. //reverseCreature(ID, aStack.position, true);
  1968. break;
  1969. case 1:
  1970. reverseCreature(attackingInfo->ID, aStack.position, true);
  1971. break;
  1972. case 2:
  1973. reverseCreature(attackingInfo->ID, aStack.position, true);
  1974. break;
  1975. case 3:
  1976. reverseCreature(attackingInfo->ID, aStack.position, true);
  1977. break;
  1978. case 4:
  1979. //reverseCreature(ID, aStack.position, true);
  1980. break;
  1981. case 5:
  1982. //reverseCreature(ID, aStack.position, true);
  1983. break;
  1984. }
  1985. }
  1986. }
  1987. attackingInfo->reversing = false;
  1988. creAnims[attackingInfo->ID]->setType(2);
  1989. delete attackingInfo;
  1990. attackingInfo = NULL;
  1991. }
  1992. if(attackingInfo)
  1993. {
  1994. attackingInfo->hitCount++;
  1995. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1996. attackingInfo->frame++;
  1997. }
  1998. }
  1999. }
  2000. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  2001. {
  2002. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack, true);
  2003. //preparating background graphic with hexes and shaded hexes
  2004. blitAt(background, 0, 0, backgroundWithHexes);
  2005. if(printCellBorders)
  2006. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  2007. if(printStackRange)
  2008. {
  2009. for(size_t m=0; m<shadedHexes.size(); ++m) //rows
  2010. {
  2011. int i = shadedHexes[m]/BFIELD_WIDTH; //row
  2012. int j = shadedHexes[m]%BFIELD_WIDTH-1; //column
  2013. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  2014. int y = 86 + 42 * i;
  2015. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  2016. }
  2017. }
  2018. }
  2019. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  2020. {
  2021. char tabh[200];
  2022. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  2023. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  2024. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  2025. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  2026. dmg);
  2027. if(killed > 0)
  2028. {
  2029. if(killed > 1)
  2030. {
  2031. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  2032. }
  2033. else //killed == 1
  2034. {
  2035. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  2036. }
  2037. }
  2038. console->addText(std::string(tabh));
  2039. }
  2040. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  2041. {
  2042. if(to == NULL)
  2043. to = screen;
  2044. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  2045. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  2046. {
  2047. if(it->animStartDelay>0)
  2048. {
  2049. --(it->animStartDelay);
  2050. continue;
  2051. }
  2052. SDL_Rect dst;
  2053. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  2054. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  2055. dst.x = it->x;
  2056. dst.y = it->y;
  2057. if(it->reverse)
  2058. {
  2059. SDL_Surface * rev = CSDL_Ext::rotate01(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap);
  2060. CSDL_Ext::blit8bppAlphaTo24bpp(rev, NULL, to, &dst);
  2061. SDL_FreeSurface(rev);
  2062. }
  2063. else
  2064. {
  2065. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  2066. }
  2067. //actualizing projectile
  2068. ++it->step;
  2069. if(it->step == it->lastStep)
  2070. {
  2071. toBeDeleted.insert(toBeDeleted.end(), it);
  2072. }
  2073. else
  2074. {
  2075. it->x += it->dx;
  2076. it->y += it->dy;
  2077. if(it->spin)
  2078. {
  2079. ++(it->frameNum);
  2080. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  2081. }
  2082. }
  2083. }
  2084. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  2085. {
  2086. projectiles.erase(*it);
  2087. }
  2088. }
  2089. void CBattleHero::show(SDL_Surface *to)
  2090. {
  2091. //animation of flag
  2092. if(flip)
  2093. {
  2094. CSDL_Ext::blit8bppAlphaTo24bpp(
  2095. flag->ourImages[flagAnim].bitmap,
  2096. NULL,
  2097. screen,
  2098. &genRect(
  2099. flag->ourImages[flagAnim].bitmap->h,
  2100. flag->ourImages[flagAnim].bitmap->w,
  2101. 62 + pos.x,
  2102. 39 + pos.y
  2103. )
  2104. );
  2105. }
  2106. else
  2107. {
  2108. CSDL_Ext::blit8bppAlphaTo24bpp(
  2109. flag->ourImages[flagAnim].bitmap,
  2110. NULL,
  2111. screen,
  2112. &genRect(
  2113. flag->ourImages[flagAnim].bitmap->h,
  2114. flag->ourImages[flagAnim].bitmap->w,
  2115. 71 + pos.x,
  2116. 39 + pos.y
  2117. )
  2118. );
  2119. }
  2120. ++flagAnimCount;
  2121. if(flagAnimCount%4==0)
  2122. {
  2123. ++flagAnim;
  2124. flagAnim %= flag->ourImages.size();
  2125. }
  2126. //animation of hero
  2127. int tick=-1;
  2128. for(int i=0; i<dh->ourImages.size(); ++i)
  2129. {
  2130. if(dh->ourImages[i].groupNumber==phase)
  2131. ++tick;
  2132. if(tick==image)
  2133. {
  2134. SDL_Rect posb = pos;
  2135. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  2136. if(phase != 4 || nextPhase != -1 || image < 4)
  2137. {
  2138. if(flagAnimCount%2==0)
  2139. {
  2140. ++image;
  2141. }
  2142. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  2143. {
  2144. image = 0;
  2145. }
  2146. }
  2147. if(phase == 4 && nextPhase != -1 && image == 7)
  2148. {
  2149. phase = nextPhase;
  2150. nextPhase = -1;
  2151. image = 0;
  2152. }
  2153. break;
  2154. }
  2155. }
  2156. }
  2157. void CBattleHero::activate()
  2158. {
  2159. ClickableL::activate();
  2160. }
  2161. void CBattleHero::deactivate()
  2162. {
  2163. ClickableL::deactivate();
  2164. }
  2165. void CBattleHero::setPhase(int newPhase)
  2166. {
  2167. if(phase != 4)
  2168. {
  2169. phase = newPhase;
  2170. image = 0;
  2171. }
  2172. else
  2173. {
  2174. nextPhase = newPhase;
  2175. }
  2176. }
  2177. void CBattleHero::clickLeft(boost::logic::tribool down)
  2178. {
  2179. 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
  2180. {
  2181. for(int it=0; it<BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  2182. {
  2183. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  2184. return;
  2185. }
  2186. CGI->curh->changeGraphic(0,0);
  2187. LOCPLINT->curint->deactivate();
  2188. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), myHero);
  2189. spellWindow->activate();
  2190. LOCPLINT->objsToBlit.push_back(spellWindow);
  2191. }
  2192. }
  2193. 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)
  2194. {
  2195. dh = CDefHandler::giveDef( defName );
  2196. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  2197. {
  2198. if(flip)
  2199. {
  2200. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  2201. SDL_FreeSurface(dh->ourImages[i].bitmap);
  2202. dh->ourImages[i].bitmap = hlp;
  2203. }
  2204. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  2205. }
  2206. dh->alphaTransformed = true;
  2207. if(flip)
  2208. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  2209. else
  2210. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  2211. //coloring flag and adding transparency
  2212. for(int i=0; i<flag->ourImages.size(); ++i)
  2213. {
  2214. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  2215. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  2216. }
  2217. }
  2218. CBattleHero::~CBattleHero()
  2219. {
  2220. delete dh;
  2221. delete flag;
  2222. }
  2223. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  2224. {
  2225. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  2226. ret.second = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
  2227. //counting x
  2228. if(attacker)
  2229. {
  2230. ret.first = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  2231. }
  2232. else
  2233. {
  2234. ret.first = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  2235. }
  2236. //shifting position for double - hex creatures
  2237. if(creature && creature->isDoubleWide())
  2238. {
  2239. if(attacker)
  2240. {
  2241. ret.first -= 42;
  2242. }
  2243. else
  2244. {
  2245. ret.first += 42;
  2246. }
  2247. }
  2248. //returning
  2249. return ret;
  2250. }
  2251. void CBattleHex::activate()
  2252. {
  2253. Hoverable::activate();
  2254. MotionInterested::activate();
  2255. ClickableL::activate();
  2256. ClickableR::activate();
  2257. }
  2258. void CBattleHex::deactivate()
  2259. {
  2260. Hoverable::deactivate();
  2261. MotionInterested::deactivate();
  2262. ClickableL::deactivate();
  2263. ClickableR::deactivate();
  2264. }
  2265. void CBattleHex::hover(bool on)
  2266. {
  2267. hovered = on;
  2268. Hoverable::hover(on);
  2269. if(!on && setAlterText)
  2270. {
  2271. myInterface->console->alterTxt = std::string();
  2272. setAlterText = false;
  2273. }
  2274. }
  2275. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  2276. {
  2277. }
  2278. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  2279. {
  2280. if(myInterface->cellShade)
  2281. {
  2282. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  2283. {
  2284. strictHovered = false;
  2285. }
  2286. else //hovered pixel is inside hex
  2287. {
  2288. strictHovered = true;
  2289. }
  2290. }
  2291. if(hovered && strictHovered) //print attacked creature to console
  2292. {
  2293. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  2294. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  2295. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  2296. {
  2297. char tabh[160];
  2298. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  2299. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  2300. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  2301. myInterface->console->alterTxt = std::string(tabh);
  2302. setAlterText = true;
  2303. }
  2304. }
  2305. else if(setAlterText)
  2306. {
  2307. myInterface->console->alterTxt = std::string();
  2308. setAlterText = false;
  2309. }
  2310. }
  2311. void CBattleHex::clickLeft(boost::logic::tribool down)
  2312. {
  2313. if(!down && hovered && strictHovered) //we've been really clicked!
  2314. {
  2315. myInterface->hexLclicked(myNumber);
  2316. }
  2317. }
  2318. void CBattleHex::clickRight(boost::logic::tribool down)
  2319. {
  2320. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  2321. if(hovered && strictHovered && stID!=-1)
  2322. {
  2323. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  2324. if(!myst.alive()) return;
  2325. StackState *pom = NULL;
  2326. if(down)
  2327. {
  2328. pom = new StackState();
  2329. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  2330. if(h)
  2331. {
  2332. pom->attackBonus = h->getPrimSkillLevel(0);
  2333. pom->defenseBonus = h->getPrimSkillLevel(1);
  2334. pom->luck = myst.Luck();
  2335. pom->morale = myst.Morale();
  2336. pom->speedBonus = myst.speed() - myst.creature->speed;
  2337. }
  2338. pom->shotsLeft = myst.shots;
  2339. for(int vb=0; vb<myst.effects.size(); ++vb)
  2340. {
  2341. pom->effects.insert(myst.effects[vb].id);
  2342. }
  2343. pom->currentHealth = myst.firstHPleft;
  2344. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  2345. ->activate();
  2346. }
  2347. delete pom;
  2348. }
  2349. }
  2350. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt(""), whoSetAlter(0)
  2351. {
  2352. }
  2353. CBattleConsole::~CBattleConsole()
  2354. {
  2355. texts.clear();
  2356. }
  2357. void CBattleConsole::show(SDL_Surface * to)
  2358. {
  2359. if(alterTxt.size())
  2360. {
  2361. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2362. }
  2363. else if(texts.size())
  2364. {
  2365. if(texts.size()==1)
  2366. {
  2367. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2368. }
  2369. else
  2370. {
  2371. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2372. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  2373. }
  2374. }
  2375. }
  2376. bool CBattleConsole::addText(const std::string & text)
  2377. {
  2378. if(text.size()>70)
  2379. return false; //text too long!
  2380. int firstInToken = 0;
  2381. for(int i=0; i<text.size(); ++i) //tokenize
  2382. {
  2383. if(text[i] == 10)
  2384. {
  2385. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  2386. firstInToken = i+1;
  2387. }
  2388. }
  2389. texts.push_back( text.substr(firstInToken, text.size()) );
  2390. lastShown = texts.size()-1;
  2391. return true;
  2392. }
  2393. void CBattleConsole::eraseText(unsigned int pos)
  2394. {
  2395. if(pos < texts.size())
  2396. {
  2397. texts.erase(texts.begin() + pos);
  2398. if(lastShown == texts.size())
  2399. --lastShown;
  2400. }
  2401. }
  2402. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  2403. {
  2404. if(pos >= texts.size()) //no such pos
  2405. return;
  2406. texts[pos] = text;
  2407. }
  2408. void CBattleConsole::scrollUp(unsigned int by)
  2409. {
  2410. if(lastShown > by)
  2411. lastShown -= by;
  2412. }
  2413. void CBattleConsole::scrollDown(unsigned int by)
  2414. {
  2415. if(lastShown + by < texts.size())
  2416. lastShown += by;
  2417. }
  2418. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  2419. {
  2420. this->pos = pos;
  2421. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  2422. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2423. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  2424. SDL_FreeSurface(background);
  2425. background = pom;
  2426. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 384 + pos.x, 505 + pos.y, "iok6432.def", SDLK_RETURN);
  2427. if(br.winner==0) //attacker won
  2428. {
  2429. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  2430. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  2431. }
  2432. else //if(br.winner==1)
  2433. {
  2434. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  2435. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  2436. }
  2437. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  2438. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  2439. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  2440. std::string attackerName, defenderName;
  2441. if(owner->attackingHeroInstance) //a hero attacked
  2442. {
  2443. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  2444. //setting attackerName
  2445. attackerName = owner->attackingHeroInstance->name;
  2446. }
  2447. else //a monster attacked
  2448. {
  2449. int bestMonsterID = -1;
  2450. int bestPower = 0;
  2451. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  2452. {
  2453. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  2454. {
  2455. bestPower = CGI->creh->creatures[it->first].AIValue;
  2456. bestMonsterID = it->first;
  2457. }
  2458. }
  2459. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  2460. //setting attackerName
  2461. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  2462. }
  2463. if(owner->defendingHeroInstance) //a hero defended
  2464. {
  2465. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  2466. //setting defenderName
  2467. defenderName = owner->defendingHeroInstance->name;
  2468. }
  2469. else //a monster defended
  2470. {
  2471. int bestMonsterID = -1;
  2472. int bestPower = 0;
  2473. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  2474. {
  2475. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  2476. {
  2477. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  2478. bestMonsterID = it->second.first;
  2479. }
  2480. }
  2481. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  2482. //setting defenderName
  2483. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  2484. }
  2485. //printing attacker and defender's names
  2486. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  2487. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  2488. //printing casualities
  2489. for(int step = 0; step < 2; ++step)
  2490. {
  2491. if(br.casualties[step].size()==0)
  2492. {
  2493. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  2494. }
  2495. else
  2496. {
  2497. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  2498. int yPos = 344 + step*97;
  2499. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  2500. {
  2501. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  2502. std::stringstream amount;
  2503. amount<<it->second;
  2504. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  2505. xPos += 42;
  2506. }
  2507. }
  2508. }
  2509. //printing result description
  2510. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  2511. switch(br.result)
  2512. {
  2513. case 0: //normal victory
  2514. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2515. {
  2516. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  2517. }
  2518. else
  2519. {
  2520. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  2521. }
  2522. break;
  2523. case 1: //flee
  2524. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2525. {
  2526. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  2527. }
  2528. else
  2529. {
  2530. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  2531. }
  2532. break;
  2533. case 2: //surrender
  2534. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2535. {
  2536. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  2537. }
  2538. else
  2539. {
  2540. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  2541. }
  2542. break;
  2543. }
  2544. }
  2545. CBattleReslutWindow::~CBattleReslutWindow()
  2546. {
  2547. SDL_FreeSurface(background);
  2548. }
  2549. void CBattleReslutWindow::activate()
  2550. {
  2551. LOCPLINT->showingDialog->set(true);
  2552. exit->activate();
  2553. }
  2554. void CBattleReslutWindow::deactivate()
  2555. {
  2556. exit->deactivate();
  2557. }
  2558. void CBattleReslutWindow::show(SDL_Surface *to)
  2559. {
  2560. //evaluating to
  2561. if(!to)
  2562. to = screen;
  2563. SDL_BlitSurface(background, NULL, to, &pos);
  2564. exit->show(to);
  2565. }
  2566. void CBattleReslutWindow::bExitf()
  2567. {
  2568. LOCPLINT->battleResultQuited();
  2569. }
  2570. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  2571. {
  2572. pos = position;
  2573. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  2574. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2575. 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);
  2576. viewGrid->select(owner->printCellBorders);
  2577. 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);
  2578. movementShadow->select(owner->printStackRange);
  2579. 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);
  2580. mouseShadow->select(owner->printMouseShadow);
  2581. animSpeeds = new CHighlightableButtonsGroup(0);
  2582. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def",188, 309, 1);
  2583. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def",252, 309, 2);
  2584. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",315, 309, 4);
  2585. animSpeeds->select(owner->getAnimSpeed(), 1);
  2586. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  2587. setToDefault = new AdventureMapButton (CGI->generaltexth->zelp[392].first, CGI->generaltexth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def");
  2588. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  2589. exit = new AdventureMapButton (CGI->generaltexth->zelp[393].first, CGI->generaltexth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def",SDLK_RETURN);
  2590. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  2591. //printing texts to background
  2592. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  2593. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  2594. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  2595. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  2596. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  2597. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  2598. //auto - combat options
  2599. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  2600. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  2601. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  2602. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  2603. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  2604. //creature info
  2605. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  2606. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  2607. //general options
  2608. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  2609. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  2610. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  2611. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  2612. //texts printed
  2613. }
  2614. CBattleOptionsWindow::~CBattleOptionsWindow()
  2615. {
  2616. SDL_FreeSurface(background);
  2617. delete setToDefault;
  2618. delete exit;
  2619. delete viewGrid;
  2620. delete movementShadow;
  2621. delete animSpeeds;
  2622. delete mouseShadow;
  2623. }
  2624. void CBattleOptionsWindow::activate()
  2625. {
  2626. setToDefault->activate();
  2627. exit->activate();
  2628. viewGrid->activate();
  2629. movementShadow->activate();
  2630. animSpeeds->activate();
  2631. mouseShadow->activate();
  2632. }
  2633. void CBattleOptionsWindow::deactivate()
  2634. {
  2635. setToDefault->deactivate();
  2636. exit->deactivate();
  2637. viewGrid->deactivate();
  2638. movementShadow->deactivate();
  2639. animSpeeds->deactivate();
  2640. mouseShadow->deactivate();
  2641. }
  2642. void CBattleOptionsWindow::show(SDL_Surface *to)
  2643. {
  2644. if(!to) //"evaluating" to
  2645. to = screen;
  2646. SDL_BlitSurface(background, NULL, to, &pos);
  2647. setToDefault->show(to);
  2648. exit->show(to);
  2649. viewGrid->show(to);
  2650. movementShadow->show(to);
  2651. animSpeeds->show(to);
  2652. mouseShadow->show(to);
  2653. }
  2654. void CBattleOptionsWindow::bDefaultf()
  2655. {
  2656. }
  2657. void CBattleOptionsWindow::bExitf()
  2658. {
  2659. deactivate();
  2660. for(size_t g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  2661. {
  2662. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  2663. {
  2664. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  2665. break;
  2666. }
  2667. }
  2668. delete this;
  2669. LOCPLINT->curint->activate();
  2670. }