CBattleInterface.cpp 92 KB

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