CBattleInterface.cpp 104 KB

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