CBattleInterface.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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 "client/CCreatureAnimation.h"
  15. #include "client/Graphics.h"
  16. #include <queue>
  17. #include <sstream>
  18. #include "lib/CondSh.h"
  19. #ifndef __GNUC__
  20. const double M_PI = 3.14159265358979323846;
  21. #else
  22. #define _USE_MATH_DEFINES
  23. #include <cmath>
  24. #endif
  25. extern SDL_Surface * screen;
  26. extern TTF_Font * GEOR13;
  27. extern SDL_Color zwykly;
  28. SDL_Surface * CBattleInterface::cellBorder, * CBattleInterface::cellShade;
  29. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2)
  30. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL), attackingInfo(NULL), myTurn(false)
  31. {
  32. givenCommand = new CondSh<BattleAction *>(NULL);
  33. //initializing armies
  34. this->army1 = army1;
  35. this->army2 = army2;
  36. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  37. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  38. {
  39. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  40. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  41. creAnims[b->second.ID]->setType(2);
  42. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  43. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  44. }
  45. //preparing menu background and terrain
  46. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  47. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  48. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  49. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  50. //preparing graphics for displaying amounts of creatures
  51. amountBasic = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  52. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  53. CSDL_Ext::alphaTransform(amountNormal);
  54. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  55. {
  56. if((amountNormal->format->palette->colors+g)->b != 132 &&
  57. (amountNormal->format->palette->colors+g)->g != 231 &&
  58. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  59. {
  60. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  61. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  62. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  63. }
  64. }
  65. ////blitting menu background and terrain
  66. blitAt(background, 0, 0);
  67. blitAt(menu, 0, 556);
  68. CSDL_Ext::update();
  69. //preparing buttons and console
  70. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3, 561, "icm003.def", false, NULL, false);
  71. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54, 561, "icm001.def", false, NULL, false);
  72. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105, 561, "icm002.def", false, NULL, false);
  73. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157, 561, "icm004.def", false, NULL, false);
  74. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645, 561, "icm005.def", false, NULL, false);
  75. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696, 561, "icm006.def", false, NULL, false);
  76. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747, 561, "icm007.def", false, NULL, false);
  77. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624, 561, "ComSlide.def", false, NULL, false);
  78. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624, 580, "ComSlide.def", false, NULL, false);
  79. bConsoleDown->bitmapOffset = 2;
  80. console = new CBattleConsole();
  81. console->pos.x = 211;
  82. console->pos.y = 560;
  83. console->pos.w = 406;
  84. console->pos.h = 38;
  85. //loading hero animations
  86. if(hero1) // attacking hero
  87. {
  88. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner);
  89. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40, 0);
  90. }
  91. else
  92. {
  93. attackingHero = NULL;
  94. }
  95. if(hero2) // defending hero
  96. {
  97. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner);
  98. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690, 0);
  99. }
  100. else
  101. {
  102. defendingHero = NULL;
  103. }
  104. //preparing cells and hexes
  105. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  106. CSDL_Ext::alphaTransform(cellBorder);
  107. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  108. CSDL_Ext::alphaTransform(cellShade);
  109. for(int h=0; h<187; ++h)
  110. {
  111. bfield[h].myNumber = h;
  112. int x = 14 + ((h/17)%2==0 ? 22 : 0) + 44*(h%17);
  113. int y = 86 + 42 * (h/17);
  114. bfield[h].pos = genRect(cellShade->h, cellShade->w, x, y);
  115. bfield[h].accesible = true;
  116. bfield[h].myInterface = this;
  117. }
  118. //locking occupied positions on batlefield
  119. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  120. {
  121. bfield[it->second.position].accesible = false;
  122. }
  123. //loading projectiles for units
  124. for(std::map<int, CStack>::iterator g = stacks.begin(); g != stacks.end(); ++g)
  125. {
  126. if(g->second.creature->isShooting() && CGI->creh->idToProjectile[g->second.creature->idNumber] != std::string())
  127. {
  128. idToProjectile[g->second.creature->idNumber] = CDefHandler::giveDef(CGI->creh->idToProjectile[g->second.creature->idNumber]);
  129. if(idToProjectile[g->second.creature->idNumber]->ourImages.size() > 2) //add symmetric images
  130. {
  131. for(int k = idToProjectile[g->second.creature->idNumber]->ourImages.size()-2; k > 1; --k)
  132. {
  133. Cimage ci;
  134. ci.bitmap = CSDL_Ext::rotate01(idToProjectile[g->second.creature->idNumber]->ourImages[k].bitmap);
  135. ci.groupNumber = 0;
  136. ci.imName = std::string();
  137. idToProjectile[g->second.creature->idNumber]->ourImages.push_back(ci);
  138. }
  139. }
  140. for(int s=0; s<idToProjectile[g->second.creature->idNumber]->ourImages.size(); ++s) //alpha transforming
  141. {
  142. CSDL_Ext::alphaTransform(idToProjectile[g->second.creature->idNumber]->ourImages[s].bitmap);
  143. }
  144. }
  145. }
  146. }
  147. CBattleInterface::~CBattleInterface()
  148. {
  149. SDL_FreeSurface(background);
  150. SDL_FreeSurface(menu);
  151. SDL_FreeSurface(amountBasic);
  152. SDL_FreeSurface(amountNormal);
  153. delete bOptions;
  154. delete bSurrender;
  155. delete bFlee;
  156. delete bAutofight;
  157. delete bSpell;
  158. delete bWait;
  159. delete bDefence;
  160. delete bConsoleUp;
  161. delete bConsoleDown;
  162. delete console;
  163. delete givenCommand;
  164. delete attackingHero;
  165. delete defendingHero;
  166. SDL_FreeSurface(cellBorder);
  167. SDL_FreeSurface(cellShade);
  168. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  169. delete g->second;
  170. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  171. delete g->second;
  172. }
  173. void CBattleInterface::activate()
  174. {
  175. bOptions->activate();
  176. bSurrender->activate();
  177. bFlee->activate();
  178. bAutofight->activate();
  179. bSpell->activate();
  180. bWait->activate();
  181. bDefence->activate();
  182. bConsoleUp->activate();
  183. bConsoleDown->activate();
  184. for(int b=0; b<187; ++b)
  185. {
  186. bfield[b].activate();
  187. }
  188. }
  189. void CBattleInterface::deactivate()
  190. {
  191. bOptions->deactivate();
  192. bSurrender->deactivate();
  193. bFlee->deactivate();
  194. bAutofight->deactivate();
  195. bSpell->deactivate();
  196. bWait->deactivate();
  197. bDefence->deactivate();
  198. bConsoleUp->deactivate();
  199. bConsoleDown->deactivate();
  200. for(int b=0; b<187; ++b)
  201. {
  202. bfield[b].deactivate();
  203. }
  204. }
  205. void CBattleInterface::show(SDL_Surface * to)
  206. {
  207. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  208. ++animCount;
  209. if(!to) //"evaluating" to
  210. to = screen;
  211. //showing background
  212. blitAt(background, 0, 0, to);
  213. if(printCellBorders) //printing cell borders
  214. {
  215. for(int i=0; i<11; ++i) //rows
  216. {
  217. for(int j=0; j<15; ++j) //columns
  218. {
  219. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  220. int y = 86 + 42 * i;
  221. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorder, NULL, to, &genRect(cellBorder->h, cellBorder->w, x, y));
  222. }
  223. }
  224. }
  225. //printing hovered cell
  226. for(int b=0; b<187; ++b)
  227. {
  228. if(bfield[b].strictHovered && bfield[b].hovered)
  229. {
  230. int x = 14 + ((b/17)%2==0 ? 22 : 0) + 44*(b%17);
  231. int y = 86 + 42 * (b/17);
  232. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  233. }
  234. }
  235. //showing selected unit's range
  236. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //don't show if unit is moving
  237. {
  238. showRange(to, activeStack);
  239. }
  240. //showing menu background and console
  241. blitAt(menu, 0, 556, to);
  242. console->show(to);
  243. //showing buttons
  244. bOptions->show(to);
  245. bSurrender->show(to);
  246. bFlee->show(to);
  247. bAutofight->show(to);
  248. bSpell->show(to);
  249. bWait->show(to);
  250. bDefence->show(to);
  251. bConsoleUp->show(to);
  252. bConsoleDown->show(to);
  253. //showing hero animations
  254. if(attackingHero)
  255. attackingHero->show(to);
  256. if(defendingHero)
  257. defendingHero->show(to);
  258. ////showing units //a lot of work...
  259. std::vector<int> stackAliveByHex[187];
  260. //double loop because dead stacks should be printed first
  261. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  262. {
  263. if(j->second.alive)
  264. stackAliveByHex[j->second.position].push_back(j->second.ID);
  265. }
  266. std::vector<int> stackDeadByHex[187];
  267. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  268. {
  269. if(!j->second.alive)
  270. stackDeadByHex[j->second.position].push_back(j->second.ID);
  271. }
  272. attackingShowHelper(); // handle attack animation
  273. for(int b=0; b<187; ++b) //showing dead stacks
  274. {
  275. for(int v=0; v<stackDeadByHex[b].size(); ++v)
  276. {
  277. creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x, creAnims[stackDeadByHex[b][v]]->pos.y, creDir[stackDeadByHex[b][v]], (animCount%4==0 || creAnims[stackDeadByHex[b][v]]->getType()!=2) && stacks[stackDeadByHex[b][v]].alive, stackDeadByHex[b][v]==activeStack); //increment always when moving, never if stack died
  278. //printing amount
  279. if(stacks[stackDeadByHex[b][v]].amount > 0) //don't print if stack is not alive
  280. {
  281. int xAdd = stacks[stackDeadByHex[b][v]].attackerOwned ? 220 : 202;
  282. 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));
  283. std::stringstream ss;
  284. ss<<stacks[stackDeadByHex[b][v]].amount;
  285. 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);
  286. }
  287. }
  288. }
  289. for(int b=0; b<187; ++b) //showing alive stacks
  290. {
  291. for(int v=0; v<stackAliveByHex[b].size(); ++v)
  292. {
  293. creAnims[stackAliveByHex[b][v]]->nextFrame(to, creAnims[stackAliveByHex[b][v]]->pos.x, creAnims[stackAliveByHex[b][v]]->pos.y, creDir[stackAliveByHex[b][v]], (animCount%4==0) && stacks[stackAliveByHex[b][v]].alive, stackAliveByHex[b][v]==activeStack); //increment always when moving, never if stack died
  294. //printing amount
  295. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  296. {
  297. int xAdd = stacks[stackAliveByHex[b][v]].attackerOwned ? 220 : 202;
  298. 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));
  299. std::stringstream ss;
  300. ss<<stacks[stackAliveByHex[b][v]].amount;
  301. 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);
  302. }
  303. }
  304. }
  305. //units shown
  306. projectileShowHelper(to);//showing projectiles
  307. }
  308. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  309. {
  310. if(creAnims[number]==NULL)
  311. return false; //there is no such creature
  312. creAnims[number]->setType(8);
  313. for(int g=0; g<creAnims[number]->framesInGroup(8); ++g)
  314. {
  315. show();
  316. CSDL_Ext::update();
  317. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  318. if((animCount+1)%4)
  319. creAnims[number]->incrementFrame();
  320. }
  321. creDir[number] = !creDir[number];
  322. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  323. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  324. creAnims[number]->pos.x = coords.first;
  325. //creAnims[number]->pos.y = coords.second;
  326. if(wideTrick && curs.creature->isDoubleWide())
  327. {
  328. if(!creDir[number])
  329. creAnims[number]->pos.x -= 44;
  330. }
  331. creAnims[number]->setType(7);
  332. for(int g=0; g<creAnims[number]->framesInGroup(7); ++g)
  333. {
  334. show();
  335. CSDL_Ext::update();
  336. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  337. }
  338. creAnims[number]->setType(2);
  339. return true;
  340. }
  341. void CBattleInterface::bOptionsf()
  342. {
  343. }
  344. void CBattleInterface::bSurrenderf()
  345. {
  346. }
  347. void CBattleInterface::bFleef()
  348. {
  349. giveCommand(4,0,0);
  350. CGI->curh->changeGraphic(0, 0);
  351. }
  352. void CBattleInterface::bAutofightf()
  353. {
  354. }
  355. void CBattleInterface::bSpellf()
  356. {
  357. }
  358. void CBattleInterface::bWaitf()
  359. {
  360. }
  361. void CBattleInterface::bDefencef()
  362. {
  363. giveCommand(3,0,activeStack);
  364. }
  365. void CBattleInterface::bConsoleUpf()
  366. {
  367. console->scrollUp();
  368. }
  369. void CBattleInterface::bConsoleDownf()
  370. {
  371. console->scrollDown();
  372. }
  373. void CBattleInterface::newStack(CStack stack)
  374. {
  375. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  376. creAnims[stack.ID]->setType(2);
  377. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  378. }
  379. void CBattleInterface::stackRemoved(CStack stack)
  380. {
  381. delete creAnims[stack.ID];
  382. creAnims.erase(stack.ID);
  383. }
  384. void CBattleInterface::stackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  385. {
  386. if(creAnims[ID]->getType() != 2)
  387. {
  388. return; //something went wrong
  389. }
  390. if(byShooting) //delay hit animation
  391. {
  392. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  393. while(true)
  394. {
  395. bool found = false;
  396. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  397. {
  398. if(it->creID == attacker.creature->idNumber)
  399. {
  400. found = true;
  401. break;
  402. }
  403. }
  404. if(!found)
  405. break;
  406. else
  407. {
  408. show();
  409. CSDL_Ext::update();
  410. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  411. }
  412. }
  413. }
  414. creAnims[ID]->setType(5); //death
  415. for(int i=0; i<creAnims[ID]->framesInGroup(5); ++i)
  416. {
  417. show();
  418. CSDL_Ext::update();
  419. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  420. if((animCount+1)%4)
  421. creAnims[ID]->incrementFrame();
  422. }
  423. printConsoleAttacked(ID, dmg, killed, IDby);
  424. }
  425. void CBattleInterface::stackActivated(int number)
  426. {
  427. //givenCommand = NULL;
  428. activeStack = number;
  429. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(number);
  430. myTurn = true;
  431. }
  432. void CBattleInterface::stackMoved(int number, int destHex, bool startMoving, bool endMoving)
  433. {
  434. //a few useful variables
  435. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  436. int steps = creAnims[number]->framesInGroup(0);
  437. int hexWbase = 44, hexHbase = 42;
  438. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  439. if(startMoving) //animation of starting move
  440. {
  441. deactivate();
  442. CGI->curh->hide();
  443. creAnims[number]->setType(20);
  444. //LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  445. for(int i=0; i<creAnims[number]->framesInGroup(20); ++i)
  446. {
  447. show();
  448. CSDL_Ext::update();
  449. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  450. if((animCount+1)%4)
  451. creAnims[number]->incrementFrame();
  452. }
  453. }
  454. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  455. {
  456. switch(mutPos) //reverse unit if necessary
  457. {
  458. case 0: case 4: case 5:
  459. if(creDir[number] == true)
  460. reverseCreature(number, curStackPos, twoTiles);
  461. break;
  462. case 1: case 2: case 3:
  463. if(creDir[number] == false)
  464. reverseCreature(number, curStackPos, twoTiles);
  465. break;
  466. }
  467. //moving instructions
  468. creAnims[number]->setType(0);
  469. for(int i=0; i<steps; ++i)
  470. {
  471. switch(mutPos)
  472. {
  473. case 0:
  474. creAnims[number]->pos.x -= hexWbase/(2*steps);
  475. creAnims[number]->pos.y -= hexHbase/steps;
  476. break;
  477. case 1:
  478. creAnims[number]->pos.x += hexWbase/(2*steps);
  479. creAnims[number]->pos.y -= hexHbase/steps;
  480. break;
  481. case 2:
  482. creAnims[number]->pos.x += hexWbase/steps;
  483. break;
  484. case 3:
  485. creAnims[number]->pos.x += hexWbase/(2*steps);
  486. creAnims[number]->pos.y += hexHbase/steps;
  487. break;
  488. case 4:
  489. creAnims[number]->pos.x -= hexWbase/(2*steps);
  490. creAnims[number]->pos.y += hexHbase/steps;
  491. break;
  492. case 5:
  493. creAnims[number]->pos.x -= hexWbase/steps;
  494. break;
  495. }
  496. show();
  497. CSDL_Ext::update();
  498. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  499. if((animCount+1)%4)
  500. creAnims[number]->incrementFrame();
  501. }
  502. }
  503. if(endMoving) //animation of ending move
  504. {
  505. creAnims[number]->setType(21);
  506. for(int i=0; i<creAnims[number]->framesInGroup(21); ++i)
  507. {
  508. show();
  509. CSDL_Ext::update();
  510. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  511. if((animCount+1)%4)
  512. creAnims[number]->incrementFrame();
  513. }
  514. creAnims[number]->setType(2); //resetting to default
  515. activate();
  516. CGI->curh->show();
  517. }
  518. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  519. if(endMoving) //resetting to default
  520. {
  521. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  522. reverseCreature(number, destHex, twoTiles);
  523. }
  524. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  525. creAnims[number]->pos.x = coords.first;
  526. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  527. creAnims[number]->pos.x -= 44;
  528. creAnims[number]->pos.y = coords.second;
  529. }
  530. void CBattleInterface::stackIsAttacked(int ID, int dmg, int killed, int IDby, bool byShooting)
  531. {
  532. if(creAnims[ID]->getType() != 2)
  533. {
  534. return; //something went wrong
  535. }
  536. if(byShooting) //delay hit animation
  537. {
  538. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  539. while(true)
  540. {
  541. bool found = false;
  542. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  543. {
  544. if(it->creID == attacker.creature->idNumber)
  545. {
  546. found = true;
  547. break;
  548. }
  549. }
  550. if(!found)
  551. break;
  552. else
  553. {
  554. show();
  555. CSDL_Ext::update();
  556. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  557. }
  558. }
  559. }
  560. creAnims[ID]->setType(3); //getting hit
  561. for(int i=0; i<creAnims[ID]->framesInGroup(3); ++i)
  562. {
  563. show();
  564. CSDL_Ext::update();
  565. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  566. if((animCount+1)%4)
  567. creAnims[ID]->incrementFrame();
  568. }
  569. creAnims[ID]->setType(2);
  570. printConsoleAttacked(ID, dmg, killed, IDby);
  571. }
  572. void CBattleInterface::stackAttacking(int ID, int dest)
  573. {
  574. if(attackingInfo != NULL)
  575. {
  576. return; //something went wrong
  577. }
  578. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  579. if(aStack.creature->isDoubleWide())
  580. {
  581. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  582. {
  583. case 0:
  584. //reverseCreature(ID, aStack.position, true);
  585. break;
  586. case 1:
  587. break;
  588. case 2:
  589. break;
  590. case 3:
  591. break;
  592. case 4:
  593. //reverseCreature(ID, aStack.position, true);
  594. break;
  595. case 5:
  596. reverseCreature(ID, aStack.position, true);
  597. break;
  598. }
  599. }
  600. else //else for if(aStack.creature->isDoubleWide())
  601. {
  602. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  603. {
  604. case 0:
  605. reverseCreature(ID, aStack.position, true);
  606. break;
  607. case 1:
  608. break;
  609. case 2:
  610. break;
  611. case 3:
  612. break;
  613. case 4:
  614. reverseCreature(ID, aStack.position, true);
  615. break;
  616. case 5:
  617. reverseCreature(ID, aStack.position, true);
  618. break;
  619. }
  620. }
  621. attackingInfo = new CAttHelper;
  622. attackingInfo->dest = dest;
  623. attackingInfo->frame = 0;
  624. attackingInfo->ID = ID;
  625. attackingInfo->reversing = false;
  626. attackingInfo->shooting = false;
  627. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  628. {
  629. case 0:
  630. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  631. break;
  632. case 1:
  633. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  634. break;
  635. case 2:
  636. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  637. break;
  638. case 3:
  639. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  640. break;
  641. case 4:
  642. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  643. break;
  644. case 5:
  645. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  646. break;
  647. }
  648. }
  649. void CBattleInterface::newRound(int number)
  650. {
  651. console->addText(CGI->generaltexth->allTexts[412]);
  652. }
  653. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  654. {
  655. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  656. ba->actionType = action;
  657. ba->destinationTile = tile;
  658. ba->stackNumber = stack;
  659. ba->additionalInfo = additional;
  660. givenCommand->setn(ba);
  661. myTurn = false;
  662. activeStack = -1;
  663. }
  664. void CBattleInterface::hexLclicked(int whichOne)
  665. {
  666. if((whichOne%17)!=0 && (whichOne%17)!=16) //if player is trying to attack enemey unit or move creature stack
  667. {
  668. if(!myTurn)
  669. return; //we are not permit to do anything
  670. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  671. if(!dest || !dest->alive) //no creature at that tile
  672. {
  673. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  674. giveCommand(2,whichOne,activeStack);
  675. }
  676. else if(dest->owner != attackingHeroInstance->tempOwner
  677. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne)) //shooting
  678. {
  679. giveCommand(7,whichOne,activeStack);
  680. }
  681. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  682. {
  683. std::vector<int> n = BattleInfo::neighbouringTiles(whichOne);
  684. for(int i=0;i<n.size();i++)
  685. {
  686. //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
  687. if(vstd::contains(shadedHexes,n[i]))
  688. {
  689. giveCommand(6,n[i],activeStack,whichOne);
  690. return;
  691. }
  692. }
  693. }
  694. }
  695. }
  696. void CBattleInterface::stackIsShooting(int ID, int dest)
  697. {
  698. if(attackingInfo != NULL)
  699. {
  700. return; //something went wrong
  701. }
  702. //projectile
  703. float projectileAngle; //in radians; if positive, projectiles goes up
  704. 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)
  705. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  706. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  707. if(fromHex < dest)
  708. projectileAngle = -projectileAngle;
  709. SProjectileInfo spi;
  710. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  711. spi.step = 0;
  712. spi.frameNum = 0;
  713. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  714. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  715. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  716. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  717. if(projectileAngle > straightAngle) //upper shot
  718. {
  719. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  720. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  721. }
  722. else if(projectileAngle < -straightAngle) //lower shot
  723. {
  724. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  725. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  726. }
  727. else //straight shot
  728. {
  729. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  730. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  731. }
  732. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  733. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  734. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  735. //set starting frame
  736. if(spi.spin)
  737. {
  738. spi.frameNum = 0;
  739. }
  740. else
  741. {
  742. 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);
  743. }
  744. //set delay
  745. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  746. projectiles.push_back(spi);
  747. //attack aniamtion
  748. attackingInfo = new CAttHelper;
  749. attackingInfo->dest = dest;
  750. attackingInfo->frame = 0;
  751. attackingInfo->ID = ID;
  752. attackingInfo->reversing = false;
  753. attackingInfo->shooting = true;
  754. if(projectileAngle > straightAngle) //upper shot
  755. attackingInfo->shootingGroup = 14;
  756. else if(projectileAngle < -straightAngle) //lower shot
  757. attackingInfo->shootingGroup = 15;
  758. else //straight shot
  759. attackingInfo->shootingGroup = 16;
  760. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  761. }
  762. void CBattleInterface::showRange(SDL_Surface * to, int ID)
  763. {
  764. for(int i=0; i<shadedHexes.size(); ++i)
  765. {
  766. CSDL_Ext::blit8bppAlphaTo24bpp(CBattleInterface::cellShade, NULL, to, &bfield[shadedHexes[i]].pos);
  767. }
  768. }
  769. void CBattleInterface::attackingShowHelper()
  770. {
  771. if(attackingInfo && !attackingInfo->reversing)
  772. {
  773. if(attackingInfo->frame == 0)
  774. {
  775. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  776. if(attackingInfo->shooting)
  777. {
  778. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  779. }
  780. else
  781. {
  782. if(aStack.creature->isDoubleWide())
  783. {
  784. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  785. {
  786. case 0:
  787. creAnims[attackingInfo->ID]->setType(10);
  788. break;
  789. case 1:
  790. creAnims[attackingInfo->ID]->setType(10);
  791. break;
  792. case 2:
  793. creAnims[attackingInfo->ID]->setType(11);
  794. break;
  795. case 3:
  796. creAnims[attackingInfo->ID]->setType(12);
  797. break;
  798. case 4:
  799. creAnims[attackingInfo->ID]->setType(12);
  800. break;
  801. case 5:
  802. creAnims[attackingInfo->ID]->setType(11);
  803. break;
  804. }
  805. }
  806. else //else for if(aStack.creature->isDoubleWide())
  807. {
  808. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  809. {
  810. case 0:
  811. creAnims[attackingInfo->ID]->setType(10);
  812. break;
  813. case 1:
  814. creAnims[attackingInfo->ID]->setType(10);
  815. break;
  816. case 2:
  817. creAnims[attackingInfo->ID]->setType(11);
  818. break;
  819. case 3:
  820. creAnims[attackingInfo->ID]->setType(12);
  821. break;
  822. case 4:
  823. creAnims[attackingInfo->ID]->setType(12);
  824. break;
  825. case 5:
  826. creAnims[attackingInfo->ID]->setType(11);
  827. break;
  828. }
  829. }
  830. }
  831. }
  832. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  833. {
  834. attackingInfo->reversing = true;
  835. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  836. if(aStack.creature->isDoubleWide())
  837. {
  838. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  839. {
  840. case 0:
  841. //reverseCreature(ID, aStack.position, true);
  842. break;
  843. case 1:
  844. break;
  845. case 2:
  846. break;
  847. case 3:
  848. break;
  849. case 4:
  850. //reverseCreature(ID, aStack.position, true);
  851. break;
  852. case 5:
  853. reverseCreature(attackingInfo->ID, aStack.position, true);
  854. break;
  855. }
  856. }
  857. else //else for if(aStack.creature->isDoubleWide())
  858. {
  859. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  860. {
  861. case 0:
  862. reverseCreature(attackingInfo->ID, aStack.position, true);
  863. break;
  864. case 1:
  865. break;
  866. case 2:
  867. break;
  868. case 3:
  869. break;
  870. case 4:
  871. reverseCreature(attackingInfo->ID, aStack.position, true);
  872. break;
  873. case 5:
  874. reverseCreature(attackingInfo->ID, aStack.position, true);
  875. break;
  876. }
  877. }
  878. attackingInfo->reversing = false;
  879. creAnims[attackingInfo->ID]->setType(2);
  880. delete attackingInfo;
  881. attackingInfo = NULL;
  882. }
  883. if(attackingInfo)
  884. {
  885. attackingInfo->frame++;
  886. }
  887. }
  888. }
  889. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  890. {
  891. char tabh[200];
  892. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  893. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  894. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  895. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  896. dmg);
  897. if(killed > 0)
  898. {
  899. if(killed > 1)
  900. {
  901. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  902. }
  903. else //killed == 1
  904. {
  905. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  906. }
  907. }
  908. console->addText(std::string(tabh));
  909. }
  910. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  911. {
  912. if(to == NULL)
  913. to = screen;
  914. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  915. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  916. {
  917. if(it->animStartDelay>0)
  918. {
  919. --(it->animStartDelay);
  920. continue;
  921. }
  922. SDL_Rect dst;
  923. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  924. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  925. dst.x = it->x;
  926. dst.y = it->y;
  927. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  928. //actualizing projectile
  929. ++it->step;
  930. if(it->step == it->lastStep)
  931. {
  932. toBeDeleted.insert(toBeDeleted.end(), it);
  933. }
  934. else
  935. {
  936. it->x += it->dx;
  937. it->y += it->dy;
  938. if(it->spin)
  939. {
  940. ++(it->frameNum);
  941. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  942. }
  943. }
  944. }
  945. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  946. {
  947. projectiles.erase(*it);
  948. }
  949. }
  950. void CBattleHero::show(SDL_Surface *to)
  951. {
  952. //animation of flag
  953. if(flip)
  954. {
  955. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 752, 39));
  956. }
  957. else
  958. {
  959. CSDL_Ext::blit8bppAlphaTo24bpp(flag->ourImages[flagAnim].bitmap, NULL, screen, &genRect(flag->ourImages[flagAnim].bitmap->h, flag->ourImages[flagAnim].bitmap->w, 31, 39));
  960. }
  961. {
  962. ++flagAnim;
  963. flagAnim %= flag->ourImages.size();
  964. }
  965. //animation of hero
  966. int tick=-1;
  967. for(int i=0; i<dh->ourImages.size(); ++i)
  968. {
  969. if(dh->ourImages[i].groupNumber==phase)
  970. ++tick;
  971. if(tick==image)
  972. {
  973. SDL_Rect posb = pos;
  974. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  975. ++image;
  976. if(dh->ourImages[i+1].groupNumber!=phase) //back to appropriate frame
  977. {
  978. image = 0;
  979. }
  980. break;
  981. }
  982. }
  983. }
  984. CBattleHero::CBattleHero(std::string defName, int phaseG, int imageG, bool flipG, unsigned char player): phase(phaseG), image(imageG), flip(flipG), flagAnim(0)
  985. {
  986. dh = CDefHandler::giveDef( defName );
  987. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  988. {
  989. if(flip)
  990. {
  991. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  992. SDL_FreeSurface(dh->ourImages[i].bitmap);
  993. dh->ourImages[i].bitmap = hlp;
  994. }
  995. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  996. }
  997. dh->alphaTransformed = true;
  998. if(flip)
  999. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1000. else
  1001. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1002. //coloring flag and adding transparency
  1003. for(int i=0; i<flag->ourImages.size(); ++i)
  1004. {
  1005. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1006. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1007. }
  1008. }
  1009. CBattleHero::~CBattleHero()
  1010. {
  1011. delete dh;
  1012. delete flag;
  1013. }
  1014. std::pair<int, int> CBattleHex::getXYUnitAnim(int hexNum, bool attacker, CCreature * creature)
  1015. {
  1016. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1017. ret.second = -139 + 42 * (hexNum/17); //counting y
  1018. //counting x
  1019. if(attacker)
  1020. {
  1021. ret.first = -160 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1022. }
  1023. else
  1024. {
  1025. ret.first = -219 + 22 * ( ((hexNum/17) + 1)%2 ) + 44 * (hexNum % 17);
  1026. }
  1027. //shifting position for double - hex creatures
  1028. if(creature->isDoubleWide())
  1029. {
  1030. if(attacker)
  1031. {
  1032. ret.first -= 42;
  1033. }
  1034. else
  1035. {
  1036. ret.first += 42;
  1037. }
  1038. }
  1039. //returning
  1040. return ret;
  1041. }
  1042. void CBattleHex::activate()
  1043. {
  1044. Hoverable::activate();
  1045. MotionInterested::activate();
  1046. ClickableL::activate();
  1047. ClickableR::activate();
  1048. }
  1049. void CBattleHex::deactivate()
  1050. {
  1051. Hoverable::deactivate();
  1052. MotionInterested::deactivate();
  1053. ClickableL::deactivate();
  1054. ClickableR::deactivate();
  1055. }
  1056. void CBattleHex::hover(bool on)
  1057. {
  1058. hovered = on;
  1059. Hoverable::hover(on);
  1060. if(!on && setAlterText)
  1061. {
  1062. myInterface->console->alterTxt = std::string();
  1063. setAlterText = false;
  1064. }
  1065. }
  1066. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  1067. {
  1068. }
  1069. void CBattleHex::mouseMoved(SDL_MouseMotionEvent &sEvent)
  1070. {
  1071. if(CBattleInterface::cellShade)
  1072. {
  1073. if(CSDL_Ext::SDL_GetPixel(CBattleInterface::cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  1074. {
  1075. strictHovered = false;
  1076. }
  1077. else //hovered pixel is inside hex
  1078. {
  1079. strictHovered = true;
  1080. if(myInterface->activeStack>=0)
  1081. {
  1082. if(std::find(myInterface->shadedHexes.begin(),myInterface->shadedHexes.end(),myNumber) == myInterface->shadedHexes.end())
  1083. {
  1084. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  1085. if(shere)
  1086. {
  1087. if(shere->owner == LOCPLINT->playerID) //our stack
  1088. CGI->curh->changeGraphic(1,5);
  1089. else if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isShooting()) //we can shoot enemy
  1090. CGI->curh->changeGraphic(1,3);
  1091. else //unavailable enemy
  1092. CGI->curh->changeGraphic(1,0);
  1093. }
  1094. else //empty unavailable tile
  1095. CGI->curh->changeGraphic(1,0);
  1096. }
  1097. else //available tile
  1098. {
  1099. if(LOCPLINT->cb->battleGetStackByID(myInterface->activeStack)->creature->isFlying())
  1100. CGI->curh->changeGraphic(1,2);
  1101. else
  1102. CGI->curh->changeGraphic(1,1);
  1103. }
  1104. }
  1105. }
  1106. }
  1107. if(hovered && strictHovered) //print attacked creature to console
  1108. {
  1109. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  1110. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  1111. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive)
  1112. {
  1113. char tabh[160];
  1114. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  1115. std::string attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  1116. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  1117. myInterface->console->alterTxt = std::string(tabh);
  1118. setAlterText = true;
  1119. }
  1120. }
  1121. else if(setAlterText)
  1122. {
  1123. myInterface->console->alterTxt = std::string();
  1124. setAlterText = false;
  1125. }
  1126. }
  1127. void CBattleHex::clickLeft(boost::logic::tribool down)
  1128. {
  1129. if(!down && hovered && strictHovered) //we've been really clicked!
  1130. {
  1131. myInterface->hexLclicked(myNumber);
  1132. }
  1133. }
  1134. void CBattleHex::clickRight(boost::logic::tribool down)
  1135. {
  1136. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  1137. if(hovered && strictHovered && stID!=-1)
  1138. {
  1139. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  1140. if(!myst.alive) return;
  1141. StackState *pom = NULL;
  1142. if(down)
  1143. {
  1144. pom = new StackState();
  1145. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  1146. if(h)
  1147. {
  1148. pom->attackBonus = h->primSkills[0];
  1149. pom->defenseBonus = h->primSkills[1];
  1150. pom->luck = h->getCurrentLuck();
  1151. pom->morale = h->getCurrentMorale();
  1152. pom->currentHealth = myst.firstHPleft;
  1153. }
  1154. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  1155. ->activate();
  1156. }
  1157. delete pom;
  1158. }
  1159. }
  1160. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  1161. {
  1162. }
  1163. CBattleConsole::~CBattleConsole()
  1164. {
  1165. texts.clear();
  1166. }
  1167. void CBattleConsole::show(SDL_Surface * to)
  1168. {
  1169. if(alterTxt.size())
  1170. {
  1171. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1172. }
  1173. else if(texts.size())
  1174. {
  1175. if(texts.size()==1)
  1176. {
  1177. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1178. }
  1179. else
  1180. {
  1181. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  1182. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  1183. }
  1184. }
  1185. }
  1186. bool CBattleConsole::addText(std::string text)
  1187. {
  1188. if(text.size()>70)
  1189. return false; //text too long!
  1190. int firstInToken = 0;
  1191. for(int i=0; i<text.size(); ++i) //tokenize
  1192. {
  1193. if(text[i] == 10)
  1194. {
  1195. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  1196. firstInToken = i+1;
  1197. }
  1198. }
  1199. texts.push_back( text.substr(firstInToken, text.size()) );
  1200. lastShown = texts.size()-1;
  1201. return true;
  1202. }
  1203. void CBattleConsole::eraseText(unsigned int pos)
  1204. {
  1205. if(pos < texts.size())
  1206. {
  1207. texts.erase(texts.begin() + pos);
  1208. if(lastShown == texts.size())
  1209. --lastShown;
  1210. }
  1211. }
  1212. void CBattleConsole::changeTextAt(std::string text, unsigned int pos)
  1213. {
  1214. if(pos >= texts.size()) //no such pos
  1215. return;
  1216. texts[pos] = text;
  1217. }
  1218. void CBattleConsole::scrollUp(unsigned int by)
  1219. {
  1220. if(lastShown > by)
  1221. lastShown -= by;
  1222. }
  1223. void CBattleConsole::scrollDown(unsigned int by)
  1224. {
  1225. if(lastShown + by < texts.size())
  1226. lastShown += by;
  1227. }