CBattleInterface.cpp 64 KB

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