CBattleInterface.cpp 93 KB

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