2
0

CBattleInterface.cpp 92 KB

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