CBattleInterface.cpp 107 KB

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