CBattleInterface.cpp 95 KB

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