CBattleInterface.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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()!=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)
  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. default:
  943. tlog1<<"Critical Error! Wrong dest in stackAttacking!"<<std::endl;
  944. }
  945. }
  946. void CBattleInterface::newRound(int number)
  947. {
  948. console->addText(CGI->generaltexth->allTexts[412]);
  949. }
  950. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  951. {
  952. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  953. ba->actionType = action;
  954. ba->destinationTile = tile;
  955. ba->stackNumber = stack;
  956. ba->additionalInfo = additional;
  957. givenCommand->setn(ba);
  958. myTurn = false;
  959. activeStack = -1;
  960. }
  961. bool CBattleInterface::isTileAttackable(int number)
  962. {
  963. for(int b=0; b<shadedHexes.size(); ++b)
  964. {
  965. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  966. return true;
  967. }
  968. return false;
  969. }
  970. void CBattleInterface::hexLclicked(int whichOne)
  971. {
  972. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  973. {
  974. if(!myTurn)
  975. return; //we are not permit to do anything
  976. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  977. if(!dest || !dest->alive()) //no creature at that tile
  978. {
  979. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  980. giveCommand(2,whichOne,activeStack);
  981. }
  982. else if(dest->owner != attackingHeroInstance->tempOwner
  983. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)
  984. && BattleInfo::mutualPosition(LOCPLINT->cb->battleGetPos(activeStack),whichOne) < 0 ) //shooting
  985. {
  986. giveCommand(7,whichOne,activeStack);
  987. }
  988. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  989. {
  990. //std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  991. //for(int i=0;i<n.size();i++)
  992. //{
  993. // //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
  994. // if(vstd::contains(shadedHexes,n[i]))
  995. // {
  996. // giveCommand(6,n[i],activeStack,whichOne);
  997. // return;
  998. // }
  999. //}
  1000. switch(CGI->curh->number)
  1001. {
  1002. case 12:
  1003. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1004. break;
  1005. case 7:
  1006. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1007. break;
  1008. case 8:
  1009. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1010. break;
  1011. case 9:
  1012. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1013. break;
  1014. case 10:
  1015. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1016. break;
  1017. case 11:
  1018. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1019. break;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. void CBattleInterface::stackIsShooting(int ID, int dest)
  1025. {
  1026. if(attackingInfo != NULL)
  1027. {
  1028. return; //something went wrong
  1029. }
  1030. //projectile
  1031. float projectileAngle; //in radians; if positive, projectiles goes up
  1032. 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)
  1033. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1034. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1035. if(fromHex < dest)
  1036. projectileAngle = -projectileAngle;
  1037. SProjectileInfo spi;
  1038. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1039. spi.step = 0;
  1040. spi.frameNum = 0;
  1041. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1042. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1043. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1044. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1045. if(projectileAngle > straightAngle) //upper shot
  1046. {
  1047. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1048. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1049. }
  1050. else if(projectileAngle < -straightAngle) //lower shot
  1051. {
  1052. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1053. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1054. }
  1055. else //straight shot
  1056. {
  1057. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1058. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1059. }
  1060. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1061. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1062. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1063. //set starting frame
  1064. if(spi.spin)
  1065. {
  1066. spi.frameNum = 0;
  1067. }
  1068. else
  1069. {
  1070. 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);
  1071. }
  1072. //set delay
  1073. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1074. projectiles.push_back(spi);
  1075. //attack aniamtion
  1076. attackingInfo = new CAttHelper;
  1077. attackingInfo->dest = dest;
  1078. attackingInfo->frame = 0;
  1079. attackingInfo->hitCount = 0;
  1080. attackingInfo->ID = ID;
  1081. attackingInfo->reversing = false;
  1082. attackingInfo->shooting = true;
  1083. if(projectileAngle > straightAngle) //upper shot
  1084. attackingInfo->shootingGroup = 14;
  1085. else if(projectileAngle < -straightAngle) //lower shot
  1086. attackingInfo->shootingGroup = 16;
  1087. else //straight shot
  1088. attackingInfo->shootingGroup = 15;
  1089. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1090. }
  1091. void CBattleInterface::battleFinished(const BattleResult& br)
  1092. {
  1093. deactivate();
  1094. CGI->curh->changeGraphic(0,0);
  1095. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1096. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1097. resWindow->activate();
  1098. }
  1099. void CBattleInterface::setAnimSpeed(int set)
  1100. {
  1101. animSpeed = set;
  1102. }
  1103. int CBattleInterface::getAnimSpeed() const
  1104. {
  1105. return animSpeed;
  1106. }
  1107. float CBattleInterface::getAnimSpeedMultiplier() const
  1108. {
  1109. switch(animSpeed)
  1110. {
  1111. case 1:
  1112. return 3.5f;
  1113. case 2:
  1114. return 2.2f;
  1115. case 4:
  1116. return 1.0f;
  1117. default:
  1118. return 0.0f;
  1119. }
  1120. }
  1121. void CBattleInterface::attackingShowHelper()
  1122. {
  1123. if(attackingInfo && !attackingInfo->reversing)
  1124. {
  1125. if(attackingInfo->frame == 0)
  1126. {
  1127. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1128. if(attackingInfo->shooting)
  1129. {
  1130. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1131. }
  1132. else
  1133. {
  1134. if(aStack.creature->isDoubleWide())
  1135. {
  1136. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1137. {
  1138. case 0:
  1139. creAnims[attackingInfo->ID]->setType(11);
  1140. break;
  1141. case 1:
  1142. creAnims[attackingInfo->ID]->setType(11);
  1143. break;
  1144. case 2:
  1145. creAnims[attackingInfo->ID]->setType(12);
  1146. break;
  1147. case 3:
  1148. creAnims[attackingInfo->ID]->setType(13);
  1149. break;
  1150. case 4:
  1151. creAnims[attackingInfo->ID]->setType(13);
  1152. break;
  1153. case 5:
  1154. creAnims[attackingInfo->ID]->setType(12);
  1155. break;
  1156. }
  1157. }
  1158. else //else for if(aStack.creature->isDoubleWide())
  1159. {
  1160. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1161. {
  1162. case 0:
  1163. creAnims[attackingInfo->ID]->setType(11);
  1164. break;
  1165. case 1:
  1166. creAnims[attackingInfo->ID]->setType(11);
  1167. break;
  1168. case 2:
  1169. creAnims[attackingInfo->ID]->setType(12);
  1170. break;
  1171. case 3:
  1172. creAnims[attackingInfo->ID]->setType(13);
  1173. break;
  1174. case 4:
  1175. creAnims[attackingInfo->ID]->setType(13);
  1176. break;
  1177. case 5:
  1178. creAnims[attackingInfo->ID]->setType(12);
  1179. break;
  1180. }
  1181. }
  1182. }
  1183. }
  1184. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1185. {
  1186. attackingInfo->reversing = true;
  1187. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1188. if(aStackp == NULL)
  1189. return;
  1190. CStack aStack = *aStackp;
  1191. if(aStack.attackerOwned)
  1192. {
  1193. if(aStack.creature->isDoubleWide())
  1194. {
  1195. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1196. {
  1197. case 0:
  1198. //reverseCreature(ID, aStack.position, true);
  1199. break;
  1200. case 1:
  1201. break;
  1202. case 2:
  1203. break;
  1204. case 3:
  1205. break;
  1206. case 4:
  1207. //reverseCreature(ID, aStack.position, true);
  1208. break;
  1209. case 5:
  1210. reverseCreature(attackingInfo->ID, aStack.position, true);
  1211. break;
  1212. }
  1213. }
  1214. else //else for if(aStack.creature->isDoubleWide())
  1215. {
  1216. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1217. {
  1218. case 0:
  1219. reverseCreature(attackingInfo->ID, aStack.position, true);
  1220. break;
  1221. case 1:
  1222. break;
  1223. case 2:
  1224. break;
  1225. case 3:
  1226. break;
  1227. case 4:
  1228. reverseCreature(attackingInfo->ID, aStack.position, true);
  1229. break;
  1230. case 5:
  1231. reverseCreature(attackingInfo->ID, aStack.position, true);
  1232. break;
  1233. }
  1234. }
  1235. }
  1236. else //if(aStack.attackerOwned)
  1237. {
  1238. if(aStack.creature->isDoubleWide())
  1239. {
  1240. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1241. {
  1242. case 0:
  1243. //reverseCreature(ID, aStack.position, true);
  1244. break;
  1245. case 1:
  1246. break;
  1247. case 2:
  1248. reverseCreature(attackingInfo->ID, aStack.position, true);
  1249. break;
  1250. case 3:
  1251. break;
  1252. case 4:
  1253. //reverseCreature(ID, aStack.position, true);
  1254. break;
  1255. case 5:
  1256. //reverseCreature(ID, aStack.position, true);
  1257. break;
  1258. }
  1259. }
  1260. else //else for if(aStack.creature->isDoubleWide())
  1261. {
  1262. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1263. {
  1264. case 0:
  1265. //reverseCreature(ID, aStack.position, true);
  1266. break;
  1267. case 1:
  1268. reverseCreature(attackingInfo->ID, aStack.position, true);
  1269. break;
  1270. case 2:
  1271. reverseCreature(attackingInfo->ID, aStack.position, true);
  1272. break;
  1273. case 3:
  1274. reverseCreature(attackingInfo->ID, aStack.position, true);
  1275. break;
  1276. case 4:
  1277. //reverseCreature(ID, aStack.position, true);
  1278. break;
  1279. case 5:
  1280. //reverseCreature(ID, aStack.position, true);
  1281. break;
  1282. }
  1283. }
  1284. }
  1285. attackingInfo->reversing = false;
  1286. creAnims[attackingInfo->ID]->setType(2);
  1287. delete attackingInfo;
  1288. attackingInfo = NULL;
  1289. }
  1290. if(attackingInfo)
  1291. {
  1292. attackingInfo->hitCount++;
  1293. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1294. attackingInfo->frame++;
  1295. }
  1296. }
  1297. }
  1298. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1299. {
  1300. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1301. //preparating background graphic with hexes and shaded hexes
  1302. blitAt(background, 0, 0, backgroundWithHexes);
  1303. if(printCellBorders)
  1304. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1305. if(printStackRange)
  1306. {
  1307. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1308. {
  1309. int i = shadedHexes[m]/17; //row
  1310. int j = shadedHexes[m]%17-1; //column
  1311. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1312. int y = 86 + 42 * i;
  1313. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1314. }
  1315. }
  1316. }
  1317. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1318. {
  1319. char tabh[200];
  1320. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1321. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1322. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1323. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1324. dmg);
  1325. if(killed > 0)
  1326. {
  1327. if(killed > 1)
  1328. {
  1329. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1330. }
  1331. else //killed == 1
  1332. {
  1333. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1334. }
  1335. }
  1336. console->addText(std::string(tabh));
  1337. }
  1338. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1339. {
  1340. if(to == NULL)
  1341. to = screen;
  1342. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1343. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1344. {
  1345. if(it->animStartDelay>0)
  1346. {
  1347. --(it->animStartDelay);
  1348. continue;
  1349. }
  1350. SDL_Rect dst;
  1351. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1352. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1353. dst.x = it->x;
  1354. dst.y = it->y;
  1355. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1356. //actualizing projectile
  1357. ++it->step;
  1358. if(it->step == it->lastStep)
  1359. {
  1360. toBeDeleted.insert(toBeDeleted.end(), it);
  1361. }
  1362. else
  1363. {
  1364. it->x += it->dx;
  1365. it->y += it->dy;
  1366. if(it->spin)
  1367. {
  1368. ++(it->frameNum);
  1369. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1370. }
  1371. }
  1372. }
  1373. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1374. {
  1375. projectiles.erase(*it);
  1376. }
  1377. }
  1378. void CBattleHero::show(SDL_Surface *to)
  1379. {
  1380. //animation of flag
  1381. if(flip)
  1382. {
  1383. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1384. }
  1385. else
  1386. {
  1387. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1388. }
  1389. ++flagAnimCount;
  1390. if(flagAnimCount%4==0)
  1391. {
  1392. ++flagAnim;
  1393. flagAnim %= flag->ourImages.size();
  1394. }
  1395. //animation of hero
  1396. int tick=-1;
  1397. for(int i=0; i<dh->ourImages.size(); ++i)
  1398. {
  1399. if(dh->ourImages[i].groupNumber==phase)
  1400. ++tick;
  1401. if(tick==image)
  1402. {
  1403. SDL_Rect posb = pos;
  1404. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1405. ++image;
  1406. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1407. {
  1408. image = 0;
  1409. }
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. void CBattleHero::activate()
  1415. {
  1416. ClickableL::activate();
  1417. }
  1418. void CBattleHero::deactivate()
  1419. {
  1420. ClickableL::deactivate();
  1421. }
  1422. void CBattleHero::clickLeft(boost::logic::tribool down)
  1423. {
  1424. if(!down && myHero)
  1425. {
  1426. CGI->curh->changeGraphic(0,0);
  1427. LOCPLINT->curint->deactivate();
  1428. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1429. spellWindow->activate();
  1430. LOCPLINT->objsToBlit.push_back(spellWindow);
  1431. }
  1432. }
  1433. 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)
  1434. {
  1435. dh = CDefHandler::giveDef( defName );
  1436. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1437. {
  1438. if(flip)
  1439. {
  1440. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1441. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1442. dh->ourImages[i].bitmap = hlp;
  1443. }
  1444. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1445. }
  1446. dh->alphaTransformed = true;
  1447. if(flip)
  1448. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1449. else
  1450. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1451. //coloring flag and adding transparency
  1452. for(int i=0; i<flag->ourImages.size(); ++i)
  1453. {
  1454. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1455. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1456. }
  1457. }
  1458. CBattleHero::~CBattleHero()
  1459. {
  1460. delete dh;
  1461. delete flag;
  1462. }
  1463. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1464. {
  1465. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1466. ret.second = -139 + 42 * (hexNum/17); //counting y
  1467. //counting x
  1468. if(attacker)
  1469. {
  1470. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1471. }
  1472. else
  1473. {
  1474. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1475. }
  1476. //shifting position for double - hex creatures
  1477. if(creature->isDoubleWide())
  1478. {
  1479. if(attacker)
  1480. {
  1481. ret.first -= 42;
  1482. }
  1483. else
  1484. {
  1485. ret.first += 42;
  1486. }
  1487. }
  1488. //returning
  1489. return ret;
  1490. }
  1491. void CBattleHex::activate()
  1492. {
  1493. Hoverable::activate();
  1494. MotionInterested::activate();
  1495. ClickableL::activate();
  1496. ClickableR::activate();
  1497. }
  1498. void CBattleHex::deactivate()
  1499. {
  1500. Hoverable::deactivate();
  1501. MotionInterested::deactivate();
  1502. ClickableL::deactivate();
  1503. ClickableR::deactivate();
  1504. }
  1505. void CBattleHex::hover(bool on)
  1506. {
  1507. hovered = on;
  1508. Hoverable::hover(on);
  1509. if(!on && setAlterText)
  1510. {
  1511. myInterface->console->alterTxt = std::string();
  1512. setAlterText = false;
  1513. }
  1514. }
  1515. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1516. {
  1517. }
  1518. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1519. {
  1520. if(myInterface->cellShade)
  1521. {
  1522. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1523. {
  1524. strictHovered = false;
  1525. }
  1526. else //hovered pixel is inside hex
  1527. {
  1528. strictHovered = true;
  1529. }
  1530. }
  1531. if(hovered && strictHovered) //print attacked creature to console
  1532. {
  1533. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1534. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1535. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1536. {
  1537. char tabh[160];
  1538. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1539. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1540. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1541. myInterface->console->alterTxt = std::string(tabh);
  1542. setAlterText = true;
  1543. }
  1544. }
  1545. else if(setAlterText)
  1546. {
  1547. myInterface->console->alterTxt = std::string();
  1548. setAlterText = false;
  1549. }
  1550. }
  1551. void CBattleHex::clickLeft(boost::logic::tribool down)
  1552. {
  1553. if(!down && hovered && strictHovered) //we've been really clicked!
  1554. {
  1555. myInterface->hexLclicked(myNumber);
  1556. }
  1557. }
  1558. void CBattleHex::clickRight(boost::logic::tribool down)
  1559. {
  1560. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1561. if(hovered && strictHovered && stID!=-1)
  1562. {
  1563. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1564. if(!myst.alive()) return;
  1565. StackState *pom = NULL;
  1566. if(down)
  1567. {
  1568. pom = new StackState();
  1569. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1570. if(h)
  1571. {
  1572. pom->attackBonus = h->primSkills[0];
  1573. pom->defenseBonus = h->primSkills[1];
  1574. pom->luck = h->getCurrentLuck();
  1575. pom->morale = h->getCurrentMorale();
  1576. pom->currentHealth = myst.firstHPleft;
  1577. }
  1578. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1579. ->activate();
  1580. }
  1581. delete pom;
  1582. }
  1583. }
  1584. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1585. {
  1586. }
  1587. CBattleConsole::~CBattleConsole()
  1588. {
  1589. texts.clear();
  1590. }
  1591. void CBattleConsole::show(SDL_Surface * to)
  1592. {
  1593. if(alterTxt.size())
  1594. {
  1595. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1596. }
  1597. else if(texts.size())
  1598. {
  1599. if(texts.size()==1)
  1600. {
  1601. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1602. }
  1603. else
  1604. {
  1605. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1606. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1607. }
  1608. }
  1609. }
  1610. bool CBattleConsole::addText(const std::string & text)
  1611. {
  1612. if(text.size()>70)
  1613. return false; //text too long!
  1614. int firstInToken = 0;
  1615. for(int i=0; i<text.size(); ++i) //tokenize
  1616. {
  1617. if(text[i] == 10)
  1618. {
  1619. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1620. firstInToken = i+1;
  1621. }
  1622. }
  1623. texts.push_back( text.substr(firstInToken, text.size()) );
  1624. lastShown = texts.size()-1;
  1625. return true;
  1626. }
  1627. void CBattleConsole::eraseText(unsigned int pos)
  1628. {
  1629. if(pos < texts.size())
  1630. {
  1631. texts.erase(texts.begin() + pos);
  1632. if(lastShown == texts.size())
  1633. --lastShown;
  1634. }
  1635. }
  1636. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  1637. {
  1638. if(pos >= texts.size()) //no such pos
  1639. return;
  1640. texts[pos] = text;
  1641. }
  1642. void CBattleConsole::scrollUp(unsigned int by)
  1643. {
  1644. if(lastShown > by)
  1645. lastShown -= by;
  1646. }
  1647. void CBattleConsole::scrollDown(unsigned int by)
  1648. {
  1649. if(lastShown + by < texts.size())
  1650. lastShown += by;
  1651. }
  1652. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1653. {
  1654. this->pos = pos;
  1655. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1656. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1657. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1658. SDL_FreeSurface(background);
  1659. background = pom;
  1660. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1661. if(br.winner==0) //attacker won
  1662. {
  1663. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1664. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1665. }
  1666. else //if(br.winner==1)
  1667. {
  1668. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1669. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1670. }
  1671. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1672. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1673. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1674. std::string attackerName, defenderName;
  1675. if(owner->attackingHeroInstance) //a hero attacked
  1676. {
  1677. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1678. //setting attackerName
  1679. attackerName = owner->attackingHeroInstance->name;
  1680. }
  1681. else //a monster attacked
  1682. {
  1683. int bestMonsterID = -1;
  1684. int bestPower = 0;
  1685. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1686. {
  1687. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1688. {
  1689. bestPower = CGI->creh->creatures[it->first].AIValue;
  1690. bestMonsterID = it->first;
  1691. }
  1692. }
  1693. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1694. //setting attackerName
  1695. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1696. }
  1697. if(owner->defendingHeroInstance) //a hero defended
  1698. {
  1699. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1700. //setting defenderName
  1701. defenderName = owner->defendingHeroInstance->name;
  1702. }
  1703. else //a monster defended
  1704. {
  1705. int bestMonsterID = -1;
  1706. int bestPower = 0;
  1707. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1708. {
  1709. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1710. {
  1711. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1712. bestMonsterID = it->second.first;
  1713. }
  1714. }
  1715. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1716. //setting defenderName
  1717. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1718. }
  1719. //printing attacker and defender's names
  1720. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1721. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1722. //printing casualities
  1723. for(int step = 0; step < 2; ++step)
  1724. {
  1725. if(br.casualties[step].size()==0)
  1726. {
  1727. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  1728. }
  1729. else
  1730. {
  1731. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  1732. int yPos = 344 + step*97;
  1733. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  1734. {
  1735. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1736. std::stringstream amount;
  1737. amount<<it->second;
  1738. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1739. xPos += 42;
  1740. }
  1741. }
  1742. }
  1743. //printing result description
  1744. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  1745. switch(br.result)
  1746. {
  1747. case 0: //normal victory
  1748. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1749. {
  1750. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  1751. }
  1752. else
  1753. {
  1754. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  1755. }
  1756. break;
  1757. case 1: //flee
  1758. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1759. {
  1760. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  1761. }
  1762. else
  1763. {
  1764. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  1765. }
  1766. break;
  1767. case 2: //surrender
  1768. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1769. {
  1770. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  1771. }
  1772. else
  1773. {
  1774. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  1775. }
  1776. break;
  1777. }
  1778. }
  1779. CBattleReslutWindow::~CBattleReslutWindow()
  1780. {
  1781. SDL_FreeSurface(background);
  1782. }
  1783. void CBattleReslutWindow::activate()
  1784. {
  1785. LOCPLINT->showingDialog->set(true);
  1786. exit->activate();
  1787. }
  1788. void CBattleReslutWindow::deactivate()
  1789. {
  1790. exit->deactivate();
  1791. }
  1792. void CBattleReslutWindow::show(SDL_Surface *to)
  1793. {
  1794. //evaluating to
  1795. if(!to)
  1796. to = screen;
  1797. SDL_BlitSurface(background, NULL, to, &pos);
  1798. exit->show(to);
  1799. }
  1800. void CBattleReslutWindow::bExitf()
  1801. {
  1802. LOCPLINT->battleResultQuited();
  1803. }
  1804. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  1805. {
  1806. pos = position;
  1807. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  1808. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1809. 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);
  1810. viewGrid->select(owner->printCellBorders);
  1811. 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);
  1812. movementShadow->select(owner->printStackRange);
  1813. 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);
  1814. mouseShadow->select(owner->printMouseShadow);
  1815. animSpeeds = new CHighlightableButtonsGroup(0);
  1816. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[422].first),CGI->preth->zelp[422].second, "sysopb9.def",188, 309, 1);
  1817. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[423].first),CGI->preth->zelp[423].second, "sysob10.def",252, 309, 2);
  1818. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->preth->zelp[424].first),CGI->preth->zelp[424].second, "sysob11.def",315, 309, 4);
  1819. animSpeeds->select(owner->getAnimSpeed(), 1);
  1820. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  1821. 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);
  1822. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  1823. 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);
  1824. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  1825. //printing texts to background
  1826. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  1827. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  1828. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  1829. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  1830. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  1831. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  1832. //auto - combat options
  1833. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  1834. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  1835. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  1836. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  1837. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  1838. //creature info
  1839. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  1840. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  1841. //general options
  1842. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  1843. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  1844. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  1845. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  1846. //texts printed
  1847. }
  1848. CBattleOptionsWindow::~CBattleOptionsWindow()
  1849. {
  1850. SDL_FreeSurface(background);
  1851. delete setToDefault;
  1852. delete exit;
  1853. delete viewGrid;
  1854. delete movementShadow;
  1855. delete animSpeeds;
  1856. delete mouseShadow;
  1857. }
  1858. void CBattleOptionsWindow::activate()
  1859. {
  1860. setToDefault->activate();
  1861. exit->activate();
  1862. viewGrid->activate();
  1863. movementShadow->activate();
  1864. animSpeeds->activate();
  1865. mouseShadow->activate();
  1866. }
  1867. void CBattleOptionsWindow::deactivate()
  1868. {
  1869. setToDefault->deactivate();
  1870. exit->deactivate();
  1871. viewGrid->deactivate();
  1872. movementShadow->deactivate();
  1873. animSpeeds->deactivate();
  1874. mouseShadow->deactivate();
  1875. }
  1876. void CBattleOptionsWindow::show(SDL_Surface *to)
  1877. {
  1878. if(!to) //"evaluating" to
  1879. to = screen;
  1880. SDL_BlitSurface(background, NULL, to, &pos);
  1881. setToDefault->show(to);
  1882. exit->show(to);
  1883. viewGrid->show(to);
  1884. movementShadow->show(to);
  1885. animSpeeds->show(to);
  1886. mouseShadow->show(to);
  1887. }
  1888. void CBattleOptionsWindow::bDefaultf()
  1889. {
  1890. }
  1891. void CBattleOptionsWindow::bExitf()
  1892. {
  1893. deactivate();
  1894. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  1895. {
  1896. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  1897. {
  1898. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  1899. break;
  1900. }
  1901. }
  1902. delete this;
  1903. LOCPLINT->curint->activate();
  1904. }