CBattleInterface.cpp 73 KB

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