CBattleInterface.cpp 74 KB

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