CBattleInterface.cpp 95 KB

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