CBattleInterface.cpp 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349
  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. //prepairing 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. }
  657. void CBattleInterface::bDefencef()
  658. {
  659. giveCommand(3,0,activeStack);
  660. }
  661. void CBattleInterface::bConsoleUpf()
  662. {
  663. console->scrollUp();
  664. }
  665. void CBattleInterface::bConsoleDownf()
  666. {
  667. console->scrollDown();
  668. }
  669. void CBattleInterface::newStack(CStack stack)
  670. {
  671. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  672. creAnims[stack.ID]->setType(2);
  673. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  674. }
  675. void CBattleInterface::stackRemoved(CStack stack)
  676. {
  677. delete creAnims[stack.ID];
  678. creAnims.erase(stack.ID);
  679. }
  680. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  681. {
  682. if(creAnims[ID]->getType() != 2)
  683. {
  684. return; //something went wrong
  685. }
  686. if(byShooting) //delay hit animation
  687. {
  688. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  689. while(true)
  690. {
  691. bool found = false;
  692. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  693. {
  694. if(it->creID == attacker.creature->idNumber)
  695. {
  696. found = true;
  697. break;
  698. }
  699. }
  700. if(!found)
  701. break;
  702. else
  703. {
  704. show();
  705. CSDL_Ext::update();
  706. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  707. }
  708. }
  709. }
  710. creAnims[ID]->setType(5); //death
  711. //int firstFrame = creAnims[ID]->getFrame();
  712. int increments = 0;
  713. while(increments < creAnims[ID]->framesInGroup(5)-1)
  714. {
  715. if((animCount%(4/animSpeed))==0)
  716. {
  717. creAnims[ID]->incrementFrame();
  718. ++increments;
  719. }
  720. show();
  721. CSDL_Ext::update();
  722. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  723. }
  724. if(IDby!=-1)
  725. printConsoleAttacked(ID, dmg, killed, IDby);
  726. }
  727. void CBattleInterface::stackActivated(int number)
  728. {
  729. //givenCommand = NULL;
  730. activeStack = number;
  731. myTurn = true;
  732. redrawBackgroundWithHexes(number);
  733. }
  734. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving)
  735. {
  736. bool startMoving = creAnims[number]->type==20;
  737. //a few useful variables
  738. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  739. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  740. int hexWbase = 44, hexHbase = 42;
  741. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  742. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  743. {
  744. CGI->curh->hide();
  745. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  746. {
  747. show();
  748. CSDL_Ext::update();
  749. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  750. if((animCount+1)%(4/animSpeed)==0)
  751. creAnims[number]->incrementFrame();
  752. }
  753. }
  754. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  755. {
  756. switch(mutPos) //reverse unit if necessary
  757. {
  758. case 0: case 4: case 5:
  759. if(creDir[number] == true)
  760. reverseCreature(number, curStackPos, twoTiles);
  761. break;
  762. case 1: case 2: case 3:
  763. if(creDir[number] == false)
  764. reverseCreature(number, curStackPos, twoTiles);
  765. break;
  766. }
  767. //moving instructions
  768. creAnims[number]->setType(0);
  769. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  770. for(int i=0; i<steps; ++i)
  771. {
  772. switch(mutPos)
  773. {
  774. case 0:
  775. posX -= ((float)hexWbase)/(2.0f*steps);
  776. creAnims[number]->pos.x = posX;
  777. posY -= ((float)hexHbase)/((float)steps);
  778. creAnims[number]->pos.y = posY;
  779. break;
  780. case 1:
  781. posX += ((float)hexWbase)/(2.0f*steps);
  782. creAnims[number]->pos.x = posX;
  783. posY -= ((float)hexHbase)/((float)steps);
  784. creAnims[number]->pos.y = posY;
  785. break;
  786. case 2:
  787. posX += ((float)hexWbase)/((float)steps);
  788. creAnims[number]->pos.x = posX;
  789. break;
  790. case 3:
  791. posX += ((float)hexWbase)/(2.0f*steps);
  792. creAnims[number]->pos.x = posX;
  793. posY += ((float)hexHbase)/((float)steps);
  794. creAnims[number]->pos.y = posY;
  795. break;
  796. case 4:
  797. posX -= ((float)hexWbase)/(2.0f*steps);
  798. creAnims[number]->pos.x = posX;
  799. posY += ((float)hexHbase)/((float)steps);
  800. creAnims[number]->pos.y = posY;
  801. break;
  802. case 5:
  803. posX -= ((float)hexWbase)/((float)steps);
  804. creAnims[number]->pos.x = posX;
  805. break;
  806. }
  807. show();
  808. CSDL_Ext::update();
  809. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  810. if((animCount+1)%(4/animSpeed)==0)
  811. creAnims[number]->incrementFrame();
  812. }
  813. }
  814. if(endMoving) //animation of ending move
  815. {
  816. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  817. {
  818. creAnims[number]->setType(21);
  819. for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  820. {
  821. show();
  822. CSDL_Ext::update();
  823. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  824. if((animCount+1)%(4/animSpeed)==0)
  825. creAnims[number]->incrementFrame();
  826. }
  827. }
  828. creAnims[number]->setType(2); //resetting to default
  829. CGI->curh->show();
  830. }
  831. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  832. if(endMoving) //resetting to default
  833. {
  834. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  835. reverseCreature(number, destHex, twoTiles);
  836. }
  837. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  838. creAnims[number]->pos.x = coords.first;
  839. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  840. creAnims[number]->pos.x -= 44;
  841. creAnims[number]->pos.y = coords.second;
  842. }
  843. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  844. {
  845. while(creAnims[ID]->getType() != 2 || (attackingInfo && attackingInfo->IDby == IDby))
  846. {
  847. show();
  848. CSDL_Ext::update();
  849. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  850. }
  851. if(byShooting) //delay hit animation
  852. {
  853. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  854. while(true)
  855. {
  856. bool found = false;
  857. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  858. {
  859. if(it->creID == attacker.creature->idNumber)
  860. {
  861. found = true;
  862. break;
  863. }
  864. }
  865. if(!found)
  866. break;
  867. else
  868. {
  869. show();
  870. CSDL_Ext::update();
  871. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  872. }
  873. }
  874. }
  875. creAnims[ID]->setType(3); //getting hit
  876. int firstFrame = creAnims[ID]->getFrame();
  877. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(3) + firstFrame - 1; ++i)
  878. {
  879. show();
  880. CSDL_Ext::update();
  881. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  882. /*if((animCount+1)%(4/animSpeed)==0)
  883. creAnims[ID]->incrementFrame();*/
  884. }
  885. creAnims[ID]->setType(2);
  886. if(IDby!=-1)
  887. printConsoleAttacked(ID, dmg, killed, IDby);
  888. }
  889. void CBattleInterface::stackAttacking(int ID, int dest)
  890. {
  891. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  892. {
  893. show();
  894. CSDL_Ext::update();
  895. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  896. }
  897. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  898. int reversedShift = 0; //shift of attacking stack's position due to reversing
  899. if(aStack.attackerOwned)
  900. {
  901. if(aStack.creature->isDoubleWide())
  902. {
  903. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  904. {
  905. case 0:
  906. //reverseCreature(ID, aStack.position, true);
  907. break;
  908. case 1:
  909. break;
  910. case 2:
  911. break;
  912. case 3:
  913. break;
  914. case 4:
  915. //reverseCreature(ID, aStack.position, true);
  916. break;
  917. case 5:
  918. reverseCreature(ID, aStack.position, true);
  919. break;
  920. case -1:
  921. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  922. {
  923. reverseCreature(ID, aStack.position, true);
  924. reversedShift = (aStack.attackerOwned ? -1 : 1);
  925. }
  926. break;
  927. }
  928. }
  929. else //else for if(aStack.creature->isDoubleWide())
  930. {
  931. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  932. {
  933. case 0:
  934. reverseCreature(ID, aStack.position, true);
  935. break;
  936. case 1:
  937. break;
  938. case 2:
  939. break;
  940. case 3:
  941. break;
  942. case 4:
  943. reverseCreature(ID, aStack.position, true);
  944. break;
  945. case 5:
  946. reverseCreature(ID, aStack.position, true);
  947. break;
  948. }
  949. }
  950. }
  951. else //if(aStack.attackerOwned)
  952. {
  953. if(aStack.creature->isDoubleWide())
  954. {
  955. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  956. {
  957. case 0:
  958. //reverseCreature(ID, aStack.position, true);
  959. break;
  960. case 1:
  961. break;
  962. case 2:
  963. reverseCreature(ID, aStack.position, true);
  964. break;
  965. case 3:
  966. break;
  967. case 4:
  968. //reverseCreature(ID, aStack.position, true);
  969. break;
  970. case 5:
  971. //reverseCreature(ID, aStack.position, true);
  972. break;
  973. case -1:
  974. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  975. {
  976. reverseCreature(ID, aStack.position, true);
  977. reversedShift = (aStack.attackerOwned ? -1 : 1);
  978. }
  979. break;
  980. }
  981. }
  982. else //else for if(aStack.creature->isDoubleWide())
  983. {
  984. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  985. {
  986. case 0:
  987. //reverseCreature(ID, aStack.position, true);
  988. break;
  989. case 1:
  990. reverseCreature(ID, aStack.position, true);
  991. break;
  992. case 2:
  993. reverseCreature(ID, aStack.position, true);
  994. break;
  995. case 3:
  996. reverseCreature(ID, aStack.position, true);
  997. break;
  998. case 4:
  999. //reverseCreature(ID, aStack.position, true);
  1000. break;
  1001. case 5:
  1002. //reverseCreature(ID, aStack.position, true);
  1003. break;
  1004. }
  1005. }
  1006. }
  1007. attackingInfo = new CAttHelper;
  1008. attackingInfo->dest = dest;
  1009. attackingInfo->frame = 0;
  1010. attackingInfo->hitCount = 0;
  1011. attackingInfo->ID = ID;
  1012. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  1013. attackingInfo->reversing = false;
  1014. attackingInfo->posShiftDueToDist = reversedShift;
  1015. attackingInfo->shooting = false;
  1016. switch(BattleInfo::mutualPosition(aStack.position + reversedShift, dest)) //attack direction
  1017. {
  1018. case 0:
  1019. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1020. break;
  1021. case 1:
  1022. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1023. break;
  1024. case 2:
  1025. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1026. break;
  1027. case 3:
  1028. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1029. break;
  1030. case 4:
  1031. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1032. break;
  1033. case 5:
  1034. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1035. break;
  1036. default:
  1037. tlog1<<"Critical Error! Wrong dest in stackAttacking!"<<std::endl;
  1038. }
  1039. }
  1040. void CBattleInterface::newRound(int number)
  1041. {
  1042. console->addText(CGI->generaltexth->allTexts[412]);
  1043. }
  1044. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  1045. {
  1046. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  1047. ba->actionType = action;
  1048. ba->destinationTile = tile;
  1049. ba->stackNumber = stack;
  1050. ba->additionalInfo = additional;
  1051. givenCommand->setn(ba);
  1052. myTurn = false;
  1053. activeStack = -1;
  1054. }
  1055. bool CBattleInterface::isTileAttackable(const int & number) const
  1056. {
  1057. for(int b=0; b<shadedHexes.size(); ++b)
  1058. {
  1059. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  1060. return true;
  1061. }
  1062. return false;
  1063. }
  1064. void CBattleInterface::hexLclicked(int whichOne)
  1065. {
  1066. if((whichOne%BFIELD_WIDTH)!=0 && (whichOne%BFIELD_WIDTH)!=(BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  1067. {
  1068. if(!myTurn)
  1069. return; //we are not permit to do anything
  1070. if(spellDestSelectMode)
  1071. {
  1072. //checking destination
  1073. bool allowCasting = true;
  1074. switch(spellSelMode)
  1075. {
  1076. case 1:
  1077. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1078. allowCasting = false;
  1079. break;
  1080. case 2:
  1081. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1082. allowCasting = false;
  1083. break;
  1084. case 3:
  1085. if(!LOCPLINT->cb->battleGetStackByPos(whichOne))
  1086. allowCasting = false;
  1087. break;
  1088. case 4: //TODO: implement this case
  1089. break;
  1090. }
  1091. //destination checked
  1092. if(allowCasting)
  1093. {
  1094. spellToCast->destinationTile = whichOne;
  1095. LOCPLINT->cb->battleMakeAction(spellToCast);
  1096. delete spellToCast;
  1097. spellToCast = NULL;
  1098. spellDestSelectMode = false;
  1099. CGI->curh->changeGraphic(1, 6);
  1100. }
  1101. }
  1102. else
  1103. {
  1104. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  1105. if(!dest || !dest->alive()) //no creature at that tile
  1106. {
  1107. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  1108. {
  1109. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1110. giveCommand(2,whichOne,activeStack);
  1111. }
  1112. }
  1113. else if(dest->owner != attackingHeroInstance->tempOwner
  1114. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne) ) //shooting
  1115. {
  1116. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1117. giveCommand(7,whichOne,activeStack);
  1118. }
  1119. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  1120. {
  1121. switch(CGI->curh->number)
  1122. {
  1123. case 12:
  1124. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1125. break;
  1126. case 7:
  1127. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1128. break;
  1129. case 8:
  1130. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1131. break;
  1132. case 9:
  1133. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1134. break;
  1135. case 10:
  1136. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1137. break;
  1138. case 11:
  1139. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1140. break;
  1141. }
  1142. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1143. }
  1144. }
  1145. }
  1146. }
  1147. void CBattleInterface::stackIsShooting(int ID, int dest)
  1148. {
  1149. if(attackingInfo != NULL)
  1150. {
  1151. return; //something went wrong
  1152. }
  1153. //projectile
  1154. float projectileAngle; //in radians; if positive, projectiles goes up
  1155. 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)
  1156. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1157. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1158. if(fromHex < dest)
  1159. projectileAngle = -projectileAngle;
  1160. SProjectileInfo spi;
  1161. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1162. spi.step = 0;
  1163. spi.frameNum = 0;
  1164. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1165. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1166. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1167. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1168. if(projectileAngle > straightAngle) //upper shot
  1169. {
  1170. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1171. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1172. }
  1173. else if(projectileAngle < -straightAngle) //lower shot
  1174. {
  1175. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1176. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1177. }
  1178. else //straight shot
  1179. {
  1180. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1181. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1182. }
  1183. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1184. if(spi.lastStep == 0)
  1185. spi.lastStep = 1;
  1186. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1187. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1188. //set starting frame
  1189. if(spi.spin)
  1190. {
  1191. spi.frameNum = 0;
  1192. }
  1193. else
  1194. {
  1195. 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);
  1196. }
  1197. //set delay
  1198. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1199. projectiles.push_back(spi);
  1200. //attack aniamtion
  1201. attackingInfo = new CAttHelper;
  1202. attackingInfo->dest = dest;
  1203. attackingInfo->frame = 0;
  1204. attackingInfo->hitCount = 0;
  1205. attackingInfo->ID = ID;
  1206. attackingInfo->reversing = false;
  1207. attackingInfo->posShiftDueToDist = 0;
  1208. attackingInfo->shooting = true;
  1209. if(projectileAngle > straightAngle) //upper shot
  1210. attackingInfo->shootingGroup = 14;
  1211. else if(projectileAngle < -straightAngle) //lower shot
  1212. attackingInfo->shootingGroup = 16;
  1213. else //straight shot
  1214. attackingInfo->shootingGroup = 15;
  1215. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1216. }
  1217. void CBattleInterface::battleFinished(const BattleResult& br)
  1218. {
  1219. deactivate();
  1220. CGI->curh->changeGraphic(0,0);
  1221. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1222. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1223. resWindow->activate();
  1224. }
  1225. void CBattleInterface::spellCasted(SpellCasted * sc)
  1226. {
  1227. std::vector< std::string > anims; //for magic arrow and ice bolt
  1228. switch(sc->id)
  1229. {
  1230. case 15: //magic arrow
  1231. {
  1232. //initialization of anims
  1233. 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");
  1234. }
  1235. case 16: //ice bolt
  1236. {
  1237. if(anims.size() == 0) //initialiaztion of anims
  1238. {
  1239. 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");
  1240. }
  1241. } //end of ice bolt only part
  1242. { //common ice bolt and magic arrow part
  1243. //initial variables
  1244. std::string animToDisplay;
  1245. std::pair<int, int> srccoord = sc->side ? std::make_pair(770, 60) : std::make_pair(30, 60);
  1246. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile)->creature); //position attacked by arrow
  1247. destcoord.first += 250; destcoord.second += 240;
  1248. //animation angle
  1249. float angle = atan2(float(destcoord.first - srccoord.first), float(destcoord.second - srccoord.second));
  1250. //choosing animation by angle
  1251. if(angle > 1.50)
  1252. animToDisplay = anims[0];
  1253. else if(angle > 1.20)
  1254. animToDisplay = anims[1];
  1255. else if(angle > 0.90)
  1256. animToDisplay = anims[2];
  1257. else if(angle > 0.60)
  1258. animToDisplay = anims[3];
  1259. else
  1260. animToDisplay = anims[4];
  1261. //displaying animation
  1262. int steps = sqrt((float)((destcoord.first - srccoord.first)*(destcoord.first - srccoord.first) + (destcoord.second - srccoord.second) * (destcoord.second - srccoord.second))) / 40;
  1263. if(steps <= 0)
  1264. steps = 1;
  1265. CDefHandler * animDef = CDefHandler::giveDef(animToDisplay);
  1266. int dx = (destcoord.first - srccoord.first - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.second - srccoord.second - animDef->ourImages[0].bitmap->h)/steps;
  1267. for(int g=0; g<steps; ++g)
  1268. {
  1269. show();
  1270. SDL_Rect & srcr = animDef->ourImages[g%animDef->ourImages.size()].bitmap->clip_rect;
  1271. SDL_Rect dstr = genRect(srcr.h, srcr.w, srccoord.first + g*dx, srccoord.second + g*dy);
  1272. SDL_BlitSurface(animDef->ourImages[g%animDef->ourImages.size()].bitmap, &srcr, screen, &dstr);
  1273. CSDL_Ext::update();
  1274. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1275. }
  1276. int b=0;
  1277. break; //for 15 and 16 cases
  1278. }
  1279. case 17: //lightning bolt
  1280. {
  1281. displayEffect(1, sc->tile);
  1282. break;
  1283. }
  1284. case 18: //implosion
  1285. {
  1286. displayEffect(10, sc->tile);
  1287. break;
  1288. }
  1289. case 53: //haste
  1290. {
  1291. displayEffect(31, sc->tile);
  1292. break;
  1293. }
  1294. case 54: //slow
  1295. {
  1296. displayEffect(19, sc->tile);
  1297. break;
  1298. }
  1299. }
  1300. }
  1301. void CBattleInterface::castThisSpell(int spellID)
  1302. {
  1303. BattleAction * ba = new BattleAction;
  1304. ba->actionType = 1;
  1305. ba->additionalInfo = spellID; //spell number
  1306. ba->destinationTile = -1;
  1307. ba->stackNumber = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? -1 : -2;
  1308. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1309. spellToCast = ba;
  1310. spellDestSelectMode = true;
  1311. //choosing possible tragets
  1312. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? attackingHeroInstance : attackingHeroInstance;
  1313. spellSelMode = 0;
  1314. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET") != std::string::npos)
  1315. {
  1316. spellSelMode = 3;
  1317. }
  1318. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET_2") != std::string::npos)
  1319. {
  1320. if(castingHero && castingHero->getSpellSecLevel(spellID) < 3)
  1321. spellSelMode = 3;
  1322. else //TODO: no destination chould apply in this case
  1323. {
  1324. }
  1325. }
  1326. CGI->curh->changeGraphic(3, 0);
  1327. }
  1328. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  1329. {
  1330. if(graphics->battleACToDef[effect].size() != 0)
  1331. {
  1332. SBattleEffect be;
  1333. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1334. be.frame = 0;
  1335. be.maxFrame = be.anim->ourImages.size();
  1336. be.x = 22 * ( ((destTile/BFIELD_WIDTH) + 1)%2 ) + 44 * (destTile % BFIELD_WIDTH) + 45;
  1337. be.y = 105 + 42 * (destTile/BFIELD_WIDTH);
  1338. if(effect != 1)
  1339. {
  1340. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1341. be.y -= be.anim->ourImages[0].bitmap->h/2;
  1342. }
  1343. else if(effect == 1)
  1344. {
  1345. be.x -= be.anim->ourImages[0].bitmap->w;
  1346. be.y -= be.anim->ourImages[0].bitmap->h;
  1347. }
  1348. battleEffects.push_back(be);
  1349. }
  1350. //battleEffects
  1351. }
  1352. void CBattleInterface::setAnimSpeed(int set)
  1353. {
  1354. animSpeed = set;
  1355. }
  1356. int CBattleInterface::getAnimSpeed() const
  1357. {
  1358. return animSpeed;
  1359. }
  1360. float CBattleInterface::getAnimSpeedMultiplier() const
  1361. {
  1362. switch(animSpeed)
  1363. {
  1364. case 1:
  1365. return 3.5f;
  1366. case 2:
  1367. return 2.2f;
  1368. case 4:
  1369. return 1.0f;
  1370. default:
  1371. return 0.0f;
  1372. }
  1373. }
  1374. void CBattleInterface::attackingShowHelper()
  1375. {
  1376. if(attackingInfo && !attackingInfo->reversing)
  1377. {
  1378. if(attackingInfo->frame == 0)
  1379. {
  1380. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1381. if(attackingInfo->shooting)
  1382. {
  1383. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1384. }
  1385. else
  1386. {
  1387. if(aStack.creature->isDoubleWide())
  1388. {
  1389. switch(BattleInfo::mutualPosition(aStack.position+attackingInfo->posShiftDueToDist, attackingInfo->dest)) //attack direction
  1390. {
  1391. case 0:
  1392. creAnims[attackingInfo->ID]->setType(11);
  1393. break;
  1394. case 1:
  1395. creAnims[attackingInfo->ID]->setType(11);
  1396. break;
  1397. case 2:
  1398. creAnims[attackingInfo->ID]->setType(12);
  1399. break;
  1400. case 3:
  1401. creAnims[attackingInfo->ID]->setType(13);
  1402. break;
  1403. case 4:
  1404. creAnims[attackingInfo->ID]->setType(13);
  1405. break;
  1406. case 5:
  1407. creAnims[attackingInfo->ID]->setType(12);
  1408. break;
  1409. }
  1410. }
  1411. else //else for if(aStack.creature->isDoubleWide())
  1412. {
  1413. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1414. {
  1415. case 0:
  1416. creAnims[attackingInfo->ID]->setType(11);
  1417. break;
  1418. case 1:
  1419. creAnims[attackingInfo->ID]->setType(11);
  1420. break;
  1421. case 2:
  1422. creAnims[attackingInfo->ID]->setType(12);
  1423. break;
  1424. case 3:
  1425. creAnims[attackingInfo->ID]->setType(13);
  1426. break;
  1427. case 4:
  1428. creAnims[attackingInfo->ID]->setType(13);
  1429. break;
  1430. case 5:
  1431. creAnims[attackingInfo->ID]->setType(12);
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. }
  1437. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1438. {
  1439. attackingInfo->reversing = true;
  1440. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1441. if(aStackp == NULL)
  1442. return;
  1443. CStack aStack = *aStackp;
  1444. if(aStack.attackerOwned)
  1445. {
  1446. if(aStack.creature->isDoubleWide())
  1447. {
  1448. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1449. {
  1450. case 0:
  1451. //reverseCreature(ID, aStack.position, true);
  1452. break;
  1453. case 1:
  1454. break;
  1455. case 2:
  1456. break;
  1457. case 3:
  1458. break;
  1459. case 4:
  1460. //reverseCreature(ID, aStack.position, true);
  1461. break;
  1462. case 5:
  1463. reverseCreature(attackingInfo->ID, aStack.position, true);
  1464. break;
  1465. case -1:
  1466. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1467. {
  1468. reverseCreature(attackingInfo->ID, aStack.position, true);
  1469. }
  1470. break;
  1471. }
  1472. }
  1473. else //else for if(aStack.creature->isDoubleWide())
  1474. {
  1475. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1476. {
  1477. case 0:
  1478. reverseCreature(attackingInfo->ID, aStack.position, true);
  1479. break;
  1480. case 1:
  1481. break;
  1482. case 2:
  1483. break;
  1484. case 3:
  1485. break;
  1486. case 4:
  1487. reverseCreature(attackingInfo->ID, aStack.position, true);
  1488. break;
  1489. case 5:
  1490. reverseCreature(attackingInfo->ID, aStack.position, true);
  1491. break;
  1492. }
  1493. }
  1494. }
  1495. else //if(aStack.attackerOwned)
  1496. {
  1497. if(aStack.creature->isDoubleWide())
  1498. {
  1499. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1500. {
  1501. case 0:
  1502. //reverseCreature(ID, aStack.position, true);
  1503. break;
  1504. case 1:
  1505. break;
  1506. case 2:
  1507. reverseCreature(attackingInfo->ID, aStack.position, true);
  1508. break;
  1509. case 3:
  1510. break;
  1511. case 4:
  1512. //reverseCreature(ID, aStack.position, true);
  1513. break;
  1514. case 5:
  1515. //reverseCreature(ID, aStack.position, true);
  1516. break;
  1517. case -1:
  1518. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1519. {
  1520. reverseCreature(attackingInfo->ID, aStack.position, true);
  1521. }
  1522. break;
  1523. }
  1524. }
  1525. else //else for if(aStack.creature->isDoubleWide())
  1526. {
  1527. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1528. {
  1529. case 0:
  1530. //reverseCreature(ID, aStack.position, true);
  1531. break;
  1532. case 1:
  1533. reverseCreature(attackingInfo->ID, aStack.position, true);
  1534. break;
  1535. case 2:
  1536. reverseCreature(attackingInfo->ID, aStack.position, true);
  1537. break;
  1538. case 3:
  1539. reverseCreature(attackingInfo->ID, aStack.position, true);
  1540. break;
  1541. case 4:
  1542. //reverseCreature(ID, aStack.position, true);
  1543. break;
  1544. case 5:
  1545. //reverseCreature(ID, aStack.position, true);
  1546. break;
  1547. }
  1548. }
  1549. }
  1550. attackingInfo->reversing = false;
  1551. creAnims[attackingInfo->ID]->setType(2);
  1552. delete attackingInfo;
  1553. attackingInfo = NULL;
  1554. }
  1555. if(attackingInfo)
  1556. {
  1557. attackingInfo->hitCount++;
  1558. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1559. attackingInfo->frame++;
  1560. }
  1561. }
  1562. }
  1563. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1564. {
  1565. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1566. //preparating background graphic with hexes and shaded hexes
  1567. blitAt(background, 0, 0, backgroundWithHexes);
  1568. if(printCellBorders)
  1569. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1570. if(printStackRange)
  1571. {
  1572. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1573. {
  1574. int i = shadedHexes[m]/17; //row
  1575. int j = shadedHexes[m]%17-1; //column
  1576. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1577. int y = 86 + 42 * i;
  1578. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1579. }
  1580. }
  1581. }
  1582. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1583. {
  1584. char tabh[200];
  1585. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1586. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1587. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1588. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1589. dmg);
  1590. if(killed > 0)
  1591. {
  1592. if(killed > 1)
  1593. {
  1594. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1595. }
  1596. else //killed == 1
  1597. {
  1598. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1599. }
  1600. }
  1601. console->addText(std::string(tabh));
  1602. }
  1603. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1604. {
  1605. if(to == NULL)
  1606. to = screen;
  1607. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1608. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1609. {
  1610. if(it->animStartDelay>0)
  1611. {
  1612. --(it->animStartDelay);
  1613. continue;
  1614. }
  1615. SDL_Rect dst;
  1616. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1617. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1618. dst.x = it->x;
  1619. dst.y = it->y;
  1620. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1621. //actualizing projectile
  1622. ++it->step;
  1623. if(it->step == it->lastStep)
  1624. {
  1625. toBeDeleted.insert(toBeDeleted.end(), it);
  1626. }
  1627. else
  1628. {
  1629. it->x += it->dx;
  1630. it->y += it->dy;
  1631. if(it->spin)
  1632. {
  1633. ++(it->frameNum);
  1634. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1635. }
  1636. }
  1637. }
  1638. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1639. {
  1640. projectiles.erase(*it);
  1641. }
  1642. }
  1643. void CBattleHero::show(SDL_Surface *to)
  1644. {
  1645. //animation of flag
  1646. if(flip)
  1647. {
  1648. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1649. }
  1650. else
  1651. {
  1652. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1653. }
  1654. ++flagAnimCount;
  1655. if(flagAnimCount%4==0)
  1656. {
  1657. ++flagAnim;
  1658. flagAnim %= flag->ourImages.size();
  1659. }
  1660. //animation of hero
  1661. int tick=-1;
  1662. for(int i=0; i<dh->ourImages.size(); ++i)
  1663. {
  1664. if(dh->ourImages[i].groupNumber==phase)
  1665. ++tick;
  1666. if(tick==image)
  1667. {
  1668. SDL_Rect posb = pos;
  1669. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1670. ++image;
  1671. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  1672. {
  1673. image = 0;
  1674. }
  1675. break;
  1676. }
  1677. }
  1678. }
  1679. void CBattleHero::activate()
  1680. {
  1681. ClickableL::activate();
  1682. }
  1683. void CBattleHero::deactivate()
  1684. {
  1685. ClickableL::deactivate();
  1686. }
  1687. void CBattleHero::setPhase(int newPhase)
  1688. {
  1689. phase = newPhase;
  1690. image = 0;
  1691. }
  1692. void CBattleHero::clickLeft(boost::logic::tribool down)
  1693. {
  1694. if(!down && myHero)
  1695. {
  1696. for(int it=0; it<BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  1697. {
  1698. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  1699. return;
  1700. }
  1701. CGI->curh->changeGraphic(0,0);
  1702. LOCPLINT->curint->deactivate();
  1703. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1704. spellWindow->activate();
  1705. LOCPLINT->objsToBlit.push_back(spellWindow);
  1706. }
  1707. }
  1708. 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)
  1709. {
  1710. dh = CDefHandler::giveDef( defName );
  1711. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1712. {
  1713. if(flip)
  1714. {
  1715. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1716. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1717. dh->ourImages[i].bitmap = hlp;
  1718. }
  1719. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1720. }
  1721. dh->alphaTransformed = true;
  1722. if(flip)
  1723. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1724. else
  1725. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1726. //coloring flag and adding transparency
  1727. for(int i=0; i<flag->ourImages.size(); ++i)
  1728. {
  1729. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1730. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1731. }
  1732. }
  1733. CBattleHero::~CBattleHero()
  1734. {
  1735. delete dh;
  1736. delete flag;
  1737. }
  1738. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1739. {
  1740. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1741. ret.second = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
  1742. //counting x
  1743. if(attacker)
  1744. {
  1745. ret.first = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  1746. }
  1747. else
  1748. {
  1749. ret.first = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  1750. }
  1751. //shifting position for double - hex creatures
  1752. if(creature && creature->isDoubleWide())
  1753. {
  1754. if(attacker)
  1755. {
  1756. ret.first -= 42;
  1757. }
  1758. else
  1759. {
  1760. ret.first += 42;
  1761. }
  1762. }
  1763. //returning
  1764. return ret;
  1765. }
  1766. void CBattleHex::activate()
  1767. {
  1768. Hoverable::activate();
  1769. MotionInterested::activate();
  1770. ClickableL::activate();
  1771. ClickableR::activate();
  1772. }
  1773. void CBattleHex::deactivate()
  1774. {
  1775. Hoverable::deactivate();
  1776. MotionInterested::deactivate();
  1777. ClickableL::deactivate();
  1778. ClickableR::deactivate();
  1779. }
  1780. void CBattleHex::hover(bool on)
  1781. {
  1782. hovered = on;
  1783. Hoverable::hover(on);
  1784. if(!on && setAlterText)
  1785. {
  1786. myInterface->console->alterTxt = std::string();
  1787. setAlterText = false;
  1788. }
  1789. }
  1790. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1791. {
  1792. }
  1793. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1794. {
  1795. if(myInterface->cellShade)
  1796. {
  1797. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1798. {
  1799. strictHovered = false;
  1800. }
  1801. else //hovered pixel is inside hex
  1802. {
  1803. strictHovered = true;
  1804. }
  1805. }
  1806. if(hovered && strictHovered) //print attacked creature to console
  1807. {
  1808. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1809. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1810. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1811. {
  1812. char tabh[160];
  1813. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1814. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1815. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1816. myInterface->console->alterTxt = std::string(tabh);
  1817. setAlterText = true;
  1818. }
  1819. }
  1820. else if(setAlterText)
  1821. {
  1822. myInterface->console->alterTxt = std::string();
  1823. setAlterText = false;
  1824. }
  1825. }
  1826. void CBattleHex::clickLeft(boost::logic::tribool down)
  1827. {
  1828. if(!down && hovered && strictHovered) //we've been really clicked!
  1829. {
  1830. myInterface->hexLclicked(myNumber);
  1831. }
  1832. }
  1833. void CBattleHex::clickRight(boost::logic::tribool down)
  1834. {
  1835. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1836. if(hovered && strictHovered && stID!=-1)
  1837. {
  1838. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1839. if(!myst.alive()) return;
  1840. StackState *pom = NULL;
  1841. if(down)
  1842. {
  1843. pom = new StackState();
  1844. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1845. if(h)
  1846. {
  1847. pom->attackBonus = h->getPrimSkillLevel(0);
  1848. pom->defenseBonus = h->getPrimSkillLevel(1);
  1849. pom->luck = h->getCurrentLuck();
  1850. pom->morale = h->getCurrentMorale();
  1851. pom->shotsLeft = myst.shots;
  1852. }
  1853. pom->currentHealth = myst.firstHPleft;
  1854. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1855. ->activate();
  1856. }
  1857. delete pom;
  1858. }
  1859. }
  1860. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1861. {
  1862. }
  1863. CBattleConsole::~CBattleConsole()
  1864. {
  1865. texts.clear();
  1866. }
  1867. void CBattleConsole::show(SDL_Surface * to)
  1868. {
  1869. if(alterTxt.size())
  1870. {
  1871. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1872. }
  1873. else if(texts.size())
  1874. {
  1875. if(texts.size()==1)
  1876. {
  1877. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1878. }
  1879. else
  1880. {
  1881. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1882. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1883. }
  1884. }
  1885. }
  1886. bool CBattleConsole::addText(const std::string & text)
  1887. {
  1888. if(text.size()>70)
  1889. return false; //text too long!
  1890. int firstInToken = 0;
  1891. for(int i=0; i<text.size(); ++i) //tokenize
  1892. {
  1893. if(text[i] == 10)
  1894. {
  1895. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1896. firstInToken = i+1;
  1897. }
  1898. }
  1899. texts.push_back( text.substr(firstInToken, text.size()) );
  1900. lastShown = texts.size()-1;
  1901. return true;
  1902. }
  1903. void CBattleConsole::eraseText(unsigned int pos)
  1904. {
  1905. if(pos < texts.size())
  1906. {
  1907. texts.erase(texts.begin() + pos);
  1908. if(lastShown == texts.size())
  1909. --lastShown;
  1910. }
  1911. }
  1912. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  1913. {
  1914. if(pos >= texts.size()) //no such pos
  1915. return;
  1916. texts[pos] = text;
  1917. }
  1918. void CBattleConsole::scrollUp(unsigned int by)
  1919. {
  1920. if(lastShown > by)
  1921. lastShown -= by;
  1922. }
  1923. void CBattleConsole::scrollDown(unsigned int by)
  1924. {
  1925. if(lastShown + by < texts.size())
  1926. lastShown += by;
  1927. }
  1928. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1929. {
  1930. this->pos = pos;
  1931. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1932. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1933. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1934. SDL_FreeSurface(background);
  1935. background = pom;
  1936. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", SDLK_RETURN);
  1937. if(br.winner==0) //attacker won
  1938. {
  1939. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1940. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1941. }
  1942. else //if(br.winner==1)
  1943. {
  1944. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1945. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1946. }
  1947. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1948. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1949. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1950. std::string attackerName, defenderName;
  1951. if(owner->attackingHeroInstance) //a hero attacked
  1952. {
  1953. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1954. //setting attackerName
  1955. attackerName = owner->attackingHeroInstance->name;
  1956. }
  1957. else //a monster attacked
  1958. {
  1959. int bestMonsterID = -1;
  1960. int bestPower = 0;
  1961. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1962. {
  1963. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1964. {
  1965. bestPower = CGI->creh->creatures[it->first].AIValue;
  1966. bestMonsterID = it->first;
  1967. }
  1968. }
  1969. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1970. //setting attackerName
  1971. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1972. }
  1973. if(owner->defendingHeroInstance) //a hero defended
  1974. {
  1975. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1976. //setting defenderName
  1977. defenderName = owner->defendingHeroInstance->name;
  1978. }
  1979. else //a monster defended
  1980. {
  1981. int bestMonsterID = -1;
  1982. int bestPower = 0;
  1983. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1984. {
  1985. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1986. {
  1987. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1988. bestMonsterID = it->second.first;
  1989. }
  1990. }
  1991. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1992. //setting defenderName
  1993. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1994. }
  1995. //printing attacker and defender's names
  1996. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1997. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1998. //printing casualities
  1999. for(int step = 0; step < 2; ++step)
  2000. {
  2001. if(br.casualties[step].size()==0)
  2002. {
  2003. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  2004. }
  2005. else
  2006. {
  2007. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  2008. int yPos = 344 + step*97;
  2009. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  2010. {
  2011. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  2012. std::stringstream amount;
  2013. amount<<it->second;
  2014. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  2015. xPos += 42;
  2016. }
  2017. }
  2018. }
  2019. //printing result description
  2020. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  2021. switch(br.result)
  2022. {
  2023. case 0: //normal victory
  2024. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2025. {
  2026. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  2027. }
  2028. else
  2029. {
  2030. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  2031. }
  2032. break;
  2033. case 1: //flee
  2034. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2035. {
  2036. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  2037. }
  2038. else
  2039. {
  2040. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  2041. }
  2042. break;
  2043. case 2: //surrender
  2044. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2045. {
  2046. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  2047. }
  2048. else
  2049. {
  2050. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  2051. }
  2052. break;
  2053. }
  2054. }
  2055. CBattleReslutWindow::~CBattleReslutWindow()
  2056. {
  2057. SDL_FreeSurface(background);
  2058. }
  2059. void CBattleReslutWindow::activate()
  2060. {
  2061. LOCPLINT->showingDialog->set(true);
  2062. exit->activate();
  2063. }
  2064. void CBattleReslutWindow::deactivate()
  2065. {
  2066. exit->deactivate();
  2067. }
  2068. void CBattleReslutWindow::show(SDL_Surface *to)
  2069. {
  2070. //evaluating to
  2071. if(!to)
  2072. to = screen;
  2073. SDL_BlitSurface(background, NULL, to, &pos);
  2074. exit->show(to);
  2075. }
  2076. void CBattleReslutWindow::bExitf()
  2077. {
  2078. LOCPLINT->battleResultQuited();
  2079. }
  2080. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  2081. {
  2082. pos = position;
  2083. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  2084. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2085. 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);
  2086. viewGrid->select(owner->printCellBorders);
  2087. 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);
  2088. movementShadow->select(owner->printStackRange);
  2089. 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);
  2090. mouseShadow->select(owner->printMouseShadow);
  2091. animSpeeds = new CHighlightableButtonsGroup(0);
  2092. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[422].first),CGI->preth->zelp[422].second, "sysopb9.def",188, 309, 1);
  2093. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[423].first),CGI->preth->zelp[423].second, "sysob10.def",252, 309, 2);
  2094. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[424].first),CGI->preth->zelp[424].second, "sysob11.def",315, 309, 4);
  2095. animSpeeds->select(owner->getAnimSpeed(), 1);
  2096. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  2097. setToDefault = new AdventureMapButton (CGI->preth->zelp[392].first, CGI->preth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def");
  2098. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  2099. exit = new AdventureMapButton (CGI->preth->zelp[393].first, CGI->preth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def",SDLK_RETURN);
  2100. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  2101. //printing texts to background
  2102. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  2103. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  2104. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  2105. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  2106. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  2107. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  2108. //auto - combat options
  2109. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  2110. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  2111. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  2112. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  2113. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  2114. //creature info
  2115. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  2116. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  2117. //general options
  2118. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  2119. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  2120. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  2121. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  2122. //texts printed
  2123. }
  2124. CBattleOptionsWindow::~CBattleOptionsWindow()
  2125. {
  2126. SDL_FreeSurface(background);
  2127. delete setToDefault;
  2128. delete exit;
  2129. delete viewGrid;
  2130. delete movementShadow;
  2131. delete animSpeeds;
  2132. delete mouseShadow;
  2133. }
  2134. void CBattleOptionsWindow::activate()
  2135. {
  2136. setToDefault->activate();
  2137. exit->activate();
  2138. viewGrid->activate();
  2139. movementShadow->activate();
  2140. animSpeeds->activate();
  2141. mouseShadow->activate();
  2142. }
  2143. void CBattleOptionsWindow::deactivate()
  2144. {
  2145. setToDefault->deactivate();
  2146. exit->deactivate();
  2147. viewGrid->deactivate();
  2148. movementShadow->deactivate();
  2149. animSpeeds->deactivate();
  2150. mouseShadow->deactivate();
  2151. }
  2152. void CBattleOptionsWindow::show(SDL_Surface *to)
  2153. {
  2154. if(!to) //"evaluating" to
  2155. to = screen;
  2156. SDL_BlitSurface(background, NULL, to, &pos);
  2157. setToDefault->show(to);
  2158. exit->show(to);
  2159. viewGrid->show(to);
  2160. movementShadow->show(to);
  2161. animSpeeds->show(to);
  2162. mouseShadow->show(to);
  2163. }
  2164. void CBattleOptionsWindow::bDefaultf()
  2165. {
  2166. }
  2167. void CBattleOptionsWindow::bExitf()
  2168. {
  2169. deactivate();
  2170. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  2171. {
  2172. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  2173. {
  2174. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  2175. break;
  2176. }
  2177. }
  2178. delete this;
  2179. LOCPLINT->curint->activate();
  2180. }