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 startMoving, bool endMoving)
  655. {
  656. //a few useful variables
  657. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  658. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  659. int hexWbase = 44, hexHbase = 42;
  660. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  661. if(startMoving && creAnims[number]->framesInGroup(20)!=0) //animation of starting move; some units don't have this animation (ie. halberdier)
  662. {
  663. deactivate();
  664. CGI->curh->hide();
  665. creAnims[number]->setType(20);
  666. //LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  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. }
  750. creAnims[number]->setType(2); //resetting to default
  751. activate();
  752. CGI->curh->show();
  753. }
  754. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  755. if(endMoving) //resetting to default
  756. {
  757. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  758. reverseCreature(number, destHex, twoTiles);
  759. }
  760. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  761. creAnims[number]->pos.x = coords.first;
  762. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  763. creAnims[number]->pos.x -= 44;
  764. creAnims[number]->pos.y = coords.second;
  765. }
  766. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  767. {
  768. while(creAnims[ID]->getType() != 2 || (attackingInfo && attackingInfo->IDby == IDby))
  769. {
  770. show();
  771. CSDL_Ext::update();
  772. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  773. }
  774. if(byShooting) //delay hit animation
  775. {
  776. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  777. while(true)
  778. {
  779. bool found = false;
  780. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  781. {
  782. if(it->creID == attacker.creature->idNumber)
  783. {
  784. found = true;
  785. break;
  786. }
  787. }
  788. if(!found)
  789. break;
  790. else
  791. {
  792. show();
  793. CSDL_Ext::update();
  794. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  795. }
  796. }
  797. }
  798. creAnims[ID]->setType(3); //getting hit
  799. int firstFrame = creAnims[ID]->getFrame();
  800. for(int i=0; creAnims[ID]->getFrame() != creAnims[ID]->framesInGroup(3) + firstFrame - 1; ++i)
  801. {
  802. show();
  803. CSDL_Ext::update();
  804. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  805. /*if((animCount+1)%(4/animSpeed)==0)
  806. creAnims[ID]->incrementFrame();*/
  807. }
  808. creAnims[ID]->setType(2);
  809. printConsoleAttacked(ID, dmg, killed, IDby);
  810. }
  811. void CBattleInterface::stackAttacking(int ID, int dest)
  812. {
  813. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  814. {
  815. show();
  816. CSDL_Ext::update();
  817. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  818. }
  819. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  820. if(aStack.attackerOwned)
  821. {
  822. if(aStack.creature->isDoubleWide())
  823. {
  824. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  825. {
  826. case 0:
  827. //reverseCreature(ID, aStack.position, true);
  828. break;
  829. case 1:
  830. break;
  831. case 2:
  832. break;
  833. case 3:
  834. break;
  835. case 4:
  836. //reverseCreature(ID, aStack.position, true);
  837. break;
  838. case 5:
  839. reverseCreature(ID, aStack.position, true);
  840. break;
  841. }
  842. }
  843. else //else for if(aStack.creature->isDoubleWide())
  844. {
  845. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  846. {
  847. case 0:
  848. reverseCreature(ID, aStack.position, true);
  849. break;
  850. case 1:
  851. break;
  852. case 2:
  853. break;
  854. case 3:
  855. break;
  856. case 4:
  857. reverseCreature(ID, aStack.position, true);
  858. break;
  859. case 5:
  860. reverseCreature(ID, aStack.position, true);
  861. break;
  862. }
  863. }
  864. }
  865. else //if(aStack.attackerOwned)
  866. {
  867. if(aStack.creature->isDoubleWide())
  868. {
  869. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  870. {
  871. case 0:
  872. //reverseCreature(ID, aStack.position, true);
  873. break;
  874. case 1:
  875. break;
  876. case 2:
  877. reverseCreature(ID, aStack.position, true);
  878. break;
  879. case 3:
  880. break;
  881. case 4:
  882. //reverseCreature(ID, aStack.position, true);
  883. break;
  884. case 5:
  885. //reverseCreature(ID, aStack.position, true);
  886. break;
  887. }
  888. }
  889. else //else for if(aStack.creature->isDoubleWide())
  890. {
  891. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  892. {
  893. case 0:
  894. //reverseCreature(ID, aStack.position, true);
  895. break;
  896. case 1:
  897. reverseCreature(ID, aStack.position, true);
  898. break;
  899. case 2:
  900. reverseCreature(ID, aStack.position, true);
  901. break;
  902. case 3:
  903. reverseCreature(ID, aStack.position, true);
  904. break;
  905. case 4:
  906. //reverseCreature(ID, aStack.position, true);
  907. break;
  908. case 5:
  909. //reverseCreature(ID, aStack.position, true);
  910. break;
  911. }
  912. }
  913. }
  914. attackingInfo = new CAttHelper;
  915. attackingInfo->dest = dest;
  916. attackingInfo->frame = 0;
  917. attackingInfo->hitCount = 0;
  918. attackingInfo->ID = ID;
  919. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  920. attackingInfo->reversing = false;
  921. attackingInfo->shooting = false;
  922. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  923. {
  924. case 0:
  925. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  926. break;
  927. case 1:
  928. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  929. break;
  930. case 2:
  931. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  932. break;
  933. case 3:
  934. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  935. break;
  936. case 4:
  937. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  938. break;
  939. case 5:
  940. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  941. break;
  942. }
  943. }
  944. void CBattleInterface::newRound(int number)
  945. {
  946. console->addText(CGI->generaltexth->allTexts[412]);
  947. }
  948. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  949. {
  950. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  951. ba->actionType = action;
  952. ba->destinationTile = tile;
  953. ba->stackNumber = stack;
  954. ba->additionalInfo = additional;
  955. givenCommand->setn(ba);
  956. myTurn = false;
  957. activeStack = -1;
  958. }
  959. bool CBattleInterface::isTileAttackable(int number)
  960. {
  961. for(int b=0; b<shadedHexes.size(); ++b)
  962. {
  963. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  964. return true;
  965. }
  966. return false;
  967. }
  968. void CBattleInterface::hexLclicked(int whichOne)
  969. {
  970. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  971. {
  972. if(!myTurn)
  973. return; //we are not permit to do anything
  974. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  975. if(!dest || !dest->alive()) //no creature at that tile
  976. {
  977. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  978. giveCommand(2,whichOne,activeStack);
  979. }
  980. else if(dest->owner != attackingHeroInstance->tempOwner
  981. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)
  982. && BattleInfo::mutualPosition(LOCPLINT->cb->battleGetPos(activeStack),whichOne) < 0 ) //shooting
  983. {
  984. giveCommand(7,whichOne,activeStack);
  985. }
  986. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  987. {
  988. //std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  989. //for(int i=0;i<n.size();i++)
  990. //{
  991. // //TODO: now we are using first available tile, but in the future we should add possibility of choosing from which tile we want to attack
  992. // if(vstd::contains(shadedHexes,n[i]))
  993. // {
  994. // giveCommand(6,n[i],activeStack,whichOne);
  995. // return;
  996. // }
  997. //}
  998. switch(CGI->curh->number)
  999. {
  1000. case 12:
  1001. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1002. break;
  1003. case 7:
  1004. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1005. break;
  1006. case 8:
  1007. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1008. break;
  1009. case 9:
  1010. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1011. break;
  1012. case 10:
  1013. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1014. break;
  1015. case 11:
  1016. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. void CBattleInterface::stackIsShooting(int ID, int dest)
  1023. {
  1024. if(attackingInfo != NULL)
  1025. {
  1026. return; //something went wrong
  1027. }
  1028. //projectile
  1029. float projectileAngle; //in radians; if positive, projectiles goes up
  1030. float straightAngle = 0.2f; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  1031. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1032. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1033. if(fromHex < dest)
  1034. projectileAngle = -projectileAngle;
  1035. SProjectileInfo spi;
  1036. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1037. spi.step = 0;
  1038. spi.frameNum = 0;
  1039. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1040. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1041. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1042. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1043. if(projectileAngle > straightAngle) //upper shot
  1044. {
  1045. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1046. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1047. }
  1048. else if(projectileAngle < -straightAngle) //lower shot
  1049. {
  1050. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1051. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1052. }
  1053. else //straight shot
  1054. {
  1055. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1056. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1057. }
  1058. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1059. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1060. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1061. //set starting frame
  1062. if(spi.spin)
  1063. {
  1064. spi.frameNum = 0;
  1065. }
  1066. else
  1067. {
  1068. spi.frameNum = ((M_PI/2.0f - projectileAngle) / (2.0f *M_PI) + 1/((float)(2*(idToProjectile[spi.creID]->ourImages.size()-1)))) * (idToProjectile[spi.creID]->ourImages.size()-1);
  1069. }
  1070. //set delay
  1071. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1072. projectiles.push_back(spi);
  1073. //attack aniamtion
  1074. attackingInfo = new CAttHelper;
  1075. attackingInfo->dest = dest;
  1076. attackingInfo->frame = 0;
  1077. attackingInfo->hitCount = 0;
  1078. attackingInfo->ID = ID;
  1079. attackingInfo->reversing = false;
  1080. attackingInfo->shooting = true;
  1081. if(projectileAngle > straightAngle) //upper shot
  1082. attackingInfo->shootingGroup = 14;
  1083. else if(projectileAngle < -straightAngle) //lower shot
  1084. attackingInfo->shootingGroup = 16;
  1085. else //straight shot
  1086. attackingInfo->shootingGroup = 15;
  1087. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1088. }
  1089. void CBattleInterface::battleFinished(const BattleResult& br)
  1090. {
  1091. deactivate();
  1092. CGI->curh->changeGraphic(0,0);
  1093. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1094. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1095. resWindow->activate();
  1096. }
  1097. void CBattleInterface::setAnimSpeed(int set)
  1098. {
  1099. animSpeed = set;
  1100. }
  1101. int CBattleInterface::getAnimSpeed() const
  1102. {
  1103. return animSpeed;
  1104. }
  1105. float CBattleInterface::getAnimSpeedMultiplier() const
  1106. {
  1107. switch(animSpeed)
  1108. {
  1109. case 1:
  1110. return 3.5f;
  1111. case 2:
  1112. return 2.2f;
  1113. case 4:
  1114. return 1.0f;
  1115. default:
  1116. return 0.0f;
  1117. }
  1118. }
  1119. void CBattleInterface::attackingShowHelper()
  1120. {
  1121. if(attackingInfo && !attackingInfo->reversing)
  1122. {
  1123. if(attackingInfo->frame == 0)
  1124. {
  1125. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1126. if(attackingInfo->shooting)
  1127. {
  1128. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1129. }
  1130. else
  1131. {
  1132. if(aStack.creature->isDoubleWide())
  1133. {
  1134. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1135. {
  1136. case 0:
  1137. creAnims[attackingInfo->ID]->setType(11);
  1138. break;
  1139. case 1:
  1140. creAnims[attackingInfo->ID]->setType(11);
  1141. break;
  1142. case 2:
  1143. creAnims[attackingInfo->ID]->setType(12);
  1144. break;
  1145. case 3:
  1146. creAnims[attackingInfo->ID]->setType(13);
  1147. break;
  1148. case 4:
  1149. creAnims[attackingInfo->ID]->setType(13);
  1150. break;
  1151. case 5:
  1152. creAnims[attackingInfo->ID]->setType(12);
  1153. break;
  1154. }
  1155. }
  1156. else //else for if(aStack.creature->isDoubleWide())
  1157. {
  1158. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1159. {
  1160. case 0:
  1161. creAnims[attackingInfo->ID]->setType(11);
  1162. break;
  1163. case 1:
  1164. creAnims[attackingInfo->ID]->setType(11);
  1165. break;
  1166. case 2:
  1167. creAnims[attackingInfo->ID]->setType(12);
  1168. break;
  1169. case 3:
  1170. creAnims[attackingInfo->ID]->setType(13);
  1171. break;
  1172. case 4:
  1173. creAnims[attackingInfo->ID]->setType(13);
  1174. break;
  1175. case 5:
  1176. creAnims[attackingInfo->ID]->setType(12);
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. }
  1182. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1183. {
  1184. attackingInfo->reversing = true;
  1185. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1186. if(aStackp == NULL)
  1187. return;
  1188. CStack aStack = *aStackp;
  1189. if(aStack.attackerOwned)
  1190. {
  1191. if(aStack.creature->isDoubleWide())
  1192. {
  1193. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1194. {
  1195. case 0:
  1196. //reverseCreature(ID, aStack.position, true);
  1197. break;
  1198. case 1:
  1199. break;
  1200. case 2:
  1201. break;
  1202. case 3:
  1203. break;
  1204. case 4:
  1205. //reverseCreature(ID, aStack.position, true);
  1206. break;
  1207. case 5:
  1208. reverseCreature(attackingInfo->ID, aStack.position, true);
  1209. break;
  1210. }
  1211. }
  1212. else //else for if(aStack.creature->isDoubleWide())
  1213. {
  1214. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1215. {
  1216. case 0:
  1217. reverseCreature(attackingInfo->ID, aStack.position, true);
  1218. break;
  1219. case 1:
  1220. break;
  1221. case 2:
  1222. break;
  1223. case 3:
  1224. break;
  1225. case 4:
  1226. reverseCreature(attackingInfo->ID, aStack.position, true);
  1227. break;
  1228. case 5:
  1229. reverseCreature(attackingInfo->ID, aStack.position, true);
  1230. break;
  1231. }
  1232. }
  1233. }
  1234. else //if(aStack.attackerOwned)
  1235. {
  1236. if(aStack.creature->isDoubleWide())
  1237. {
  1238. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1239. {
  1240. case 0:
  1241. //reverseCreature(ID, aStack.position, true);
  1242. break;
  1243. case 1:
  1244. break;
  1245. case 2:
  1246. reverseCreature(attackingInfo->ID, aStack.position, true);
  1247. break;
  1248. case 3:
  1249. break;
  1250. case 4:
  1251. //reverseCreature(ID, aStack.position, true);
  1252. break;
  1253. case 5:
  1254. //reverseCreature(ID, aStack.position, true);
  1255. break;
  1256. }
  1257. }
  1258. else //else for if(aStack.creature->isDoubleWide())
  1259. {
  1260. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1261. {
  1262. case 0:
  1263. //reverseCreature(ID, aStack.position, true);
  1264. break;
  1265. case 1:
  1266. reverseCreature(attackingInfo->ID, aStack.position, true);
  1267. break;
  1268. case 2:
  1269. reverseCreature(attackingInfo->ID, aStack.position, true);
  1270. break;
  1271. case 3:
  1272. reverseCreature(attackingInfo->ID, aStack.position, true);
  1273. break;
  1274. case 4:
  1275. //reverseCreature(ID, aStack.position, true);
  1276. break;
  1277. case 5:
  1278. //reverseCreature(ID, aStack.position, true);
  1279. break;
  1280. }
  1281. }
  1282. }
  1283. attackingInfo->reversing = false;
  1284. creAnims[attackingInfo->ID]->setType(2);
  1285. delete attackingInfo;
  1286. attackingInfo = NULL;
  1287. }
  1288. if(attackingInfo)
  1289. {
  1290. attackingInfo->hitCount++;
  1291. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1292. attackingInfo->frame++;
  1293. }
  1294. }
  1295. }
  1296. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1297. {
  1298. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1299. //preparating background graphic with hexes and shaded hexes
  1300. blitAt(background, 0, 0, backgroundWithHexes);
  1301. if(printCellBorders)
  1302. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1303. if(printStackRange)
  1304. {
  1305. for(int m=0; m<shadedHexes.size(); ++m) //rows
  1306. {
  1307. int i = shadedHexes[m]/17; //row
  1308. int j = shadedHexes[m]%17-1; //column
  1309. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1310. int y = 86 + 42 * i;
  1311. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1312. }
  1313. }
  1314. }
  1315. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1316. {
  1317. char tabh[200];
  1318. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1319. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1320. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1321. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1322. dmg);
  1323. if(killed > 0)
  1324. {
  1325. if(killed > 1)
  1326. {
  1327. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1328. }
  1329. else //killed == 1
  1330. {
  1331. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1332. }
  1333. }
  1334. console->addText(std::string(tabh));
  1335. }
  1336. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1337. {
  1338. if(to == NULL)
  1339. to = screen;
  1340. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1341. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1342. {
  1343. if(it->animStartDelay>0)
  1344. {
  1345. --(it->animStartDelay);
  1346. continue;
  1347. }
  1348. SDL_Rect dst;
  1349. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1350. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1351. dst.x = it->x;
  1352. dst.y = it->y;
  1353. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1354. //actualizing projectile
  1355. ++it->step;
  1356. if(it->step == it->lastStep)
  1357. {
  1358. toBeDeleted.insert(toBeDeleted.end(), it);
  1359. }
  1360. else
  1361. {
  1362. it->x += it->dx;
  1363. it->y += it->dy;
  1364. if(it->spin)
  1365. {
  1366. ++(it->frameNum);
  1367. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1368. }
  1369. }
  1370. }
  1371. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1372. {
  1373. projectiles.erase(*it);
  1374. }
  1375. }
  1376. void CBattleHero::show(SDL_Surface *to)
  1377. {
  1378. //animation of flag
  1379. if(flip)
  1380. {
  1381. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  1382. }
  1383. else
  1384. {
  1385. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  1386. }
  1387. ++flagAnimCount;
  1388. if(flagAnimCount%4==0)
  1389. {
  1390. ++flagAnim;
  1391. flagAnim %= flag->ourImages.size();
  1392. }
  1393. //animation of hero
  1394. int tick=-1;
  1395. for(int i=0; i<dh->ourImages.size(); ++i)
  1396. {
  1397. if(dh->ourImages[i].groupNumber==phase)
  1398. ++tick;
  1399. if(tick==image)
  1400. {
  1401. SDL_Rect posb = pos;
  1402. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1403. ++image;
  1404. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  1405. {
  1406. image = 0;
  1407. }
  1408. break;
  1409. }
  1410. }
  1411. }
  1412. void CBattleHero::activate()
  1413. {
  1414. ClickableL::activate();
  1415. }
  1416. void CBattleHero::deactivate()
  1417. {
  1418. ClickableL::deactivate();
  1419. }
  1420. void CBattleHero::clickLeft(boost::logic::tribool down)
  1421. {
  1422. if(!down && myHero)
  1423. {
  1424. CGI->curh->changeGraphic(0,0);
  1425. LOCPLINT->curint->deactivate();
  1426. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, 90, 2), myHero);
  1427. spellWindow->activate();
  1428. LOCPLINT->objsToBlit.push_back(spellWindow);
  1429. }
  1430. }
  1431. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, unsigned char player, const CGHeroInstance * hero): phase(phaseG), image(imageG), flip(flipG), flagAnim(0), myHero(hero)
  1432. {
  1433. dh = CDefHandler::giveDef( defName );
  1434. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1435. {
  1436. if(flip)
  1437. {
  1438. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1439. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1440. dh->ourImages[i].bitmap = hlp;
  1441. }
  1442. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1443. }
  1444. dh->alphaTransformed = true;
  1445. if(flip)
  1446. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1447. else
  1448. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1449. //coloring flag and adding transparency
  1450. for(int i=0; i<flag->ourImages.size(); ++i)
  1451. {
  1452. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1453. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1454. }
  1455. }
  1456. CBattleHero::~CBattleHero()
  1457. {
  1458. delete dh;
  1459. delete flag;
  1460. }
  1461. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1462. {
  1463. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1464. ret.second = -139 + 42 * (hexNum/17); //counting y
  1465. //counting x
  1466. if(attacker)
  1467. {
  1468. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1469. }
  1470. else
  1471. {
  1472. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1473. }
  1474. //shifting position for double - hex creatures
  1475. if(creature->isDoubleWide())
  1476. {
  1477. if(attacker)
  1478. {
  1479. ret.first -= 42;
  1480. }
  1481. else
  1482. {
  1483. ret.first += 42;
  1484. }
  1485. }
  1486. //returning
  1487. return ret;
  1488. }
  1489. void CBattleHex::activate()
  1490. {
  1491. Hoverable::activate();
  1492. MotionInterested::activate();
  1493. ClickableL::activate();
  1494. ClickableR::activate();
  1495. }
  1496. void CBattleHex::deactivate()
  1497. {
  1498. Hoverable::deactivate();
  1499. MotionInterested::deactivate();
  1500. ClickableL::deactivate();
  1501. ClickableR::deactivate();
  1502. }
  1503. void CBattleHex::hover(bool on)
  1504. {
  1505. hovered = on;
  1506. Hoverable::hover(on);
  1507. if(!on && setAlterText)
  1508. {
  1509. myInterface->console->alterTxt = std::string();
  1510. setAlterText = false;
  1511. }
  1512. }
  1513. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1514. {
  1515. }
  1516. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1517. {
  1518. if(myInterface->cellShade)
  1519. {
  1520. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1521. {
  1522. strictHovered = false;
  1523. }
  1524. else //hovered pixel is inside hex
  1525. {
  1526. strictHovered = true;
  1527. }
  1528. }
  1529. if(hovered && strictHovered) //print attacked creature to console
  1530. {
  1531. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1532. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1533. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  1534. {
  1535. char tabh[160];
  1536. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1537. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1538. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1539. myInterface->console->alterTxt = std::string(tabh);
  1540. setAlterText = true;
  1541. }
  1542. }
  1543. else if(setAlterText)
  1544. {
  1545. myInterface->console->alterTxt = std::string();
  1546. setAlterText = false;
  1547. }
  1548. }
  1549. void CBattleHex::clickLeft(boost::logic::tribool down)
  1550. {
  1551. if(!down && hovered && strictHovered) //we've been really clicked!
  1552. {
  1553. myInterface->hexLclicked(myNumber);
  1554. }
  1555. }
  1556. void CBattleHex::clickRight(boost::logic::tribool down)
  1557. {
  1558. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1559. if(hovered && strictHovered && stID!=-1)
  1560. {
  1561. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1562. if(!myst.alive()) return;
  1563. StackState *pom = NULL;
  1564. if(down)
  1565. {
  1566. pom = new StackState();
  1567. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1568. if(h)
  1569. {
  1570. pom->attackBonus = h->primSkills[0];
  1571. pom->defenseBonus = h->primSkills[1];
  1572. pom->luck = h->getCurrentLuck();
  1573. pom->morale = h->getCurrentMorale();
  1574. pom->currentHealth = myst.firstHPleft;
  1575. }
  1576. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1577. ->activate();
  1578. }
  1579. delete pom;
  1580. }
  1581. }
  1582. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1583. {
  1584. }
  1585. CBattleConsole::~CBattleConsole()
  1586. {
  1587. texts.clear();
  1588. }
  1589. void CBattleConsole::show(SDL_Surface * to)
  1590. {
  1591. if(alterTxt.size())
  1592. {
  1593. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1594. }
  1595. else if(texts.size())
  1596. {
  1597. if(texts.size()==1)
  1598. {
  1599. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1600. }
  1601. else
  1602. {
  1603. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1604. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1605. }
  1606. }
  1607. }
  1608. bool CBattleConsole::addText(const std::string & text)
  1609. {
  1610. if(text.size()>70)
  1611. return false; //text too long!
  1612. int firstInToken = 0;
  1613. for(int i=0; i<text.size(); ++i) //tokenize
  1614. {
  1615. if(text[i] == 10)
  1616. {
  1617. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1618. firstInToken = i+1;
  1619. }
  1620. }
  1621. texts.push_back( text.substr(firstInToken, text.size()) );
  1622. lastShown = texts.size()-1;
  1623. return true;
  1624. }
  1625. void CBattleConsole::eraseText(unsigned int pos)
  1626. {
  1627. if(pos < texts.size())
  1628. {
  1629. texts.erase(texts.begin() + pos);
  1630. if(lastShown == texts.size())
  1631. --lastShown;
  1632. }
  1633. }
  1634. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  1635. {
  1636. if(pos >= texts.size()) //no such pos
  1637. return;
  1638. texts[pos] = text;
  1639. }
  1640. void CBattleConsole::scrollUp(unsigned int by)
  1641. {
  1642. if(lastShown > by)
  1643. lastShown -= by;
  1644. }
  1645. void CBattleConsole::scrollDown(unsigned int by)
  1646. {
  1647. if(lastShown + by < texts.size())
  1648. lastShown += by;
  1649. }
  1650. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  1651. {
  1652. this->pos = pos;
  1653. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  1654. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  1655. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  1656. SDL_FreeSurface(background);
  1657. background = pom;
  1658. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", false, NULL, false);
  1659. if(br.winner==0) //attacker won
  1660. {
  1661. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  1662. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  1663. }
  1664. else //if(br.winner==1)
  1665. {
  1666. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  1667. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  1668. }
  1669. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  1670. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  1671. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  1672. std::string attackerName, defenderName;
  1673. if(owner->attackingHeroInstance) //a hero attacked
  1674. {
  1675. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  1676. //setting attackerName
  1677. attackerName = owner->attackingHeroInstance->name;
  1678. }
  1679. else //a monster attacked
  1680. {
  1681. int bestMonsterID = -1;
  1682. int bestPower = 0;
  1683. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  1684. {
  1685. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  1686. {
  1687. bestPower = CGI->creh->creatures[it->first].AIValue;
  1688. bestMonsterID = it->first;
  1689. }
  1690. }
  1691. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  1692. //setting attackerName
  1693. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  1694. }
  1695. if(owner->defendingHeroInstance) //a hero defended
  1696. {
  1697. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  1698. //setting defenderName
  1699. defenderName = owner->defendingHeroInstance->name;
  1700. }
  1701. else //a monster defended
  1702. {
  1703. int bestMonsterID = -1;
  1704. int bestPower = 0;
  1705. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  1706. {
  1707. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  1708. {
  1709. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  1710. bestMonsterID = it->second.first;
  1711. }
  1712. }
  1713. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  1714. //setting defenderName
  1715. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  1716. }
  1717. //printing attacker and defender's names
  1718. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  1719. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  1720. //printing casualities
  1721. for(int step = 0; step < 2; ++step)
  1722. {
  1723. if(br.casualties[step].size()==0)
  1724. {
  1725. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  1726. }
  1727. else
  1728. {
  1729. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  1730. int yPos = 344 + step*97;
  1731. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  1732. {
  1733. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  1734. std::stringstream amount;
  1735. amount<<it->second;
  1736. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  1737. xPos += 42;
  1738. }
  1739. }
  1740. }
  1741. //printing result description
  1742. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  1743. switch(br.result)
  1744. {
  1745. case 0: //normal victory
  1746. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1747. {
  1748. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  1749. }
  1750. else
  1751. {
  1752. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  1753. }
  1754. break;
  1755. case 1: //flee
  1756. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1757. {
  1758. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  1759. }
  1760. else
  1761. {
  1762. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  1763. }
  1764. break;
  1765. case 2: //surrender
  1766. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  1767. {
  1768. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  1769. }
  1770. else
  1771. {
  1772. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  1773. }
  1774. break;
  1775. }
  1776. }
  1777. CBattleReslutWindow::~CBattleReslutWindow()
  1778. {
  1779. SDL_FreeSurface(background);
  1780. }
  1781. void CBattleReslutWindow::activate()
  1782. {
  1783. 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. }