CBattleInterface.cpp 90 KB

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