CBattleInterface.cpp 92 KB

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