CBattleInterface.cpp 94 KB

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