CBattleInterface.cpp 63 KB

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