CBattleInterface.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055
  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)
  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);
  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);
  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()!=2) && stacks[stackDeadByHex[b][v]].alive(), 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)
  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->battleGetStackByID(activeStack)->creature->isShooting()) //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)
  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. if((animCount+1)%(4/animSpeed)==0)
  518. creAnims[number]->incrementFrame();
  519. }
  520. creDir[number] = !creDir[number];
  521. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  522. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  523. creAnims[number]->pos.x = coords.first;
  524. //creAnims[number]->pos.y = coords.second;
  525. if(wideTrick && curs.creature->isDoubleWide())
  526. {
  527. if(!creDir[number])
  528. creAnims[number]->pos.x -= 44;
  529. }
  530. creAnims[number]->setType(7);
  531. firstFrame = creAnims[number]->getFrame();
  532. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(7) + firstFrame - 1; ++g)
  533. {
  534. show();
  535. CSDL_Ext::update();
  536. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  537. }
  538. creAnims[number]->setType(2);
  539. return true;
  540. }
  541. void CBattleInterface::bOptionsf()
  542. {
  543. CGI->curh->changeGraphic(0,0);
  544. deactivate();
  545. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  546. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  547. optionsWin->activate();
  548. LOCPLINT->objsToBlit.push_back(optionsWin);
  549. }
  550. void CBattleInterface::bSurrenderf()
  551. {
  552. }
  553. void CBattleInterface::bFleef()
  554. {
  555. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  556. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  557. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  558. }
  559. void CBattleInterface::reallyFlee()
  560. {
  561. giveCommand(4,0,0);
  562. CGI->curh->changeGraphic(0, 0);
  563. }
  564. void CBattleInterface::bAutofightf()
  565. {
  566. }
  567. void CBattleInterface::bSpellf()
  568. {
  569. CGI->curh->changeGraphic(0,0);
  570. deactivate();
  571. const CGHeroInstance * chi = NULL;
  572. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  573. chi = attackingHeroInstance;
  574. else
  575. chi = defendingHeroInstance;
  576. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), chi);
  577. spellWindow->activate();
  578. LOCPLINT->objsToBlit.push_back(spellWindow);
  579. }
  580. void CBattleInterface::bWaitf()
  581. {
  582. }
  583. void CBattleInterface::bDefencef()
  584. {
  585. giveCommand(3,0,activeStack);
  586. }
  587. void CBattleInterface::bConsoleUpf()
  588. {
  589. console->scrollUp();
  590. }
  591. void CBattleInterface::bConsoleDownf()
  592. {
  593. console->scrollDown();
  594. }
  595. void CBattleInterface::newStack(CStack stack)
  596. {
  597. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  598. creAnims[stack.ID]->setType(2);
  599. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  600. }
  601. void CBattleInterface::stackRemoved(CStack stack)
  602. {
  603. delete creAnims[stack.ID];
  604. creAnims.erase(stack.ID);
  605. }
  606. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  607. {
  608. if(creAnims[ID]->getType() != 2)
  609. {
  610. return; //something went wrong
  611. }
  612. if(byShooting) //delay hit animation
  613. {
  614. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  615. while(true)
  616. {
  617. bool found = false;
  618. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  619. {
  620. if(it->creID == attacker.creature->idNumber)
  621. {
  622. found = true;
  623. break;
  624. }
  625. }
  626. if(!found)
  627. break;
  628. else
  629. {
  630. show();
  631. CSDL_Ext::update();
  632. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  633. }
  634. }
  635. }
  636. creAnims[ID]->setType(5); //death
  637. int firstFrame = creAnims[ID]->getFrame();
  638. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(5) + firstFrame - 1; ++i)
  639. {
  640. if((animCount%(4/animSpeed))==0)
  641. creAnims[ID]->incrementFrame();
  642. show();
  643. CSDL_Ext::update();
  644. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  645. }
  646. printConsoleAttacked(ID, dmg, killed, IDby);
  647. }
  648. void CBattleInterface::stackActivated(int number)
  649. {
  650. //givenCommand = NULL;
  651. activeStack = number;
  652. myTurn = true;
  653. redrawBackgroundWithHexes(number);
  654. }
  655. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving)
  656. {
  657. bool startMoving = creAnims[number]->type==20;
  658. //a few useful variables
  659. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  660. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  661. int hexWbase = 44, hexHbase = 42;
  662. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  663. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  664. {
  665. deactivate();
  666. CGI->curh->hide();
  667. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  668. {
  669. show();
  670. CSDL_Ext::update();
  671. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  672. if((animCount+1)%(4/animSpeed)==0)
  673. creAnims[number]->incrementFrame();
  674. }
  675. }
  676. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  677. {
  678. switch(mutPos) //reverse unit if necessary
  679. {
  680. case 0: case 4: case 5:
  681. if(creDir[number] == true)
  682. reverseCreature(number, curStackPos, twoTiles);
  683. break;
  684. case 1: case 2: case 3:
  685. if(creDir[number] == false)
  686. reverseCreature(number, curStackPos, twoTiles);
  687. break;
  688. }
  689. //moving instructions
  690. creAnims[number]->setType(0);
  691. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  692. for(int i=0; i<steps; ++i)
  693. {
  694. switch(mutPos)
  695. {
  696. case 0:
  697. posX -= ((float)hexWbase)/(2.0f*steps);
  698. creAnims[number]->pos.x = posX;
  699. posY -= ((float)hexHbase)/((float)steps);
  700. creAnims[number]->pos.y = posY;
  701. break;
  702. case 1:
  703. posX += ((float)hexWbase)/(2.0f*steps);
  704. creAnims[number]->pos.x = posX;
  705. posY -= ((float)hexHbase)/((float)steps);
  706. creAnims[number]->pos.y = posY;
  707. break;
  708. case 2:
  709. posX += ((float)hexWbase)/((float)steps);
  710. creAnims[number]->pos.x = posX;
  711. break;
  712. case 3:
  713. posX += ((float)hexWbase)/(2.0f*steps);
  714. creAnims[number]->pos.x = posX;
  715. posY += ((float)hexHbase)/((float)steps);
  716. creAnims[number]->pos.y = posY;
  717. break;
  718. case 4:
  719. posX -= ((float)hexWbase)/(2.0f*steps);
  720. creAnims[number]->pos.x = posX;
  721. posY += ((float)hexHbase)/((float)steps);
  722. creAnims[number]->pos.y = posY;
  723. break;
  724. case 5:
  725. posX -= ((float)hexWbase)/((float)steps);
  726. creAnims[number]->pos.x = posX;
  727. break;
  728. }
  729. show();
  730. CSDL_Ext::update();
  731. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  732. if((animCount+1)%(4/animSpeed)==0)
  733. creAnims[number]->incrementFrame();
  734. }
  735. }
  736. if(endMoving) //animation of ending move
  737. {
  738. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  739. {
  740. creAnims[number]->setType(21);
  741. for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  742. {
  743. show();
  744. CSDL_Ext::update();
  745. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  746. if((animCount+1)%(4/animSpeed)==0)
  747. creAnims[number]->incrementFrame();
  748. }
  749. activate();
  750. }
  751. creAnims[number]->setType(2); //resetting to default
  752. CGI->curh->show();
  753. }
  754. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  755. if(endMoving) //resetting to default
  756. {
  757. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  758. reverseCreature(number, destHex, twoTiles);
  759. }
  760. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  761. creAnims[number]->pos.x = coords.first;
  762. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  763. creAnims[number]->pos.x -= 44;
  764. creAnims[number]->pos.y = coords.second;
  765. }
  766. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  767. {
  768. while(creAnims[ID]->getType() != 2 || (attackingInfo && attackingInfo->IDby == IDby))
  769. {
  770. show();
  771. CSDL_Ext::update();
  772. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  773. }
  774. if(byShooting) //delay hit animation
  775. {
  776. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  777. while(true)
  778. {
  779. bool found = false;
  780. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  781. {
  782. if(it->creID == attacker.creature->idNumber)
  783. {
  784. found = true;
  785. break;
  786. }
  787. }
  788. if(!found)
  789. break;
  790. else
  791. {
  792. show();
  793. CSDL_Ext::update();
  794. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  795. }
  796. }
  797. }
  798. creAnims[ID]->setType(3); //getting hit
  799. int firstFrame = creAnims[ID]->getFrame();
  800. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(3) + firstFrame - 1; ++i)
  801. {
  802. show();
  803. CSDL_Ext::update();
  804. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  805. /*if((animCount+1)%(4/animSpeed)==0)
  806. creAnims[ID]->incrementFrame();*/
  807. }
  808. creAnims[ID]->setType(2);
  809. printConsoleAttacked(ID, dmg, killed, IDby);
  810. }
  811. void CBattleInterface::stackAttacking(int ID, int dest)
  812. {
  813. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  814. {
  815. show();
  816. CSDL_Ext::update();
  817. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  818. }
  819. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  820. if(aStack.attackerOwned)
  821. {
  822. if(aStack.creature->isDoubleWide())
  823. {
  824. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  825. {
  826. case 0:
  827. //reverseCreature(ID, aStack.position, true);
  828. break;
  829. case 1:
  830. break;
  831. case 2:
  832. break;
  833. case 3:
  834. break;
  835. case 4:
  836. //reverseCreature(ID, aStack.position, true);
  837. break;
  838. case 5:
  839. reverseCreature(ID, aStack.position, true);
  840. break;
  841. }
  842. }
  843. else //else for if(aStack.creature->isDoubleWide())
  844. {
  845. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  846. {
  847. case 0:
  848. reverseCreature(ID, aStack.position, true);
  849. break;
  850. case 1:
  851. break;
  852. case 2:
  853. break;
  854. case 3:
  855. break;
  856. case 4:
  857. reverseCreature(ID, aStack.position, true);
  858. break;
  859. case 5:
  860. reverseCreature(ID, aStack.position, true);
  861. break;
  862. }
  863. }
  864. }
  865. else //if(aStack.attackerOwned)
  866. {
  867. if(aStack.creature->isDoubleWide())
  868. {
  869. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  870. {
  871. case 0:
  872. //reverseCreature(ID, aStack.position, true);
  873. break;
  874. case 1:
  875. break;
  876. case 2:
  877. reverseCreature(ID, aStack.position, true);
  878. break;
  879. case 3:
  880. break;
  881. case 4:
  882. //reverseCreature(ID, aStack.position, true);
  883. break;
  884. case 5:
  885. //reverseCreature(ID, aStack.position, true);
  886. break;
  887. }
  888. }
  889. else //else for if(aStack.creature->isDoubleWide())
  890. {
  891. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  892. {
  893. case 0:
  894. //reverseCreature(ID, aStack.position, true);
  895. break;
  896. case 1:
  897. reverseCreature(ID, aStack.position, true);
  898. break;
  899. case 2:
  900. reverseCreature(ID, aStack.position, true);
  901. break;
  902. case 3:
  903. reverseCreature(ID, aStack.position, true);
  904. break;
  905. case 4:
  906. //reverseCreature(ID, aStack.position, true);
  907. break;
  908. case 5:
  909. //reverseCreature(ID, aStack.position, true);
  910. break;
  911. }
  912. }
  913. }
  914. attackingInfo = new CAttHelper;
  915. attackingInfo->dest = dest;
  916. attackingInfo->frame = 0;
  917. attackingInfo->hitCount = 0;
  918. attackingInfo->ID = ID;
  919. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  920. attackingInfo->reversing = false;
  921. attackingInfo->shooting = false;
  922. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  923. {
  924. case 0:
  925. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  926. break;
  927. case 1:
  928. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  929. break;
  930. case 2:
  931. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  932. break;
  933. case 3:
  934. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  935. break;
  936. case 4:
  937. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  938. break;
  939. case 5:
  940. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  941. break;
  942. }
  943. }
  944. void CBattleInterface::newRound(int number)
  945. {
  946. console->addText(CGI->generaltexth->allTexts[412]);
  947. }
  948. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  949. {
  950. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  951. ba->actionType = action;
  952. ba->destinationTile = tile;
  953. ba->stackNumber = stack;
  954. ba->additionalInfo = additional;
  955. givenCommand->setn(ba);
  956. myTurn = false;
  957. activeStack = -1;
  958. }
  959. bool CBattleInterface::isTileAttackable(int number)
  960. {
  961. for(int b=0; b<shadedHexes.size(); ++b)
  962. {
  963. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  964. return true;
  965. }
  966. return false;
  967. }
  968. void CBattleInterface::hexLclicked(int whichOne)
  969. {
  970. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  971. {
  972. if(!myTurn)
  973. return; //we are not permit to do anything
  974. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  975. if(!dest || !dest->alive()) //no creature at that tile
  976. {
  977. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  978. giveCommand(2,whichOne,activeStack);
  979. }
  980. else if(dest->owner != attackingHeroInstance->tempOwner
  981. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)
  982. && BattleInfo::mutualPosition(LOCPLINT->cb->battleGetPos(activeStack),whichOne) < 0 ) //shooting
  983. {
  984. giveCommand(7,whichOne,activeStack);
  985. }
  986. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  987. {
  988. //std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  989. //for(int i=0;i<n.size();i++)
  990. //{
  991. // //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
  992. // if(vstd::contains(shadedHexes,n[i]))
  993. // {
  994. // giveCommand(6,n[i],activeStack,whichOne);
  995. // return;
  996. // }
  997. //}
  998. switch(CGI->curh->number)
  999. {
  1000. case 12:
  1001. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1002. break;
  1003. case 7:
  1004. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1005. break;
  1006. case 8:
  1007. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1008. break;
  1009. case 9:
  1010. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1011. break;
  1012. case 10:
  1013. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1014. break;
  1015. case 11:
  1016. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. void CBattleInterface::stackIsShooting(int ID, int dest)
  1023. {
  1024. if(attackingInfo != NULL)
  1025. {
  1026. return; //something went wrong
  1027. }
  1028. //projectile
  1029. float projectileAngle; //in radians; if positive, projectiles goes up
  1030. 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)
  1031. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1032. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1033. if(fromHex < dest)
  1034. projectileAngle = -projectileAngle;
  1035. SProjectileInfo spi;
  1036. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1037. spi.step = 0;
  1038. spi.frameNum = 0;
  1039. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1040. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1041. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1042. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1043. if(projectileAngle > straightAngle) //upper shot
  1044. {
  1045. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1046. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1047. }
  1048. else if(projectileAngle < -straightAngle) //lower shot
  1049. {
  1050. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1051. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1052. }
  1053. else //straight shot
  1054. {
  1055. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1056. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1057. }
  1058. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1059. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1060. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1061. //set starting frame
  1062. if(spi.spin)
  1063. {
  1064. spi.frameNum = 0;
  1065. }
  1066. else
  1067. {
  1068. 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);
  1069. }
  1070. //set delay
  1071. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1072. projectiles.push_back(spi);
  1073. //attack aniamtion
  1074. attackingInfo = new CAttHelper;
  1075. attackingInfo->dest = dest;
  1076. attackingInfo->frame = 0;
  1077. attackingInfo->hitCount = 0;
  1078. attackingInfo->ID = ID;
  1079. attackingInfo->reversing = false;
  1080. attackingInfo->shooting = true;
  1081. if(projectileAngle > straightAngle) //upper shot
  1082. attackingInfo->shootingGroup = 14;
  1083. else if(projectileAngle < -straightAngle) //lower shot
  1084. attackingInfo->shootingGroup = 16;
  1085. else //straight shot
  1086. attackingInfo->shootingGroup = 15;
  1087. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1088. }
  1089. void CBattleInterface::battleFinished(const BattleResult& br)
  1090. {
  1091. deactivate();
  1092. CGI->curh->changeGraphic(0,0);
  1093. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1094. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1095. resWindow->activate();
  1096. }
  1097. void CBattleInterface::setAnimSpeed(int set)
  1098. {
  1099. animSpeed = set;
  1100. }
  1101. int CBattleInterface::getAnimSpeed() const
  1102. {
  1103. return animSpeed;
  1104. }
  1105. float CBattleInterface::getAnimSpeedMultiplier() const
  1106. {
  1107. switch(animSpeed)
  1108. {
  1109. case 1:
  1110. return 3.5f;
  1111. case 2:
  1112. return 2.2f;
  1113. case 4:
  1114. return 1.0f;
  1115. default:
  1116. return 0.0f;
  1117. }
  1118. }
  1119. void CBattleInterface::attackingShowHelper()
  1120. {
  1121. if(attackingInfo && !attackingInfo->reversing)
  1122. {
  1123. if(attackingInfo->frame == 0)
  1124. {
  1125. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1126. if(attackingInfo->shooting)
  1127. {
  1128. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1129. }
  1130. else
  1131. {
  1132. if(aStack.creature->isDoubleWide())
  1133. {
  1134. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1135. {
  1136. case 0:
  1137. creAnims[attackingInfo->ID]->setType(11);
  1138. break;
  1139. case 1:
  1140. creAnims[attackingInfo->ID]->setType(11);
  1141. break;
  1142. case 2:
  1143. creAnims[attackingInfo->ID]->setType(12);
  1144. break;
  1145. case 3:
  1146. creAnims[attackingInfo->ID]->setType(13);
  1147. break;
  1148. case 4:
  1149. creAnims[attackingInfo->ID]->setType(13);
  1150. break;
  1151. case 5:
  1152. creAnims[attackingInfo->ID]->setType(12);
  1153. break;
  1154. }
  1155. }
  1156. else //else for if(aStack.creature->isDoubleWide())
  1157. {
  1158. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1159. {
  1160. case 0:
  1161. creAnims[attackingInfo->ID]->setType(11);
  1162. break;
  1163. case 1:
  1164. creAnims[attackingInfo->ID]->setType(11);
  1165. break;
  1166. case 2:
  1167. creAnims[attackingInfo->ID]->setType(12);
  1168. break;
  1169. case 3:
  1170. creAnims[attackingInfo->ID]->setType(13);
  1171. break;
  1172. case 4:
  1173. creAnims[attackingInfo->ID]->setType(13);
  1174. break;
  1175. case 5:
  1176. creAnims[attackingInfo->ID]->setType(12);
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1183. {
  1184. attackingInfo->reversing = true;
  1185. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1186. if(aStackp == NULL)
  1187. return;
  1188. CStack aStack = *aStackp;
  1189. if(aStack.attackerOwned)
  1190. {
  1191. if(aStack.creature->isDoubleWide())
  1192. {
  1193. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1194. {
  1195. case 0:
  1196. //reverseCreature(ID, aStack.position, true);
  1197. break;
  1198. case 1:
  1199. break;
  1200. case 2:
  1201. break;
  1202. case 3:
  1203. break;
  1204. case 4:
  1205. //reverseCreature(ID, aStack.position, true);
  1206. break;
  1207. case 5:
  1208. reverseCreature(attackingInfo->ID, aStack.position, true);
  1209. break;
  1210. }
  1211. }
  1212. else //else for if(aStack.creature->isDoubleWide())
  1213. {
  1214. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1215. {
  1216. case 0:
  1217. reverseCreature(attackingInfo->ID, aStack.position, true);
  1218. break;
  1219. case 1:
  1220. break;
  1221. case 2:
  1222. break;
  1223. case 3:
  1224. break;
  1225. case 4:
  1226. reverseCreature(attackingInfo->ID, aStack.position, true);
  1227. break;
  1228. case 5:
  1229. reverseCreature(attackingInfo->ID, aStack.position, true);
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. else //if(aStack.attackerOwned)
  1235. {
  1236. if(aStack.creature->isDoubleWide())
  1237. {
  1238. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1239. {
  1240. case 0:
  1241. //reverseCreature(ID, aStack.position, true);
  1242. break;
  1243. case 1:
  1244. break;
  1245. case 2:
  1246. reverseCreature(attackingInfo->ID, aStack.position, true);
  1247. break;
  1248. case 3:
  1249. break;
  1250. case 4:
  1251. //reverseCreature(ID, aStack.position, true);
  1252. break;
  1253. case 5:
  1254. //reverseCreature(ID, aStack.position, true);
  1255. break;
  1256. }
  1257. }
  1258. else //else for 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. reverseCreature(attackingInfo->ID, aStack.position, true);
  1267. break;
  1268. case 2:
  1269. reverseCreature(attackingInfo->ID, aStack.position, true);
  1270. break;
  1271. case 3:
  1272. reverseCreature(attackingInfo->ID, aStack.position, true);
  1273. break;
  1274. case 4:
  1275. //reverseCreature(ID, aStack.position, true);
  1276. break;
  1277. case 5:
  1278. //reverseCreature(ID, aStack.position, true);
  1279. break;
  1280. }
  1281. }
  1282. }
  1283. attackingInfo->reversing = false;
  1284. creAnims[attackingInfo->ID]->setType(2);
  1285. delete attackingInfo;
  1286. attackingInfo = NULL;
  1287. }
  1288. if(attackingInfo)
  1289. {
  1290. attackingInfo->hitCount++;
  1291. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1292. attackingInfo->frame++;
  1293. }
  1294. }
  1295. }
  1296. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1297. {
  1298. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1299. //preparating background graphic with hexes and shaded hexes
  1300. blitAt(background, 0, 0, backgroundWithHexes);
  1301. if(printCellBorders)
  1302. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1303. if(printStackRange)
  1304. {
  1305. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1306. {
  1307. int i = shadedHexes[m]/17; //row
  1308. int j = shadedHexes[m]%17-1; //column
  1309. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1310. int y = 86 + 42 * i;
  1311. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1312. }
  1313. }
  1314. }
  1315. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1316. {
  1317. char tabh[200];
  1318. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1319. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1320. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1321. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1322. dmg);
  1323. if(killed > 0)
  1324. {
  1325. if(killed > 1)
  1326. {
  1327. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1328. }
  1329. else //killed == 1
  1330. {
  1331. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1332. }
  1333. }
  1334. console->addText(std::string(tabh));
  1335. }
  1336. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1337. {
  1338. if(to == NULL)
  1339. to = screen;
  1340. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1341. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1342. {
  1343. if(it->animStartDelay>0)
  1344. {
  1345. --(it->animStartDelay);
  1346. continue;
  1347. }
  1348. SDL_Rect dst;
  1349. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1350. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1351. dst.x = it->x;
  1352. dst.y = it->y;
  1353. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1354. //actualizing projectile
  1355. ++it->step;
  1356. if(it->step == it->lastStep)
  1357. {
  1358. toBeDeleted.insert(toBeDeleted.end(), it);
  1359. }
  1360. else
  1361. {
  1362. it->x += it->dx;
  1363. it->y += it->dy;
  1364. if(it->spin)
  1365. {
  1366. ++(it->frameNum);
  1367. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1368. }
  1369. }
  1370. }
  1371. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1372. {
  1373. projectiles.erase(*it);
  1374. }
  1375. }
  1376. void CBattleHero::show(SDL_Surface *to)
  1377. {
  1378. //animation of flag
  1379. if(flip)
  1380. {
  1381. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1382. }
  1383. else
  1384. {
  1385. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1386. }
  1387. ++flagAnimCount;
  1388. if(flagAnimCount%4==0)
  1389. {
  1390. ++flagAnim;
  1391. flagAnim %= flag->ourImages.size();
  1392. }
  1393. //animation of hero
  1394. int tick=-1;
  1395. for(int i=0; i<dh->ourImages.size(); ++i)
  1396. {
  1397. if(dh->ourImages[i].groupNumber==phase)
  1398. ++tick;
  1399. if(tick==image)
  1400. {
  1401. SDL_Rect posb = pos;
  1402. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1403. ++image;
  1404. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1405. {
  1406. image = 0;
  1407. }
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. void CBattleHero::activate()
  1413. {
  1414. ClickableL::activate();
  1415. }
  1416. void CBattleHero::deactivate()
  1417. {
  1418. ClickableL::deactivate();
  1419. }
  1420. void CBattleHero::clickLeft(boost::logic::tribool down)
  1421. {
  1422. if(!down && myHero)
  1423. {
  1424. CGI->curh->changeGraphic(0,0);
  1425. LOCPLINT->curint->deactivate();
  1426. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1427. spellWindow->activate();
  1428. LOCPLINT->objsToBlit.push_back(spellWindow);
  1429. }
  1430. }
  1431. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, unsigned char player, const CGHeroInstance * hero): phase(phaseG), image(imageG), flip(flipG), flagAnim(0), myHero(hero)
  1432. {
  1433. dh = CDefHandler::giveDef( defName );
  1434. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1435. {
  1436. if(flip)
  1437. {
  1438. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1439. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1440. dh->ourImages[i].bitmap = hlp;
  1441. }
  1442. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1443. }
  1444. dh->alphaTransformed = true;
  1445. if(flip)
  1446. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1447. else
  1448. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1449. //coloring flag and adding transparency
  1450. for(int i=0; i<flag->ourImages.size(); ++i)
  1451. {
  1452. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1453. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1454. }
  1455. }
  1456. CBattleHero::~CBattleHero()
  1457. {
  1458. delete dh;
  1459. delete flag;
  1460. }
  1461. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1462. {
  1463. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1464. ret.second = -139 + 42 * (hexNum/17); //counting y
  1465. //counting x
  1466. if(attacker)
  1467. {
  1468. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1469. }
  1470. else
  1471. {
  1472. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1473. }
  1474. //shifting position for double - hex creatures
  1475. if(creature->isDoubleWide())
  1476. {
  1477. if(attacker)
  1478. {
  1479. ret.first -= 42;
  1480. }
  1481. else
  1482. {
  1483. ret.first += 42;
  1484. }
  1485. }
  1486. //returning
  1487. return ret;
  1488. }
  1489. void CBattleHex::activate()
  1490. {
  1491. Hoverable::activate();
  1492. MotionInterested::activate();
  1493. ClickableL::activate();
  1494. ClickableR::activate();
  1495. }
  1496. void CBattleHex::deactivate()
  1497. {
  1498. Hoverable::deactivate();
  1499. MotionInterested::deactivate();
  1500. ClickableL::deactivate();
  1501. ClickableR::deactivate();
  1502. }
  1503. void CBattleHex::hover(bool on)
  1504. {
  1505. hovered = on;
  1506. Hoverable::hover(on);
  1507. if(!on && setAlterText)
  1508. {
  1509. myInterface->console->alterTxt = std::string();
  1510. setAlterText = false;
  1511. }
  1512. }
  1513. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1514. {
  1515. }
  1516. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1517. {
  1518. if(myInterface->cellShade)
  1519. {
  1520. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1521. {
  1522. strictHovered = false;
  1523. }
  1524. else //hovered pixel is inside hex
  1525. {
  1526. strictHovered = true;
  1527. }
  1528. }
  1529. if(hovered && strictHovered) //print attacked creature to console
  1530. {
  1531. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1532. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1533. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1534. {
  1535. char tabh[160];
  1536. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1537. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1538. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1539. myInterface->console->alterTxt = std::string(tabh);
  1540. setAlterText = true;
  1541. }
  1542. }
  1543. else if(setAlterText)
  1544. {
  1545. myInterface->console->alterTxt = std::string();
  1546. setAlterText = false;
  1547. }
  1548. }
  1549. void CBattleHex::clickLeft(boost::logic::tribool down)
  1550. {
  1551. if(!down && hovered && strictHovered) //we've been really clicked!
  1552. {
  1553. myInterface->hexLclicked(myNumber);
  1554. }
  1555. }
  1556. void CBattleHex::clickRight(boost::logic::tribool down)
  1557. {
  1558. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1559. if(hovered && strictHovered && stID!=-1)
  1560. {
  1561. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1562. if(!myst.alive()) return;
  1563. StackState *pom = NULL;
  1564. if(down)
  1565. {
  1566. pom = new StackState();
  1567. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1568. if(h)
  1569. {
  1570. pom->attackBonus = h->primSkills[0];
  1571. pom->defenseBonus = h->primSkills[1];
  1572. pom->luck = h->getCurrentLuck();
  1573. pom->morale = h->getCurrentMorale();
  1574. pom->currentHealth = myst.firstHPleft;
  1575. }
  1576. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1577. ->activate();
  1578. }
  1579. delete pom;
  1580. }
  1581. }
  1582. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1583. {
  1584. }
  1585. CBattleConsole::~CBattleConsole()
  1586. {
  1587. texts.clear();
  1588. }
  1589. void CBattleConsole::show(SDL_Surface * to)
  1590. {
  1591. if(alterTxt.size())
  1592. {
  1593. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1594. }
  1595. else if(texts.size())
  1596. {
  1597. if(texts.size()==1)
  1598. {
  1599. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1600. }
  1601. else
  1602. {
  1603. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1604. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1605. }
  1606. }
  1607. }
  1608. bool CBattleConsole::addText(const std::string & text)
  1609. {
  1610. if(text.size()>70)
  1611. return false; //text too long!
  1612. int firstInToken = 0;
  1613. for(int i=0; i<text.size(); ++i) //tokenize
  1614. {
  1615. if(text[i] == 10)
  1616. {
  1617. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1618. firstInToken = i+1;
  1619. }
  1620. }
  1621. texts.push_back( text.substr(firstInToken, text.size()) );
  1622. lastShown = texts.size()-1;
  1623. return true;
  1624. }
  1625. void CBattleConsole::eraseText(unsigned int pos)
  1626. {
  1627. if(pos < texts.size())
  1628. {
  1629. texts.erase(texts.begin() + pos);
  1630. if(lastShown == texts.size())
  1631. --lastShown;
  1632. }
  1633. }
  1634. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  1635. {
  1636. if(pos >= texts.size()) //no such pos
  1637. return;
  1638. texts[pos] = text;
  1639. }
  1640. void CBattleConsole::scrollUp(unsigned int by)
  1641. {
  1642. if(lastShown > by)
  1643. lastShown -= by;
  1644. }
  1645. void CBattleConsole::scrollDown(unsigned int by)
  1646. {
  1647. if(lastShown + by < texts.size())
  1648. lastShown += by;
  1649. }
  1650. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1651. {
  1652. this->pos = pos;
  1653. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1654. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1655. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1656. SDL_FreeSurface(background);
  1657. background = pom;
  1658. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1659. if(br.winner==0) //attacker won
  1660. {
  1661. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1662. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1663. }
  1664. else //if(br.winner==1)
  1665. {
  1666. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1667. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1668. }
  1669. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1670. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1671. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1672. std::string attackerName, defenderName;
  1673. if(owner->attackingHeroInstance) //a hero attacked
  1674. {
  1675. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1676. //setting attackerName
  1677. attackerName = owner->attackingHeroInstance->name;
  1678. }
  1679. else //a monster attacked
  1680. {
  1681. int bestMonsterID = -1;
  1682. int bestPower = 0;
  1683. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1684. {
  1685. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1686. {
  1687. bestPower = CGI->creh->creatures[it->first].AIValue;
  1688. bestMonsterID = it->first;
  1689. }
  1690. }
  1691. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1692. //setting attackerName
  1693. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1694. }
  1695. if(owner->defendingHeroInstance) //a hero defended
  1696. {
  1697. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1698. //setting defenderName
  1699. defenderName = owner->defendingHeroInstance->name;
  1700. }
  1701. else //a monster defended
  1702. {
  1703. int bestMonsterID = -1;
  1704. int bestPower = 0;
  1705. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1706. {
  1707. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1708. {
  1709. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1710. bestMonsterID = it->second.first;
  1711. }
  1712. }
  1713. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1714. //setting defenderName
  1715. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1716. }
  1717. //printing attacker and defender's names
  1718. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1719. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1720. //printing casualities
  1721. for(int step = 0; step < 2; ++step)
  1722. {
  1723. if(br.casualties[step].size()==0)
  1724. {
  1725. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  1726. }
  1727. else
  1728. {
  1729. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  1730. int yPos = 344 + step*97;
  1731. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  1732. {
  1733. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1734. std::stringstream amount;
  1735. amount<<it->second;
  1736. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1737. xPos += 42;
  1738. }
  1739. }
  1740. }
  1741. //printing result description
  1742. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  1743. switch(br.result)
  1744. {
  1745. case 0: //normal victory
  1746. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1747. {
  1748. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  1749. }
  1750. else
  1751. {
  1752. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  1753. }
  1754. break;
  1755. case 1: //flee
  1756. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1757. {
  1758. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  1759. }
  1760. else
  1761. {
  1762. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  1763. }
  1764. break;
  1765. case 2: //surrender
  1766. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1767. {
  1768. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  1769. }
  1770. else
  1771. {
  1772. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  1773. }
  1774. break;
  1775. }
  1776. }
  1777. CBattleReslutWindow::~CBattleReslutWindow()
  1778. {
  1779. SDL_FreeSurface(background);
  1780. }
  1781. void CBattleReslutWindow::activate()
  1782. {
  1783. LOCPLINT->showingDialog->set(true);
  1784. exit->activate();
  1785. }
  1786. void CBattleReslutWindow::deactivate()
  1787. {
  1788. exit->deactivate();
  1789. }
  1790. void CBattleReslutWindow::show(SDL_Surface *to)
  1791. {
  1792. //evaluating to
  1793. if(!to)
  1794. to = screen;
  1795. SDL_BlitSurface(background, NULL, to, &pos);
  1796. exit->show(to);
  1797. }
  1798. void CBattleReslutWindow::bExitf()
  1799. {
  1800. LOCPLINT->battleResultQuited();
  1801. }
  1802. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  1803. {
  1804. pos = position;
  1805. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  1806. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1807. 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);
  1808. viewGrid->select(owner->printCellBorders);
  1809. 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);
  1810. movementShadow->select(owner->printStackRange);
  1811. 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);
  1812. mouseShadow->select(owner->printMouseShadow);
  1813. animSpeeds = new CHighlightableButtonsGroup(0);
  1814. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[422].first),CGI->preth->zelp[422].second, "sysopb9.def",188, 309, 1);
  1815. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[423].first),CGI->preth->zelp[423].second, "sysob10.def",252, 309, 2);
  1816. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[424].first),CGI->preth->zelp[424].second, "sysob11.def",315, 309, 4);
  1817. animSpeeds->select(owner->getAnimSpeed(), 1);
  1818. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  1819. 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);
  1820. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  1821. 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);
  1822. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  1823. //printing texts to background
  1824. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  1825. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  1826. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  1827. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  1828. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  1829. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  1830. //auto - combat options
  1831. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  1832. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  1833. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  1834. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  1835. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  1836. //creature info
  1837. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  1838. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  1839. //general options
  1840. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  1841. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  1842. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  1843. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  1844. //texts printed
  1845. }
  1846. CBattleOptionsWindow::~CBattleOptionsWindow()
  1847. {
  1848. SDL_FreeSurface(background);
  1849. delete setToDefault;
  1850. delete exit;
  1851. delete viewGrid;
  1852. delete movementShadow;
  1853. delete animSpeeds;
  1854. delete mouseShadow;
  1855. }
  1856. void CBattleOptionsWindow::activate()
  1857. {
  1858. setToDefault->activate();
  1859. exit->activate();
  1860. viewGrid->activate();
  1861. movementShadow->activate();
  1862. animSpeeds->activate();
  1863. mouseShadow->activate();
  1864. }
  1865. void CBattleOptionsWindow::deactivate()
  1866. {
  1867. setToDefault->deactivate();
  1868. exit->deactivate();
  1869. viewGrid->deactivate();
  1870. movementShadow->deactivate();
  1871. animSpeeds->deactivate();
  1872. mouseShadow->deactivate();
  1873. }
  1874. void CBattleOptionsWindow::show(SDL_Surface *to)
  1875. {
  1876. if(!to) //"evaluating" to
  1877. to = screen;
  1878. SDL_BlitSurface(background, NULL, to, &pos);
  1879. setToDefault->show(to);
  1880. exit->show(to);
  1881. viewGrid->show(to);
  1882. movementShadow->show(to);
  1883. animSpeeds->show(to);
  1884. mouseShadow->show(to);
  1885. }
  1886. void CBattleOptionsWindow::bDefaultf()
  1887. {
  1888. }
  1889. void CBattleOptionsWindow::bExitf()
  1890. {
  1891. deactivate();
  1892. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  1893. {
  1894. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  1895. {
  1896. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  1897. break;
  1898. }
  1899. }
  1900. delete this;
  1901. LOCPLINT->curint->activate();
  1902. }