CBattleInterface.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564
  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 "hch/CSpellHandler.h"
  11. #include "CMessage.h"
  12. #include "CCursorHandler.h"
  13. #include "CCallback.h"
  14. #include "CGameState.h"
  15. #include "hch/CGeneralTextHandler.h"
  16. #include "client/CCreatureAnimation.h"
  17. #include "client/Graphics.h"
  18. #include "client/CSpellWindow.h"
  19. #include "client/CConfigHandler.h"
  20. #include <queue>
  21. #include <sstream>
  22. #include "lib/CondSh.h"
  23. #include "lib/NetPacks.h"
  24. #include <boost/assign/list_of.hpp>
  25. #ifndef __GNUC__
  26. const double M_PI = 3.14159265358979323846;
  27. #else
  28. #define _USE_MATH_DEFINES
  29. #include <cmath>
  30. #endif
  31. extern SDL_Surface * screen;
  32. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM, *GEOR16;
  33. extern SDL_Color zwykly;
  34. struct CMP_stack2
  35. {
  36. inline bool operator ()(const CStack& a, const CStack& b)
  37. {
  38. return (a.speed())>(b.speed());
  39. }
  40. } cmpst2 ;
  41. CBattleInterface::CBattleInterface(CCreatureSet * army1, CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect)
  42. : printCellBorders(true), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0), activeStack(-1), givenCommand(NULL),
  43. attackingInfo(NULL), myTurn(false), resWindow(NULL), showStackQueue(false), animSpeed(2), printStackRange(true),
  44. printMouseShadow(true), spellDestSelectMode(false), spellToCast(NULL), previouslyHoveredHex(-1)
  45. {
  46. pos = myRect;
  47. strongInterest = true;
  48. givenCommand = new CondSh<BattleAction *>(NULL);
  49. //initializing armies
  50. this->army1 = army1;
  51. this->army2 = army2;
  52. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks();
  53. for(std::map<int, CStack>::iterator b=stacks.begin(); b!=stacks.end(); ++b)
  54. {
  55. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(b->second.position, b->second.owner == attackingHeroInstance->tempOwner, b->second.creature);
  56. creAnims[b->second.ID] = (new CCreatureAnimation(b->second.creature->animDefName));
  57. creAnims[b->second.ID]->setType(2);
  58. creAnims[b->second.ID]->pos = genRect(creAnims[b->second.ID]->fullHeight, creAnims[b->second.ID]->fullWidth, coords.first, coords.second);
  59. creDir[b->second.ID] = b->second.owner==attackingHeroInstance->tempOwner;
  60. }
  61. //preparing menu background and terrain
  62. std::vector< std::string > & backref = graphics->battleBacks[ LOCPLINT->cb->battleGetBattlefieldType() ];
  63. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()] );
  64. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  65. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  66. //preparing graphics for displaying amounts of creatures
  67. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  68. CSDL_Ext::alphaTransform(amountNormal);
  69. for(int g=0; g<amountNormal->format->palette->ncolors; ++g)
  70. {
  71. if((amountNormal->format->palette->colors+g)->b != 132 &&
  72. (amountNormal->format->palette->colors+g)->g != 231 &&
  73. (amountNormal->format->palette->colors+g)->r != 255) //it's not yellow border
  74. {
  75. (amountNormal->format->palette->colors+g)->r = (float)((amountNormal->format->palette->colors+g)->r) * 0.54f;
  76. (amountNormal->format->palette->colors+g)->g = (float)((amountNormal->format->palette->colors+g)->g) * 0.19f;
  77. (amountNormal->format->palette->colors+g)->b = (float)((amountNormal->format->palette->colors+g)->b) * 0.93f;
  78. }
  79. }
  80. amountPositive = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  81. CSDL_Ext::alphaTransform(amountPositive);
  82. for(int g=0; g<amountPositive->format->palette->ncolors; ++g)
  83. {
  84. if((amountPositive->format->palette->colors+g)->b != 132 &&
  85. (amountPositive->format->palette->colors+g)->g != 231 &&
  86. (amountPositive->format->palette->colors+g)->r != 255) //it's not yellow border
  87. {
  88. (amountPositive->format->palette->colors+g)->r = (float)((amountPositive->format->palette->colors+g)->r) * 0.18f;
  89. (amountPositive->format->palette->colors+g)->g = (float)((amountPositive->format->palette->colors+g)->g) * 1.00f;
  90. (amountPositive->format->palette->colors+g)->b = (float)((amountPositive->format->palette->colors+g)->b) * 0.18f;
  91. }
  92. }
  93. amountNegative = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  94. CSDL_Ext::alphaTransform(amountNegative);
  95. for(int g=0; g<amountNegative->format->palette->ncolors; ++g)
  96. {
  97. if((amountNegative->format->palette->colors+g)->b != 132 &&
  98. (amountNegative->format->palette->colors+g)->g != 231 &&
  99. (amountNegative->format->palette->colors+g)->r != 255) //it's not yellow border
  100. {
  101. (amountNegative->format->palette->colors+g)->r = (float)((amountNegative->format->palette->colors+g)->r) * 1.00f;
  102. (amountNegative->format->palette->colors+g)->g = (float)((amountNegative->format->palette->colors+g)->g) * 0.18f;
  103. (amountNegative->format->palette->colors+g)->b = (float)((amountNegative->format->palette->colors+g)->b) * 0.18f;
  104. }
  105. }
  106. amountEffNeutral = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  107. CSDL_Ext::alphaTransform(amountNegative);
  108. for(int g=0; g<amountNegative->format->palette->ncolors; ++g)
  109. {
  110. if((amountNegative->format->palette->colors+g)->b != 132 &&
  111. (amountNegative->format->palette->colors+g)->g != 231 &&
  112. (amountNegative->format->palette->colors+g)->r != 255) //it's not yellow border
  113. {
  114. (amountNegative->format->palette->colors+g)->r = (float)((amountNegative->format->palette->colors+g)->r) * 1.00f;
  115. (amountNegative->format->palette->colors+g)->g = (float)((amountNegative->format->palette->colors+g)->g) * 1.00f;
  116. (amountNegative->format->palette->colors+g)->b = (float)((amountNegative->format->palette->colors+g)->b) * 0.18f;
  117. }
  118. }
  119. ////blitting menu background and terrain
  120. blitAt(background, pos.x, pos.y);
  121. blitAt(menu, pos.x, 556 + pos.y);
  122. CSDL_Ext::update();
  123. //preparing buttons and console
  124. bOptions = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bOptionsf,this), 3 + pos.x, 561 + pos.y, "icm003.def", SDLK_o);
  125. bSurrender = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSurrenderf,this), 54 + pos.x, 561 + pos.y, "icm001.def", SDLK_s);
  126. bFlee = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bFleef,this), 105 + pos.x, 561 + pos.y, "icm002.def", SDLK_r);
  127. bAutofight = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bAutofightf,this), 157 + pos.x, 561 + pos.y, "icm004.def", SDLK_a);
  128. bSpell = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bSpellf,this), 645 + pos.x, 561 + pos.y, "icm005.def", SDLK_c);
  129. bWait = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bWaitf,this), 696 + pos.x, 561 + pos.y, "icm006.def", SDLK_w);
  130. bDefence = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bDefencef,this), 747 + pos.x, 561 + pos.y, "icm007.def", SDLK_d);
  131. bDefence->assignedKeys.insert(SDLK_SPACE);
  132. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624 + pos.x, 561 + pos.y, "ComSlide.def", SDLK_UP);
  133. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624 + pos.x, 580 + pos.y, "ComSlide.def", SDLK_DOWN);
  134. bConsoleDown->bitmapOffset = 2;
  135. console = new CBattleConsole();
  136. console->pos.x = 211 + pos.x;
  137. console->pos.y = 560 + pos.y;
  138. console->pos.w = 406;
  139. console->pos.h = 38;
  140. //loading hero animations
  141. if(hero1) // attacking hero
  142. {
  143. attackingHero = new CBattleHero(graphics->battleHeroes[hero1->type->heroType], 0, 0, false, hero1->tempOwner, hero1->tempOwner == LOCPLINT->playerID ? hero1 : NULL, this);
  144. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40 + pos.x, pos.y);
  145. }
  146. else
  147. {
  148. attackingHero = NULL;
  149. }
  150. if(hero2) // defending hero
  151. {
  152. defendingHero = new CBattleHero(graphics->battleHeroes[hero2->type->heroType], 0, 0, true, hero2->tempOwner, hero2->tempOwner == LOCPLINT->playerID ? hero2 : NULL, this);
  153. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690 + pos.x, pos.y);
  154. }
  155. else
  156. {
  157. defendingHero = NULL;
  158. }
  159. //preparing cells and hexes
  160. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  161. CSDL_Ext::alphaTransform(cellBorder);
  162. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  163. CSDL_Ext::alphaTransform(cellShade);
  164. for(int h=0; h<BFIELD_SIZE; ++h)
  165. {
  166. bfield[h].myNumber = h;
  167. int x = 14 + ((h/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(h%BFIELD_WIDTH);
  168. int y = 86 + 42 * (h/BFIELD_WIDTH);
  169. bfield[h].pos = genRect(cellShade->h, cellShade->w, x + pos.x, y + pos.y);
  170. bfield[h].accesible = true;
  171. bfield[h].myInterface = this;
  172. }
  173. //locking occupied positions on batlefield
  174. for(std::map<int, CStack>::iterator it = stacks.begin(); it!=stacks.end(); ++it) //stacks gained at top of this function
  175. {
  176. bfield[it->second.position].accesible = false;
  177. }
  178. //loading projectiles for units
  179. for(std::map<int, CStack>::iterator g = stacks.begin(); g != stacks.end(); ++g)
  180. {
  181. if(g->second.creature->isShooting() && CGI->creh->idToProjectile[g->second.creature->idNumber] != std::string())
  182. {
  183. idToProjectile[g->second.creature->idNumber] = CDefHandler::giveDef(CGI->creh->idToProjectile[g->second.creature->idNumber]);
  184. if(idToProjectile[g->second.creature->idNumber]->ourImages.size() > 2) //add symmetric images
  185. {
  186. for(int k = idToProjectile[g->second.creature->idNumber]->ourImages.size()-2; k > 1; --k)
  187. {
  188. Cimage ci;
  189. ci.bitmap = CSDL_Ext::rotate01(idToProjectile[g->second.creature->idNumber]->ourImages[k].bitmap);
  190. ci.groupNumber = 0;
  191. ci.imName = std::string();
  192. idToProjectile[g->second.creature->idNumber]->ourImages.push_back(ci);
  193. }
  194. }
  195. for(int s=0; s<idToProjectile[g->second.creature->idNumber]->ourImages.size(); ++s) //alpha transforming
  196. {
  197. CSDL_Ext::alphaTransform(idToProjectile[g->second.creature->idNumber]->ourImages[s].bitmap);
  198. }
  199. }
  200. }
  201. //preparing graphic with cell borders
  202. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  203. //copying palette
  204. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  205. {
  206. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  207. }
  208. //palette copied
  209. for(int i=0; i<BFIELD_HEIGHT; ++i) //rows
  210. {
  211. for(int j=0; j<BFIELD_WIDTH-2; ++j) //columns
  212. {
  213. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  214. int y = 86 + 42 * i;
  215. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  216. {
  217. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  218. {
  219. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  220. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  221. }
  222. }
  223. }
  224. }
  225. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  226. }
  227. CBattleInterface::~CBattleInterface()
  228. {
  229. SDL_FreeSurface(background);
  230. SDL_FreeSurface(menu);
  231. SDL_FreeSurface(amountNormal);
  232. SDL_FreeSurface(amountNegative);
  233. SDL_FreeSurface(amountPositive);
  234. SDL_FreeSurface(amountEffNeutral);
  235. SDL_FreeSurface(cellBorders);
  236. SDL_FreeSurface(backgroundWithHexes);
  237. delete bOptions;
  238. delete bSurrender;
  239. delete bFlee;
  240. delete bAutofight;
  241. delete bSpell;
  242. delete bWait;
  243. delete bDefence;
  244. delete bConsoleUp;
  245. delete bConsoleDown;
  246. delete console;
  247. delete resWindow;
  248. delete givenCommand;
  249. delete attackingHero;
  250. delete defendingHero;
  251. SDL_FreeSurface(cellBorder);
  252. SDL_FreeSurface(cellShade);
  253. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  254. delete g->second;
  255. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  256. delete g->second;
  257. }
  258. void CBattleInterface::setPrintCellBorders(bool set)
  259. {
  260. printCellBorders = set;
  261. redrawBackgroundWithHexes(activeStack);
  262. }
  263. void CBattleInterface::setPrintStackRange(bool set)
  264. {
  265. printStackRange = set;
  266. redrawBackgroundWithHexes(activeStack);
  267. }
  268. void CBattleInterface::setPrintMouseShadow(bool set)
  269. {
  270. printMouseShadow = set;
  271. }
  272. void CBattleInterface::activate()
  273. {
  274. KeyInterested::activate();
  275. MotionInterested::activate();
  276. subInt = NULL;
  277. bOptions->activate();
  278. bSurrender->activate();
  279. bFlee->activate();
  280. bAutofight->activate();
  281. bSpell->activate();
  282. bWait->activate();
  283. bDefence->activate();
  284. bConsoleUp->activate();
  285. bConsoleDown->activate();
  286. for(int b=0; b<BFIELD_SIZE; ++b)
  287. {
  288. bfield[b].activate();
  289. }
  290. if(attackingHero)
  291. attackingHero->activate();
  292. if(defendingHero)
  293. defendingHero->activate();
  294. }
  295. void CBattleInterface::deactivate()
  296. {
  297. KeyInterested::deactivate();
  298. MotionInterested::deactivate();
  299. bOptions->deactivate();
  300. bSurrender->deactivate();
  301. bFlee->deactivate();
  302. bAutofight->deactivate();
  303. bSpell->deactivate();
  304. bWait->deactivate();
  305. bDefence->deactivate();
  306. bConsoleUp->deactivate();
  307. bConsoleDown->deactivate();
  308. for(int b=0; b<BFIELD_SIZE; ++b)
  309. {
  310. bfield[b].deactivate();
  311. }
  312. if(attackingHero)
  313. attackingHero->deactivate();
  314. if(defendingHero)
  315. defendingHero->deactivate();
  316. }
  317. void CBattleInterface::show(SDL_Surface * to)
  318. {
  319. std::map<int, CStack> stacks = LOCPLINT->cb->battleGetStacks(); //used in a few places
  320. ++animCount;
  321. if(!to) //"evaluating" to
  322. to = screen;
  323. SDL_Rect buf;
  324. SDL_GetClipRect(to, &buf);
  325. SDL_SetClipRect(to, &pos);
  326. //printing background and hexes
  327. if(activeStack != -1 && creAnims[activeStack]->getType() != 0) //show everything with range
  328. {
  329. blitAt(backgroundWithHexes, pos.x, pos.y, to);
  330. }
  331. else
  332. {
  333. //showing background
  334. blitAt(background, pos.x, pos.y, to);
  335. if(printCellBorders)
  336. {
  337. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, &pos);
  338. }
  339. }
  340. //printing hovered cell
  341. if(printMouseShadow)
  342. {
  343. for(int b=0; b<BFIELD_SIZE; ++b)
  344. {
  345. if(bfield[b].strictHovered && bfield[b].hovered)
  346. {
  347. if(previouslyHoveredHex == -1) previouslyHoveredHex = b; //something to start with
  348. if(currentlyHoveredHex == -1) currentlyHoveredHex = b; //something to start with
  349. if(currentlyHoveredHex != b) //repair hover info
  350. {
  351. previouslyHoveredHex = currentlyHoveredHex;
  352. currentlyHoveredHex = b;
  353. }
  354. //print shade
  355. int x = 14 + ((b/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(b%BFIELD_WIDTH) + pos.x;
  356. int y = 86 + 42 * (b/BFIELD_WIDTH) + pos.y;
  357. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  358. }
  359. }
  360. }
  361. //showing menu background and console
  362. blitAt(menu, pos.x, 556 + pos.y, to);
  363. console->show(to);
  364. //showing buttons
  365. bOptions->show(to);
  366. bSurrender->show(to);
  367. bFlee->show(to);
  368. bAutofight->show(to);
  369. bSpell->show(to);
  370. bWait->show(to);
  371. bDefence->show(to);
  372. bConsoleUp->show(to);
  373. bConsoleDown->show(to);
  374. //showing hero animations
  375. if(attackingHero)
  376. attackingHero->show(to);
  377. if(defendingHero)
  378. defendingHero->show(to);
  379. ////showing units //a lot of work...
  380. std::vector<int> stackAliveByHex[BFIELD_SIZE];
  381. //double loop because dead stacks should be printed first
  382. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  383. {
  384. if(j->second.alive())
  385. stackAliveByHex[j->second.position].push_back(j->second.ID);
  386. }
  387. std::vector<int> stackDeadByHex[BFIELD_SIZE];
  388. for(std::map<int, CStack>::iterator j=stacks.begin(); j!=stacks.end(); ++j)
  389. {
  390. if(!j->second.alive())
  391. stackDeadByHex[j->second.position].push_back(j->second.ID);
  392. }
  393. attackingShowHelper(); // handle attack animation
  394. for(int b=0; b<BFIELD_SIZE; ++b) //showing dead stacks
  395. {
  396. for(size_t v=0; v<stackDeadByHex[b].size(); ++v)
  397. {
  398. creAnims[stackDeadByHex[b][v]]->nextFrame(to, creAnims[stackDeadByHex[b][v]]->pos.x + pos.x, creAnims[stackDeadByHex[b][v]]->pos.y + pos.y, creDir[stackDeadByHex[b][v]], false, stackDeadByHex[b][v]==activeStack); //increment always when moving, never if stack died
  399. }
  400. }
  401. for(int b=0; b<BFIELD_SIZE; ++b) //showing alive stacks
  402. {
  403. for(size_t v=0; v<stackAliveByHex[b].size(); ++v)
  404. {
  405. int animType = creAnims[stackAliveByHex[b][v]]->getType();
  406. bool incrementFrame = (animCount%(4/animSpeed)==0) && animType!=0 && animType!=5 && animType!=20 && animType!=21 && animType!=3;
  407. creAnims[stackAliveByHex[b][v]]->nextFrame(to, creAnims[stackAliveByHex[b][v]]->pos.x + pos.x, creAnims[stackAliveByHex[b][v]]->pos.y + pos.y, creDir[stackAliveByHex[b][v]], incrementFrame, stackAliveByHex[b][v]==activeStack); //increment always when moving, never if stack died
  408. //printing amount
  409. if(stacks[stackAliveByHex[b][v]].amount > 0) //don't print if stack is not alive
  410. {
  411. int xAdd = stacks[stackAliveByHex[b][v]].attackerOwned ? 220 : 202;
  412. //blitting amoutn background box
  413. SDL_Surface *amountBG = NULL;
  414. if(stacks[stackAliveByHex[b][v]].effects.size() == 0)
  415. {
  416. amountBG = amountNormal;
  417. }
  418. else
  419. {
  420. int pos=0; //determining total positiveness of effects
  421. for(int c=0; c<stacks[stackAliveByHex[b][v]].effects.size(); ++c)
  422. {
  423. pos += CGI->spellh->spells[ stacks[stackAliveByHex[b][v]].effects[c].id ].positiveness;
  424. }
  425. if(pos > 0)
  426. {
  427. amountBG = amountPositive;
  428. }
  429. else if(pos < 0)
  430. {
  431. amountBG = amountNegative;
  432. }
  433. else
  434. {
  435. amountBG = amountEffNeutral;
  436. }
  437. }
  438. SDL_BlitSurface(amountBG, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[stackAliveByHex[b][v]]->pos.x + xAdd + pos.x, creAnims[stackAliveByHex[b][v]]->pos.y + 260 + pos.y));
  439. //blitting amount
  440. std::stringstream ss;
  441. ss<<stacks[stackAliveByHex[b][v]].amount;
  442. CSDL_Ext::printAtMiddleWB(
  443. ss.str(),
  444. creAnims[stackAliveByHex[b][v]]->pos.x + xAdd + 14 + pos.x,
  445. creAnims[stackAliveByHex[b][v]]->pos.y + 260 + 4 + pos.y,
  446. GEOR13,
  447. 20,
  448. zwykly,
  449. to
  450. );
  451. }
  452. }
  453. }
  454. //units shown
  455. projectileShowHelper(to);//showing projectiles
  456. //showing spell effects
  457. if(battleEffects.size())
  458. {
  459. std::vector< std::list<SBattleEffect>::iterator > toErase;
  460. for(std::list<SBattleEffect>::iterator it = battleEffects.begin(); it!=battleEffects.end(); ++it)
  461. {
  462. SDL_Surface * bitmapToBlit = it->anim->ourImages[(it->frame)%it->anim->ourImages.size()].bitmap;
  463. SDL_BlitSurface(bitmapToBlit, NULL, to, &genRect(bitmapToBlit->h, bitmapToBlit->w, pos.x + it->x, pos.y + it->y));
  464. ++(it->frame);
  465. if(it->frame == it->maxFrame)
  466. {
  467. toErase.push_back(it);
  468. }
  469. }
  470. for(size_t b=0; b<toErase.size(); ++b)
  471. {
  472. delete toErase[b]->anim;
  473. battleEffects.erase(toErase[b]);
  474. }
  475. }
  476. //showing queue of stacks
  477. if(showStackQueue)
  478. {
  479. int xPos = screen->w/2 - ( stacks.size() * 37 )/2;
  480. int yPos = 10;
  481. std::vector<CStack> stacksSorted;
  482. stacksSorted = LOCPLINT->cb->battleGetStackQueue();
  483. int startFrom = -1;
  484. for(size_t n=0; n<stacksSorted.size(); ++n)
  485. {
  486. if(stacksSorted[n].ID == activeStack)
  487. {
  488. startFrom = n;
  489. break;
  490. }
  491. }
  492. if(startFrom != -1)
  493. {
  494. for(size_t b=startFrom; b<stacksSorted.size()+startFrom; ++b)
  495. {
  496. SDL_BlitSurface(graphics->smallImgs[-2], NULL, to, &genRect(32, 32, xPos, yPos));
  497. //printing colored border
  498. for(int xFrom = xPos-1; xFrom<xPos+33; ++xFrom)
  499. {
  500. for(int yFrom = yPos-1; yFrom<yPos+33; ++yFrom)
  501. {
  502. if(xFrom == xPos-1 || xFrom == xPos+32 || yFrom == yPos-1 || yFrom == yPos+32)
  503. {
  504. SDL_Color pc;
  505. if(stacksSorted[b % stacksSorted.size()].owner != 255)
  506. {
  507. pc = graphics->playerColors[stacksSorted[b % stacksSorted.size()].owner];
  508. }
  509. else
  510. {
  511. pc = *graphics->neutralColor;
  512. }
  513. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, xFrom, yFrom, pc.r, pc.g, pc.b);
  514. }
  515. }
  516. }
  517. //colored border printed
  518. SDL_BlitSurface(graphics->smallImgs[stacksSorted[b % stacksSorted.size()].creature->idNumber], NULL, to, &genRect(32, 32, xPos, yPos));
  519. xPos += 37;
  520. }
  521. }
  522. }
  523. //showing window with result of battle
  524. if(resWindow)
  525. {
  526. resWindow->show(to);
  527. }
  528. SDL_SetClipRect(to, &buf); //restoring previous clip_rect
  529. //printing border around interface
  530. if(screen->w != 800 || screen->h !=600)
  531. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  532. }
  533. void CBattleInterface::keyPressed(const SDL_KeyboardEvent & key)
  534. {
  535. if(key.keysym.sym == SDLK_q)
  536. {
  537. showStackQueue = key.state==SDL_PRESSED;
  538. }
  539. }
  540. void CBattleInterface::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  541. {
  542. if(activeStack>=0 && !spellDestSelectMode)
  543. {
  544. int myNumber = -1; //number of hovered tile
  545. for(int g=0; g<BFIELD_SIZE; ++g)
  546. {
  547. if(bfield[g].hovered && bfield[g].strictHovered)
  548. {
  549. myNumber = g;
  550. break;
  551. }
  552. }
  553. if(myNumber == -1)
  554. {
  555. CGI->curh->changeGraphic(1, 6);
  556. }
  557. else
  558. {
  559. if(std::find(shadedHexes.begin(),shadedHexes.end(),myNumber) == shadedHexes.end())
  560. {
  561. CStack *shere = LOCPLINT->cb->battleGetStackByPos(myNumber);
  562. if(shere)
  563. {
  564. if(shere->owner == LOCPLINT->playerID) //our stack
  565. CGI->curh->changeGraphic(1,5);
  566. else if(LOCPLINT->cb->battleCanShoot(activeStack,myNumber)) //we can shoot enemy
  567. CGI->curh->changeGraphic(1,3);
  568. else if(isTileAttackable(myNumber)) //available enemy (melee attackable)
  569. {
  570. int fromHex = previouslyHoveredHex;
  571. if(fromHex!=-1 && fromHex%17!=0 && fromHex%17!=16 && vstd::contains(shadedHexes, fromHex))
  572. {
  573. switch(BattleInfo::mutualPosition(fromHex, myNumber))
  574. {
  575. case 0:
  576. CGI->curh->changeGraphic(1,12);
  577. break;
  578. case 1:
  579. CGI->curh->changeGraphic(1,7);
  580. break;
  581. case 2:
  582. CGI->curh->changeGraphic(1,8);
  583. break;
  584. case 3:
  585. CGI->curh->changeGraphic(1,9);
  586. break;
  587. case 4:
  588. CGI->curh->changeGraphic(1,10);
  589. break;
  590. case 5:
  591. CGI->curh->changeGraphic(1,11);
  592. break;
  593. }
  594. }
  595. }
  596. else //unavailable enemy
  597. CGI->curh->changeGraphic(1,0);
  598. }
  599. else //empty unavailable tile
  600. CGI->curh->changeGraphic(1,0);
  601. }
  602. else //available tile
  603. {
  604. if(LOCPLINT->cb->battleGetStackByID(activeStack)->creature->isFlying())
  605. CGI->curh->changeGraphic(1,2);
  606. else
  607. CGI->curh->changeGraphic(1,1);
  608. }
  609. }
  610. }
  611. else if(spellDestSelectMode)
  612. {
  613. int myNumber = -1; //number of hovered tile
  614. for(int g=0; g<BFIELD_SIZE; ++g)
  615. {
  616. if(bfield[g].hovered && bfield[g].strictHovered)
  617. {
  618. myNumber = g;
  619. break;
  620. }
  621. }
  622. if(myNumber == -1)
  623. {
  624. CGI->curh->changeGraphic(1, 0);
  625. }
  626. else
  627. {
  628. switch(spellSelMode)
  629. {
  630. case 0:
  631. CGI->curh->changeGraphic(3, 0);
  632. break;
  633. case 1:
  634. if(LOCPLINT->cb->battleGetStackByPos(myNumber) && LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(myNumber)->owner )
  635. CGI->curh->changeGraphic(3, 0);
  636. else
  637. CGI->curh->changeGraphic(1, 0);
  638. break;
  639. case 2:
  640. if(LOCPLINT->cb->battleGetStackByPos(myNumber) && LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(myNumber)->owner )
  641. CGI->curh->changeGraphic(3, 0);
  642. else
  643. CGI->curh->changeGraphic(1, 0);
  644. break;
  645. case 3:
  646. if(LOCPLINT->cb->battleGetStackByPos(myNumber))
  647. CGI->curh->changeGraphic(3, 0);
  648. else
  649. CGI->curh->changeGraphic(1, 0);
  650. break;
  651. case 4: //TODO: implement this case
  652. break;
  653. }
  654. }
  655. }
  656. }
  657. bool CBattleInterface::reverseCreature(int number, int hex, bool wideTrick)
  658. {
  659. if(creAnims[number]==NULL)
  660. return false; //there is no such creature
  661. creAnims[number]->setType(8);
  662. int firstFrame = creAnims[number]->getFrame();
  663. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(8) + firstFrame - 1; ++g)
  664. {
  665. show();
  666. CSDL_Ext::update();
  667. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  668. }
  669. creDir[number] = !creDir[number];
  670. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  671. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(hex, creDir[number], curs.creature);
  672. creAnims[number]->pos.x = coords.first;
  673. //creAnims[number]->pos.y = coords.second;
  674. if(wideTrick && curs.creature->isDoubleWide())
  675. {
  676. if(curs.attackerOwned)
  677. {
  678. if(!creDir[number])
  679. creAnims[number]->pos.x -= 44;
  680. }
  681. else
  682. {
  683. if(creDir[number])
  684. creAnims[number]->pos.x += 44;
  685. }
  686. }
  687. creAnims[number]->setType(7);
  688. firstFrame = creAnims[number]->getFrame();
  689. for(int g=0; creAnims[number]->getFrame() != creAnims[number]->framesInGroup(7) + firstFrame - 1; ++g)
  690. {
  691. show();
  692. CSDL_Ext::update();
  693. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  694. }
  695. creAnims[number]->setType(2);
  696. return true;
  697. }
  698. void CBattleInterface::bOptionsf()
  699. {
  700. CGI->curh->changeGraphic(0,0);
  701. deactivate();
  702. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  703. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  704. optionsWin->activate();
  705. LOCPLINT->objsToBlit.push_back(optionsWin);
  706. }
  707. void CBattleInterface::bSurrenderf()
  708. {
  709. }
  710. void CBattleInterface::bFleef()
  711. {
  712. CFunctionList<void()> ony = boost::bind(&CBattleInterface::activate,this);
  713. ony += boost::bind(&CBattleInterface::reallyFlee,this);
  714. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, boost::bind(&CBattleInterface::activate,this), true, false);
  715. }
  716. void CBattleInterface::reallyFlee()
  717. {
  718. giveCommand(4,0,0);
  719. CGI->curh->changeGraphic(0, 0);
  720. }
  721. void CBattleInterface::bAutofightf()
  722. {
  723. }
  724. void CBattleInterface::bSpellf()
  725. {
  726. CGI->curh->changeGraphic(0,0);
  727. deactivate();
  728. const CGHeroInstance * chi = NULL;
  729. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  730. chi = attackingHeroInstance;
  731. else
  732. chi = defendingHeroInstance;
  733. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), chi);
  734. spellWindow->activate();
  735. LOCPLINT->objsToBlit.push_back(spellWindow);
  736. }
  737. void CBattleInterface::bWaitf()
  738. {
  739. giveCommand(8,0,activeStack);
  740. }
  741. void CBattleInterface::bDefencef()
  742. {
  743. giveCommand(3,0,activeStack);
  744. }
  745. void CBattleInterface::bConsoleUpf()
  746. {
  747. console->scrollUp();
  748. }
  749. void CBattleInterface::bConsoleDownf()
  750. {
  751. console->scrollDown();
  752. }
  753. void CBattleInterface::newStack(CStack stack)
  754. {
  755. creAnims[stack.ID] = new CCreatureAnimation(stack.creature->animDefName);
  756. creAnims[stack.ID]->setType(2);
  757. creDir[stack.ID] = stack.owner==attackingHeroInstance->tempOwner;
  758. }
  759. void CBattleInterface::stackRemoved(CStack stack)
  760. {
  761. delete creAnims[stack.ID];
  762. creAnims.erase(stack.ID);
  763. }
  764. void CBattleInterface::stackActivated(int number)
  765. {
  766. //givenCommand = NULL;
  767. activeStack = number;
  768. myTurn = true;
  769. redrawBackgroundWithHexes(number);
  770. bWait->block(vstd::contains(LOCPLINT->cb->battleGetStackByID(number)->state,WAITING)); //block waiting button if stack has been already waiting
  771. }
  772. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving)
  773. {
  774. bool startMoving = creAnims[number]->type==20;
  775. //a few useful variables
  776. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  777. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  778. int hexWbase = 44, hexHbase = 42;
  779. bool twoTiles = LOCPLINT->cb->battleGetCreature(number).isDoubleWide();
  780. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  781. {
  782. CGI->curh->hide();
  783. for(int i=0; i<creAnims[number]->framesInGroup(20)*getAnimSpeedMultiplier()-1; ++i)
  784. {
  785. show();
  786. CSDL_Ext::update();
  787. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  788. if((animCount+1)%(4/animSpeed)==0)
  789. creAnims[number]->incrementFrame();
  790. }
  791. }
  792. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  793. {
  794. switch(mutPos) //reverse unit if necessary
  795. {
  796. case 0: case 4: case 5:
  797. if(creDir[number] == true)
  798. reverseCreature(number, curStackPos, twoTiles);
  799. break;
  800. case 1: case 2: case 3:
  801. if(creDir[number] == false)
  802. reverseCreature(number, curStackPos, twoTiles);
  803. break;
  804. }
  805. //moving instructions
  806. creAnims[number]->setType(0);
  807. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  808. for(int i=0; i<steps; ++i)
  809. {
  810. switch(mutPos)
  811. {
  812. case 0:
  813. posX -= ((float)hexWbase)/(2.0f*steps);
  814. creAnims[number]->pos.x = posX;
  815. posY -= ((float)hexHbase)/((float)steps);
  816. creAnims[number]->pos.y = posY;
  817. break;
  818. case 1:
  819. posX += ((float)hexWbase)/(2.0f*steps);
  820. creAnims[number]->pos.x = posX;
  821. posY -= ((float)hexHbase)/((float)steps);
  822. creAnims[number]->pos.y = posY;
  823. break;
  824. case 2:
  825. posX += ((float)hexWbase)/((float)steps);
  826. creAnims[number]->pos.x = posX;
  827. break;
  828. case 3:
  829. posX += ((float)hexWbase)/(2.0f*steps);
  830. creAnims[number]->pos.x = posX;
  831. posY += ((float)hexHbase)/((float)steps);
  832. creAnims[number]->pos.y = posY;
  833. break;
  834. case 4:
  835. posX -= ((float)hexWbase)/(2.0f*steps);
  836. creAnims[number]->pos.x = posX;
  837. posY += ((float)hexHbase)/((float)steps);
  838. creAnims[number]->pos.y = posY;
  839. break;
  840. case 5:
  841. posX -= ((float)hexWbase)/((float)steps);
  842. creAnims[number]->pos.x = posX;
  843. break;
  844. }
  845. show();
  846. CSDL_Ext::update();
  847. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  848. if((animCount+1)%(4/animSpeed)==0)
  849. creAnims[number]->incrementFrame();
  850. }
  851. }
  852. if(endMoving) //animation of ending move
  853. {
  854. if(creAnims[number]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  855. {
  856. creAnims[number]->setType(21);
  857. for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  858. {
  859. show();
  860. CSDL_Ext::update();
  861. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  862. if((animCount+1)%(4/animSpeed)==0)
  863. creAnims[number]->incrementFrame();
  864. }
  865. }
  866. creAnims[number]->setType(2); //resetting to default
  867. CGI->curh->show();
  868. }
  869. CStack curs = *LOCPLINT->cb->battleGetStackByID(number);
  870. if(endMoving) //resetting to default
  871. {
  872. if(creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))
  873. reverseCreature(number, destHex, twoTiles);
  874. }
  875. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], curs.creature);
  876. creAnims[number]->pos.x = coords.first;
  877. if(!endMoving && twoTiles && (creDir[number] != (curs.owner == attackingHeroInstance->tempOwner))) //big creature is reversed
  878. creAnims[number]->pos.x -= 44;
  879. creAnims[number]->pos.y = coords.second;
  880. }
  881. void CBattleInterface::stacksAreAttacked(std::vector<CBattleInterface::SStackAttackedInfo> attackedInfos)
  882. {
  883. //restoring default state of battleWindow by calling show func
  884. while(true)
  885. {
  886. show();
  887. CSDL_Ext::update();
  888. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  889. //checking break conditions
  890. bool break_loop = true;
  891. for(size_t g=0; g<attackedInfos.size(); ++g)
  892. {
  893. if(creAnims[attackedInfos[g].ID]->getType() != 2)
  894. {
  895. break_loop = false;
  896. }
  897. if(attackingInfo && attackingInfo->IDby == attackedInfos[g].IDby)
  898. {
  899. break_loop = false;
  900. }
  901. }
  902. if(break_loop) break;
  903. }
  904. if(attackedInfos.size() == 1 && attackedInfos[0].byShooting) //delay hit animation
  905. {
  906. CStack attacker = *LOCPLINT->cb->battleGetStackByID(attackedInfos[0].IDby);
  907. while(true)
  908. {
  909. bool found = false;
  910. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  911. {
  912. if(it->creID == attacker.creature->idNumber)
  913. {
  914. found = true;
  915. break;
  916. }
  917. }
  918. if(!found)
  919. break;
  920. else
  921. {
  922. show();
  923. CSDL_Ext::update();
  924. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  925. }
  926. }
  927. }
  928. //initializing
  929. std::map<int, int> animLengths;
  930. std::map<int, int> increments;
  931. int maxLen = 0;
  932. for(size_t g=0; g<attackedInfos.size(); ++g)
  933. {
  934. int animLen;
  935. if(attackedInfos[g].killed)
  936. {
  937. creAnims[attackedInfos[g].ID]->setType(5); //death
  938. animLen = creAnims[attackedInfos[g].ID]->framesInGroup(5);
  939. }
  940. else
  941. {
  942. creAnims[attackedInfos[g].ID]->setType(3); //getting hit
  943. animLen = creAnims[attackedInfos[g].ID]->framesInGroup(3);
  944. }
  945. animLengths.insert(std::make_pair(attackedInfos[g].ID, animLen));
  946. increments.insert(std::make_pair(attackedInfos[g].ID, 0));
  947. if(animLen > maxLen)
  948. {
  949. maxLen = animLen;
  950. }
  951. }
  952. //main showing loop
  953. bool continueLoop = true;
  954. while(continueLoop)
  955. {
  956. show();
  957. CSDL_Ext::update();
  958. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  959. for(size_t g=0; g<attackedInfos.size(); ++g)
  960. {
  961. if((animCount+1)%(4/animSpeed)==0 && increments[attackedInfos[g].ID]<animLengths[attackedInfos[g].ID])
  962. {
  963. creAnims[attackedInfos[g].ID]->incrementFrame();
  964. ++(increments[attackedInfos[g].ID]);
  965. }
  966. if(increments[attackedInfos[g].ID]>=animLengths[attackedInfos[g].ID] && creAnims[attackedInfos[g].ID]->getType() == 3)
  967. creAnims[attackedInfos[g].ID]->setType(2);
  968. }
  969. bool isAnotherOne = false; //if true, there is a stack whose hit/death anim must be continued
  970. for(size_t g=0; g<attackedInfos.size(); ++g)
  971. {
  972. if(increments[attackedInfos[g].ID] < animLengths[attackedInfos[g].ID]-1)
  973. {
  974. isAnotherOne = true;
  975. break;
  976. }
  977. }
  978. if(!isAnotherOne)
  979. continueLoop = false;
  980. }
  981. //restoring animType
  982. for(size_t g=0; g<attackedInfos.size(); ++g)
  983. {
  984. if(creAnims[attackedInfos[g].ID]->getType() == 3)
  985. creAnims[attackedInfos[g].ID]->setType(2);
  986. }
  987. //printing info to console
  988. for(size_t g=0; g<attackedInfos.size(); ++g)
  989. {
  990. if(attackedInfos[g].IDby!=-1)
  991. printConsoleAttacked(attackedInfos[g].ID, attackedInfos[g].dmg, attackedInfos[g].amountKilled, attackedInfos[g].IDby);
  992. }
  993. }
  994. void CBattleInterface::stackAttacking(int ID, int dest)
  995. {
  996. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  997. {
  998. show();
  999. CSDL_Ext::update();
  1000. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1001. }
  1002. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  1003. int reversedShift = 0; //shift of attacking stack's position due to reversing
  1004. if(aStack.attackerOwned)
  1005. {
  1006. if(aStack.creature->isDoubleWide())
  1007. {
  1008. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1009. {
  1010. case 0:
  1011. //reverseCreature(ID, aStack.position, true);
  1012. break;
  1013. case 1:
  1014. break;
  1015. case 2:
  1016. break;
  1017. case 3:
  1018. break;
  1019. case 4:
  1020. //reverseCreature(ID, aStack.position, true);
  1021. break;
  1022. case 5:
  1023. reverseCreature(ID, aStack.position, true);
  1024. break;
  1025. case -1:
  1026. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1027. {
  1028. reverseCreature(ID, aStack.position, true);
  1029. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1030. }
  1031. break;
  1032. }
  1033. }
  1034. else //else for if(aStack.creature->isDoubleWide())
  1035. {
  1036. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1037. {
  1038. case 0:
  1039. reverseCreature(ID, aStack.position, true);
  1040. break;
  1041. case 1:
  1042. break;
  1043. case 2:
  1044. break;
  1045. case 3:
  1046. break;
  1047. case 4:
  1048. reverseCreature(ID, aStack.position, true);
  1049. break;
  1050. case 5:
  1051. reverseCreature(ID, aStack.position, true);
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. else //if(aStack.attackerOwned)
  1057. {
  1058. if(aStack.creature->isDoubleWide())
  1059. {
  1060. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1061. {
  1062. case 0:
  1063. //reverseCreature(ID, aStack.position, true);
  1064. break;
  1065. case 1:
  1066. break;
  1067. case 2:
  1068. reverseCreature(ID, aStack.position, true);
  1069. break;
  1070. case 3:
  1071. break;
  1072. case 4:
  1073. //reverseCreature(ID, aStack.position, true);
  1074. break;
  1075. case 5:
  1076. //reverseCreature(ID, aStack.position, true);
  1077. break;
  1078. case -1:
  1079. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1080. {
  1081. reverseCreature(ID, aStack.position, true);
  1082. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1083. }
  1084. break;
  1085. }
  1086. }
  1087. else //else for if(aStack.creature->isDoubleWide())
  1088. {
  1089. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1090. {
  1091. case 0:
  1092. //reverseCreature(ID, aStack.position, true);
  1093. break;
  1094. case 1:
  1095. reverseCreature(ID, aStack.position, true);
  1096. break;
  1097. case 2:
  1098. reverseCreature(ID, aStack.position, true);
  1099. break;
  1100. case 3:
  1101. reverseCreature(ID, aStack.position, true);
  1102. break;
  1103. case 4:
  1104. //reverseCreature(ID, aStack.position, true);
  1105. break;
  1106. case 5:
  1107. //reverseCreature(ID, aStack.position, true);
  1108. break;
  1109. }
  1110. }
  1111. }
  1112. attackingInfo = new CAttHelper;
  1113. attackingInfo->dest = dest;
  1114. attackingInfo->frame = 0;
  1115. attackingInfo->hitCount = 0;
  1116. attackingInfo->ID = ID;
  1117. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  1118. attackingInfo->reversing = false;
  1119. attackingInfo->posShiftDueToDist = reversedShift;
  1120. attackingInfo->shooting = false;
  1121. switch(BattleInfo::mutualPosition(aStack.position + reversedShift, dest)) //attack direction
  1122. {
  1123. case 0:
  1124. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1125. break;
  1126. case 1:
  1127. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1128. break;
  1129. case 2:
  1130. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1131. break;
  1132. case 3:
  1133. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1134. break;
  1135. case 4:
  1136. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1137. break;
  1138. case 5:
  1139. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1140. break;
  1141. default:
  1142. tlog1<<"Critical Error! Wrong dest in stackAttacking!"<<std::endl;
  1143. }
  1144. }
  1145. void CBattleInterface::newRound(int number)
  1146. {
  1147. console->addText(CGI->generaltexth->allTexts[412]);
  1148. }
  1149. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  1150. {
  1151. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  1152. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1153. ba->actionType = action;
  1154. ba->destinationTile = tile;
  1155. ba->stackNumber = stack;
  1156. ba->additionalInfo = additional;
  1157. givenCommand->setn(ba);
  1158. myTurn = false;
  1159. activeStack = -1;
  1160. }
  1161. bool CBattleInterface::isTileAttackable(const int & number) const
  1162. {
  1163. for(size_t b=0; b<shadedHexes.size(); ++b)
  1164. {
  1165. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  1166. return true;
  1167. }
  1168. return false;
  1169. }
  1170. void CBattleInterface::hexLclicked(int whichOne)
  1171. {
  1172. if((whichOne%BFIELD_WIDTH)!=0 && (whichOne%BFIELD_WIDTH)!=(BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  1173. {
  1174. if(!myTurn)
  1175. return; //we are not permit to do anything
  1176. if(spellDestSelectMode)
  1177. {
  1178. //checking destination
  1179. bool allowCasting = true;
  1180. switch(spellSelMode)
  1181. {
  1182. case 1:
  1183. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1184. allowCasting = false;
  1185. break;
  1186. case 2:
  1187. if(!LOCPLINT->cb->battleGetStackByPos(whichOne) || LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(whichOne)->owner )
  1188. allowCasting = false;
  1189. break;
  1190. case 3:
  1191. if(!LOCPLINT->cb->battleGetStackByPos(whichOne))
  1192. allowCasting = false;
  1193. break;
  1194. case 4: //TODO: implement this case
  1195. break;
  1196. }
  1197. //destination checked
  1198. if(allowCasting)
  1199. {
  1200. spellToCast->destinationTile = whichOne;
  1201. LOCPLINT->cb->battleMakeAction(spellToCast);
  1202. delete spellToCast;
  1203. spellToCast = NULL;
  1204. spellDestSelectMode = false;
  1205. CGI->curh->changeGraphic(1, 6);
  1206. }
  1207. }
  1208. else
  1209. {
  1210. CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  1211. if(!dest || !dest->alive()) //no creature at that tile
  1212. {
  1213. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  1214. {
  1215. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1216. giveCommand(2,whichOne,activeStack);
  1217. }
  1218. }
  1219. else if(dest->owner != attackingHeroInstance->tempOwner
  1220. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne) ) //shooting
  1221. {
  1222. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1223. giveCommand(7,whichOne,activeStack);
  1224. }
  1225. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  1226. {
  1227. switch(CGI->curh->number)
  1228. {
  1229. case 12:
  1230. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 17 : 18 ),activeStack,whichOne);
  1231. break;
  1232. case 7:
  1233. giveCommand(6,whichOne + ( (whichOne/17)%2 ? 16 : 17 ),activeStack,whichOne);
  1234. break;
  1235. case 8:
  1236. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1237. break;
  1238. case 9:
  1239. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 18 : 17 ),activeStack,whichOne);
  1240. break;
  1241. case 10:
  1242. giveCommand(6,whichOne - ( (whichOne/17)%2 ? 17 : 16 ),activeStack,whichOne);
  1243. break;
  1244. case 11:
  1245. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1246. break;
  1247. }
  1248. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1249. }
  1250. }
  1251. }
  1252. }
  1253. void CBattleInterface::stackIsShooting(int ID, int dest)
  1254. {
  1255. if(attackingInfo != NULL)
  1256. {
  1257. return; //something went wrong
  1258. }
  1259. //projectile
  1260. float projectileAngle; //in radians; if positive, projectiles goes up
  1261. 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)
  1262. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1263. projectileAngle = atan2(float(abs(dest - fromHex)/17), float(abs(dest - fromHex)%17));
  1264. if(fromHex < dest)
  1265. projectileAngle = -projectileAngle;
  1266. SProjectileInfo spi;
  1267. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1268. spi.step = 0;
  1269. spi.frameNum = 0;
  1270. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1271. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, &LOCPLINT->cb->battleGetCreature(ID));
  1272. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, &LOCPLINT->cb->battleGetCreature(ID));
  1273. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1274. if(projectileAngle > straightAngle) //upper shot
  1275. {
  1276. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1277. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1278. }
  1279. else if(projectileAngle < -straightAngle) //lower shot
  1280. {
  1281. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1282. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1283. }
  1284. else //straight shot
  1285. {
  1286. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1287. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1288. }
  1289. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1290. if(spi.lastStep == 0)
  1291. spi.lastStep = 1;
  1292. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1293. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1294. //set starting frame
  1295. if(spi.spin)
  1296. {
  1297. spi.frameNum = 0;
  1298. }
  1299. else
  1300. {
  1301. 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);
  1302. }
  1303. //set delay
  1304. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1305. projectiles.push_back(spi);
  1306. //attack aniamtion
  1307. attackingInfo = new CAttHelper;
  1308. attackingInfo->dest = dest;
  1309. attackingInfo->frame = 0;
  1310. attackingInfo->hitCount = 0;
  1311. attackingInfo->ID = ID;
  1312. attackingInfo->reversing = false;
  1313. attackingInfo->posShiftDueToDist = 0;
  1314. attackingInfo->shooting = true;
  1315. if(projectileAngle > straightAngle) //upper shot
  1316. attackingInfo->shootingGroup = 14;
  1317. else if(projectileAngle < -straightAngle) //lower shot
  1318. attackingInfo->shootingGroup = 16;
  1319. else //straight shot
  1320. attackingInfo->shootingGroup = 15;
  1321. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1322. }
  1323. void CBattleInterface::battleFinished(const BattleResult& br)
  1324. {
  1325. deactivate();
  1326. CGI->curh->changeGraphic(0,0);
  1327. SDL_Rect temp_rect = genRect(561, 470, 165, 19);
  1328. resWindow = new CBattleReslutWindow(br, temp_rect, this);
  1329. resWindow->activate();
  1330. }
  1331. void CBattleInterface::spellCasted(SpellCasted * sc)
  1332. {
  1333. std::vector< std::string > anims; //for magic arrow and ice bolt
  1334. switch(sc->id)
  1335. {
  1336. case 15: //magic arrow
  1337. {
  1338. //initialization of anims
  1339. anims.push_back("C20SPX0.DEF"); anims.push_back("C20SPX1.DEF"); anims.push_back("C20SPX2.DEF"); anims.push_back("C20SPX3.DEF"); anims.push_back("C20SPX4.DEF");
  1340. }
  1341. case 16: //ice bolt
  1342. {
  1343. if(anims.size() == 0) //initialiaztion of anims
  1344. {
  1345. anims.push_back("C08SPW0.DEF"); anims.push_back("C08SPW1.DEF"); anims.push_back("C08SPW2.DEF"); anims.push_back("C08SPW3.DEF"); anims.push_back("C08SPW4.DEF");
  1346. }
  1347. } //end of ice bolt only part
  1348. { //common ice bolt and magic arrow part
  1349. //initial variables
  1350. std::string animToDisplay;
  1351. std::pair<int, int> srccoord = sc->side ? std::make_pair(770, 60) : std::make_pair(30, 60);
  1352. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile)->creature); //position attacked by arrow
  1353. destcoord.first += 250; destcoord.second += 240;
  1354. //animation angle
  1355. float angle = atan2(float(destcoord.first - srccoord.first), float(destcoord.second - srccoord.second));
  1356. //choosing animation by angle
  1357. if(angle > 1.50)
  1358. animToDisplay = anims[0];
  1359. else if(angle > 1.20)
  1360. animToDisplay = anims[1];
  1361. else if(angle > 0.90)
  1362. animToDisplay = anims[2];
  1363. else if(angle > 0.60)
  1364. animToDisplay = anims[3];
  1365. else
  1366. animToDisplay = anims[4];
  1367. //displaying animation
  1368. int steps = sqrt((float)((destcoord.first - srccoord.first)*(destcoord.first - srccoord.first) + (destcoord.second - srccoord.second) * (destcoord.second - srccoord.second))) / 40;
  1369. if(steps <= 0)
  1370. steps = 1;
  1371. CDefHandler * animDef = CDefHandler::giveDef(animToDisplay);
  1372. int dx = (destcoord.first - srccoord.first - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.second - srccoord.second - animDef->ourImages[0].bitmap->h)/steps;
  1373. SDL_Rect buf;
  1374. SDL_GetClipRect(screen, &buf);
  1375. SDL_SetClipRect(screen, &pos); //setting rect we can blit to
  1376. for(int g=0; g<steps; ++g)
  1377. {
  1378. show();
  1379. SDL_Rect & srcr = animDef->ourImages[g%animDef->ourImages.size()].bitmap->clip_rect;
  1380. SDL_Rect dstr = genRect(srcr.h, srcr.w, srccoord.first + g*dx, srccoord.second + g*dy);
  1381. SDL_BlitSurface(animDef->ourImages[g%animDef->ourImages.size()].bitmap, &srcr, screen, &dstr);
  1382. CSDL_Ext::update();
  1383. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1384. }
  1385. SDL_SetClipRect(screen, &buf); //restoring previous clip rect
  1386. int b=0; //TODO use me
  1387. break; //for 15 and 16 cases
  1388. }
  1389. case 17: //lightning bolt
  1390. {
  1391. displayEffect(1, sc->tile);
  1392. break;
  1393. }
  1394. case 18: //implosion
  1395. {
  1396. displayEffect(10, sc->tile);
  1397. break;
  1398. }
  1399. case 27: //shield
  1400. {
  1401. displayEffect(27, sc->tile);
  1402. break;
  1403. }
  1404. case 28: //air shield
  1405. {
  1406. displayEffect(2, sc->tile);
  1407. break;
  1408. }
  1409. case 41: //bless
  1410. {
  1411. displayEffect(36, sc->tile);
  1412. break;
  1413. }
  1414. case 42: //curse
  1415. {
  1416. displayEffect(40, sc->tile);
  1417. break;
  1418. }
  1419. case 43: //bloodlust
  1420. {
  1421. displayEffect(4, sc->tile); //TODO: give better animation for this spell
  1422. break;
  1423. }
  1424. case 45: //weakness
  1425. {
  1426. displayEffect(56, sc->tile); //TODO: give better animation for this spell
  1427. break;
  1428. }
  1429. case 46: //stone skin
  1430. {
  1431. displayEffect(54, sc->tile);
  1432. break;
  1433. }
  1434. case 48: //prayer
  1435. {
  1436. displayEffect(0, sc->tile);
  1437. break;
  1438. }
  1439. case 53: //haste
  1440. {
  1441. displayEffect(31, sc->tile);
  1442. break;
  1443. }
  1444. case 54: //slow
  1445. {
  1446. displayEffect(19, sc->tile);
  1447. break;
  1448. }
  1449. case 56: //frenzy
  1450. {
  1451. displayEffect(17, sc->tile);
  1452. break;
  1453. }
  1454. case 61: //forgetfulness
  1455. {
  1456. displayEffect(42, sc->tile);
  1457. break;
  1458. }
  1459. }
  1460. }
  1461. void CBattleInterface::castThisSpell(int spellID)
  1462. {
  1463. BattleAction * ba = new BattleAction;
  1464. ba->actionType = 1;
  1465. ba->additionalInfo = spellID; //spell number
  1466. ba->destinationTile = -1;
  1467. ba->stackNumber = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? -1 : -2;
  1468. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1469. spellToCast = ba;
  1470. spellDestSelectMode = true;
  1471. //choosing possible tragets
  1472. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? attackingHeroInstance : attackingHeroInstance;
  1473. spellSelMode = 0;
  1474. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET") != std::string::npos)
  1475. {
  1476. switch(CGI->spellh->spells[spellID].positiveness)
  1477. {
  1478. case -1 :
  1479. spellSelMode = 2;
  1480. break;
  1481. case 0:
  1482. spellSelMode = 3;
  1483. break;
  1484. case 1:
  1485. spellSelMode = 1;
  1486. break;
  1487. }
  1488. }
  1489. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET_2") != std::string::npos)
  1490. {
  1491. if(castingHero && castingHero->getSpellSecLevel(spellID) < 3)
  1492. {
  1493. switch(CGI->spellh->spells[spellID].positiveness)
  1494. {
  1495. case -1 :
  1496. spellSelMode = 2;
  1497. break;
  1498. case 0:
  1499. spellSelMode = 3;
  1500. break;
  1501. case 1:
  1502. spellSelMode = 1;
  1503. break;
  1504. }
  1505. }
  1506. else
  1507. {
  1508. spellSelMode = -1;
  1509. }
  1510. }
  1511. CGI->curh->changeGraphic(3, 0);
  1512. }
  1513. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  1514. {
  1515. if(graphics->battleACToDef[effect].size() != 0)
  1516. {
  1517. SBattleEffect be;
  1518. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1519. be.frame = 0;
  1520. be.maxFrame = be.anim->ourImages.size();
  1521. be.x = 22 * ( ((destTile/BFIELD_WIDTH) + 1)%2 ) + 44 * (destTile % BFIELD_WIDTH) + 45;
  1522. be.y = 105 + 42 * (destTile/BFIELD_WIDTH);
  1523. if(effect != 1 && effect != 0)
  1524. {
  1525. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1526. be.y -= be.anim->ourImages[0].bitmap->h/2;
  1527. }
  1528. else if(effect == 1)
  1529. {
  1530. be.x -= be.anim->ourImages[0].bitmap->w;
  1531. be.y -= be.anim->ourImages[0].bitmap->h;
  1532. }
  1533. else if (effect == 0)
  1534. {
  1535. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1536. be.y -= be.anim->ourImages[0].bitmap->h;
  1537. }
  1538. battleEffects.push_back(be);
  1539. }
  1540. //battleEffects
  1541. }
  1542. void CBattleInterface::setAnimSpeed(int set)
  1543. {
  1544. animSpeed = set;
  1545. }
  1546. int CBattleInterface::getAnimSpeed() const
  1547. {
  1548. return animSpeed;
  1549. }
  1550. float CBattleInterface::getAnimSpeedMultiplier() const
  1551. {
  1552. switch(animSpeed)
  1553. {
  1554. case 1:
  1555. return 3.5f;
  1556. case 2:
  1557. return 2.2f;
  1558. case 4:
  1559. return 1.0f;
  1560. default:
  1561. return 0.0f;
  1562. }
  1563. }
  1564. void CBattleInterface::attackingShowHelper()
  1565. {
  1566. if(attackingInfo && !attackingInfo->reversing)
  1567. {
  1568. if(attackingInfo->frame == 0)
  1569. {
  1570. CStack aStack = *LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1571. if(attackingInfo->shooting)
  1572. {
  1573. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1574. }
  1575. else
  1576. {
  1577. if(aStack.creature->isDoubleWide())
  1578. {
  1579. switch(BattleInfo::mutualPosition(aStack.position+attackingInfo->posShiftDueToDist, attackingInfo->dest)) //attack direction
  1580. {
  1581. case 0:
  1582. creAnims[attackingInfo->ID]->setType(11);
  1583. break;
  1584. case 1:
  1585. creAnims[attackingInfo->ID]->setType(11);
  1586. break;
  1587. case 2:
  1588. creAnims[attackingInfo->ID]->setType(12);
  1589. break;
  1590. case 3:
  1591. creAnims[attackingInfo->ID]->setType(13);
  1592. break;
  1593. case 4:
  1594. creAnims[attackingInfo->ID]->setType(13);
  1595. break;
  1596. case 5:
  1597. creAnims[attackingInfo->ID]->setType(12);
  1598. break;
  1599. }
  1600. }
  1601. else //else for if(aStack.creature->isDoubleWide())
  1602. {
  1603. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1604. {
  1605. case 0:
  1606. creAnims[attackingInfo->ID]->setType(11);
  1607. break;
  1608. case 1:
  1609. creAnims[attackingInfo->ID]->setType(11);
  1610. break;
  1611. case 2:
  1612. creAnims[attackingInfo->ID]->setType(12);
  1613. break;
  1614. case 3:
  1615. creAnims[attackingInfo->ID]->setType(13);
  1616. break;
  1617. case 4:
  1618. creAnims[attackingInfo->ID]->setType(13);
  1619. break;
  1620. case 5:
  1621. creAnims[attackingInfo->ID]->setType(12);
  1622. break;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1628. {
  1629. attackingInfo->reversing = true;
  1630. CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1631. if(aStackp == NULL)
  1632. return;
  1633. CStack aStack = *aStackp;
  1634. if(aStack.attackerOwned)
  1635. {
  1636. if(aStack.creature->isDoubleWide())
  1637. {
  1638. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1639. {
  1640. case 0:
  1641. //reverseCreature(ID, aStack.position, true);
  1642. break;
  1643. case 1:
  1644. break;
  1645. case 2:
  1646. break;
  1647. case 3:
  1648. break;
  1649. case 4:
  1650. //reverseCreature(ID, aStack.position, true);
  1651. break;
  1652. case 5:
  1653. reverseCreature(attackingInfo->ID, aStack.position, true);
  1654. break;
  1655. case -1:
  1656. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1657. {
  1658. reverseCreature(attackingInfo->ID, aStack.position, true);
  1659. }
  1660. break;
  1661. }
  1662. }
  1663. else //else for if(aStack.creature->isDoubleWide())
  1664. {
  1665. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1666. {
  1667. case 0:
  1668. reverseCreature(attackingInfo->ID, aStack.position, true);
  1669. break;
  1670. case 1:
  1671. break;
  1672. case 2:
  1673. break;
  1674. case 3:
  1675. break;
  1676. case 4:
  1677. reverseCreature(attackingInfo->ID, aStack.position, true);
  1678. break;
  1679. case 5:
  1680. reverseCreature(attackingInfo->ID, aStack.position, true);
  1681. break;
  1682. }
  1683. }
  1684. }
  1685. else //if(aStack.attackerOwned)
  1686. {
  1687. if(aStack.creature->isDoubleWide())
  1688. {
  1689. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1690. {
  1691. case 0:
  1692. //reverseCreature(ID, aStack.position, true);
  1693. break;
  1694. case 1:
  1695. break;
  1696. case 2:
  1697. reverseCreature(attackingInfo->ID, aStack.position, true);
  1698. break;
  1699. case 3:
  1700. break;
  1701. case 4:
  1702. //reverseCreature(ID, aStack.position, true);
  1703. break;
  1704. case 5:
  1705. //reverseCreature(ID, aStack.position, true);
  1706. break;
  1707. case -1:
  1708. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1709. {
  1710. reverseCreature(attackingInfo->ID, aStack.position, true);
  1711. }
  1712. break;
  1713. }
  1714. }
  1715. else //else for if(aStack.creature->isDoubleWide())
  1716. {
  1717. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1718. {
  1719. case 0:
  1720. //reverseCreature(ID, aStack.position, true);
  1721. break;
  1722. case 1:
  1723. reverseCreature(attackingInfo->ID, aStack.position, true);
  1724. break;
  1725. case 2:
  1726. reverseCreature(attackingInfo->ID, aStack.position, true);
  1727. break;
  1728. case 3:
  1729. reverseCreature(attackingInfo->ID, aStack.position, true);
  1730. break;
  1731. case 4:
  1732. //reverseCreature(ID, aStack.position, true);
  1733. break;
  1734. case 5:
  1735. //reverseCreature(ID, aStack.position, true);
  1736. break;
  1737. }
  1738. }
  1739. }
  1740. attackingInfo->reversing = false;
  1741. creAnims[attackingInfo->ID]->setType(2);
  1742. delete attackingInfo;
  1743. attackingInfo = NULL;
  1744. }
  1745. if(attackingInfo)
  1746. {
  1747. attackingInfo->hitCount++;
  1748. if(attackingInfo->hitCount%(4/animSpeed) == 0)
  1749. attackingInfo->frame++;
  1750. }
  1751. }
  1752. }
  1753. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  1754. {
  1755. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack);
  1756. //preparating background graphic with hexes and shaded hexes
  1757. blitAt(background, 0, 0, backgroundWithHexes);
  1758. if(printCellBorders)
  1759. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  1760. if(printStackRange)
  1761. {
  1762. for(size_t m=0; m<shadedHexes.size(); ++m) //rows
  1763. {
  1764. int i = shadedHexes[m]/17; //row
  1765. int j = shadedHexes[m]%17-1; //column
  1766. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1767. int y = 86 + 42 * i;
  1768. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  1769. }
  1770. }
  1771. }
  1772. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  1773. {
  1774. char tabh[200];
  1775. CStack attacker = *LOCPLINT->cb->battleGetStackByID(IDby);
  1776. CStack defender = *LOCPLINT->cb->battleGetStackByID(ID);
  1777. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker.amount > 1 ? 377 : 376].c_str(),
  1778. (attacker.amount > 1 ? attacker.creature->namePl.c_str() : attacker.creature->nameSing.c_str()),
  1779. dmg);
  1780. if(killed > 0)
  1781. {
  1782. if(killed > 1)
  1783. {
  1784. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender.creature->namePl.c_str());
  1785. }
  1786. else //killed == 1
  1787. {
  1788. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender.creature->nameSing.c_str());
  1789. }
  1790. }
  1791. console->addText(std::string(tabh));
  1792. }
  1793. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  1794. {
  1795. if(to == NULL)
  1796. to = screen;
  1797. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  1798. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  1799. {
  1800. if(it->animStartDelay>0)
  1801. {
  1802. --(it->animStartDelay);
  1803. continue;
  1804. }
  1805. SDL_Rect dst;
  1806. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  1807. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  1808. dst.x = it->x;
  1809. dst.y = it->y;
  1810. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  1811. //actualizing projectile
  1812. ++it->step;
  1813. if(it->step == it->lastStep)
  1814. {
  1815. toBeDeleted.insert(toBeDeleted.end(), it);
  1816. }
  1817. else
  1818. {
  1819. it->x += it->dx;
  1820. it->y += it->dy;
  1821. if(it->spin)
  1822. {
  1823. ++(it->frameNum);
  1824. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  1825. }
  1826. }
  1827. }
  1828. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  1829. {
  1830. projectiles.erase(*it);
  1831. }
  1832. }
  1833. void CBattleHero::show(SDL_Surface *to)
  1834. {
  1835. //animation of flag
  1836. if(flip)
  1837. {
  1838. CSDL_Ext::blit8bppAlphaTo24bpp(
  1839. flag->ourImages[flagAnim].bitmap,
  1840. NULL,
  1841. screen,
  1842. &genRect(
  1843. flag->ourImages[flagAnim].bitmap->h,
  1844. flag->ourImages[flagAnim].bitmap->w,
  1845. 62 + pos.x,
  1846. 39 + pos.y
  1847. )
  1848. );
  1849. }
  1850. else
  1851. {
  1852. CSDL_Ext::blit8bppAlphaTo24bpp(
  1853. flag->ourImages[flagAnim].bitmap,
  1854. NULL,
  1855. screen,
  1856. &genRect(
  1857. flag->ourImages[flagAnim].bitmap->h,
  1858. flag->ourImages[flagAnim].bitmap->w,
  1859. 71 + pos.x,
  1860. 39 + pos.y
  1861. )
  1862. );
  1863. }
  1864. ++flagAnimCount;
  1865. if(flagAnimCount%4==0)
  1866. {
  1867. ++flagAnim;
  1868. flagAnim %= flag->ourImages.size();
  1869. }
  1870. //animation of hero
  1871. int tick=-1;
  1872. for(int i=0; i<dh->ourImages.size(); ++i)
  1873. {
  1874. if(dh->ourImages[i].groupNumber==phase)
  1875. ++tick;
  1876. if(tick==image)
  1877. {
  1878. SDL_Rect posb = pos;
  1879. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  1880. ++image;
  1881. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  1882. {
  1883. image = 0;
  1884. }
  1885. break;
  1886. }
  1887. }
  1888. }
  1889. void CBattleHero::activate()
  1890. {
  1891. ClickableL::activate();
  1892. }
  1893. void CBattleHero::deactivate()
  1894. {
  1895. ClickableL::deactivate();
  1896. }
  1897. void CBattleHero::setPhase(int newPhase)
  1898. {
  1899. phase = newPhase;
  1900. image = 0;
  1901. }
  1902. void CBattleHero::clickLeft(boost::logic::tribool down)
  1903. {
  1904. if(!down && myHero)
  1905. {
  1906. for(int it=0; it<BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  1907. {
  1908. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  1909. return;
  1910. }
  1911. CGI->curh->changeGraphic(0,0);
  1912. LOCPLINT->curint->deactivate();
  1913. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), myHero);
  1914. spellWindow->activate();
  1915. LOCPLINT->objsToBlit.push_back(spellWindow);
  1916. }
  1917. }
  1918. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, unsigned char player, const CGHeroInstance * hero, const CBattleInterface * owner): phase(phaseG), image(imageG), flip(flipG), flagAnim(0), myHero(hero), myOwner(owner)
  1919. {
  1920. dh = CDefHandler::giveDef( defName );
  1921. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  1922. {
  1923. if(flip)
  1924. {
  1925. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  1926. SDL_FreeSurface(dh->ourImages[i].bitmap);
  1927. dh->ourImages[i].bitmap = hlp;
  1928. }
  1929. dh->ourImages[i].bitmap = CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  1930. }
  1931. dh->alphaTransformed = true;
  1932. if(flip)
  1933. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  1934. else
  1935. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  1936. //coloring flag and adding transparency
  1937. for(int i=0; i<flag->ourImages.size(); ++i)
  1938. {
  1939. flag->ourImages[i].bitmap = CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  1940. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  1941. }
  1942. }
  1943. CBattleHero::~CBattleHero()
  1944. {
  1945. delete dh;
  1946. delete flag;
  1947. }
  1948. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CCreature * creature)
  1949. {
  1950. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  1951. ret.second = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
  1952. //counting x
  1953. if(attacker)
  1954. {
  1955. ret.first = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  1956. }
  1957. else
  1958. {
  1959. ret.first = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  1960. }
  1961. //shifting position for double - hex creatures
  1962. if(creature && creature->isDoubleWide())
  1963. {
  1964. if(attacker)
  1965. {
  1966. ret.first -= 42;
  1967. }
  1968. else
  1969. {
  1970. ret.first += 42;
  1971. }
  1972. }
  1973. //returning
  1974. return ret;
  1975. }
  1976. void CBattleHex::activate()
  1977. {
  1978. Hoverable::activate();
  1979. MotionInterested::activate();
  1980. ClickableL::activate();
  1981. ClickableR::activate();
  1982. }
  1983. void CBattleHex::deactivate()
  1984. {
  1985. Hoverable::deactivate();
  1986. MotionInterested::deactivate();
  1987. ClickableL::deactivate();
  1988. ClickableR::deactivate();
  1989. }
  1990. void CBattleHex::hover(bool on)
  1991. {
  1992. hovered = on;
  1993. Hoverable::hover(on);
  1994. if(!on && setAlterText)
  1995. {
  1996. myInterface->console->alterTxt = std::string();
  1997. setAlterText = false;
  1998. }
  1999. }
  2000. CBattleHex::CBattleHex() : myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL), setAlterText(false)
  2001. {
  2002. }
  2003. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  2004. {
  2005. if(myInterface->cellShade)
  2006. {
  2007. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  2008. {
  2009. strictHovered = false;
  2010. }
  2011. else //hovered pixel is inside hex
  2012. {
  2013. strictHovered = true;
  2014. }
  2015. }
  2016. if(hovered && strictHovered) //print attacked creature to console
  2017. {
  2018. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  2019. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  2020. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  2021. {
  2022. char tabh[160];
  2023. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  2024. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  2025. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  2026. myInterface->console->alterTxt = std::string(tabh);
  2027. setAlterText = true;
  2028. }
  2029. }
  2030. else if(setAlterText)
  2031. {
  2032. myInterface->console->alterTxt = std::string();
  2033. setAlterText = false;
  2034. }
  2035. }
  2036. void CBattleHex::clickLeft(boost::logic::tribool down)
  2037. {
  2038. if(!down && hovered && strictHovered) //we've been really clicked!
  2039. {
  2040. myInterface->hexLclicked(myNumber);
  2041. }
  2042. }
  2043. void CBattleHex::clickRight(boost::logic::tribool down)
  2044. {
  2045. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  2046. if(hovered && strictHovered && stID!=-1)
  2047. {
  2048. CStack myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  2049. if(!myst.alive()) return;
  2050. StackState *pom = NULL;
  2051. if(down)
  2052. {
  2053. pom = new StackState();
  2054. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  2055. if(h)
  2056. {
  2057. pom->attackBonus = h->getPrimSkillLevel(0);
  2058. pom->defenseBonus = h->getPrimSkillLevel(1);
  2059. pom->luck = h->getCurrentLuck();
  2060. pom->morale = h->getCurrentMorale();
  2061. }
  2062. pom->shotsLeft = myst.shots;
  2063. for(int vb=0; vb<myst.effects.size(); ++vb)
  2064. {
  2065. pom->effects.insert(myst.effects[vb].id);
  2066. }
  2067. pom->currentHealth = myst.firstHPleft;
  2068. (new CCreInfoWindow(myst.creature->idNumber,0,myst.amount,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  2069. ->activate();
  2070. }
  2071. delete pom;
  2072. }
  2073. }
  2074. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt("")
  2075. {
  2076. }
  2077. CBattleConsole::~CBattleConsole()
  2078. {
  2079. texts.clear();
  2080. }
  2081. void CBattleConsole::show(SDL_Surface * to)
  2082. {
  2083. if(alterTxt.size())
  2084. {
  2085. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2086. }
  2087. else if(texts.size())
  2088. {
  2089. if(texts.size()==1)
  2090. {
  2091. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2092. }
  2093. else
  2094. {
  2095. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2096. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  2097. }
  2098. }
  2099. }
  2100. bool CBattleConsole::addText(const std::string & text)
  2101. {
  2102. if(text.size()>70)
  2103. return false; //text too long!
  2104. int firstInToken = 0;
  2105. for(int i=0; i<text.size(); ++i) //tokenize
  2106. {
  2107. if(text[i] == 10)
  2108. {
  2109. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  2110. firstInToken = i+1;
  2111. }
  2112. }
  2113. texts.push_back( text.substr(firstInToken, text.size()) );
  2114. lastShown = texts.size()-1;
  2115. return true;
  2116. }
  2117. void CBattleConsole::eraseText(unsigned int pos)
  2118. {
  2119. if(pos < texts.size())
  2120. {
  2121. texts.erase(texts.begin() + pos);
  2122. if(lastShown == texts.size())
  2123. --lastShown;
  2124. }
  2125. }
  2126. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  2127. {
  2128. if(pos >= texts.size()) //no such pos
  2129. return;
  2130. texts[pos] = text;
  2131. }
  2132. void CBattleConsole::scrollUp(unsigned int by)
  2133. {
  2134. if(lastShown > by)
  2135. lastShown -= by;
  2136. }
  2137. void CBattleConsole::scrollDown(unsigned int by)
  2138. {
  2139. if(lastShown + by < texts.size())
  2140. lastShown += by;
  2141. }
  2142. CBattleReslutWindow::CBattleReslutWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  2143. {
  2144. this->pos = pos;
  2145. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  2146. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2147. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  2148. SDL_FreeSurface(background);
  2149. background = pom;
  2150. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleReslutWindow::bExitf,this), 549, 524, "iok6432.def", SDLK_RETURN);
  2151. if(br.winner==0) //attacker won
  2152. {
  2153. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  2154. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  2155. }
  2156. else //if(br.winner==1)
  2157. {
  2158. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  2159. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  2160. }
  2161. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  2162. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  2163. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  2164. std::string attackerName, defenderName;
  2165. if(owner->attackingHeroInstance) //a hero attacked
  2166. {
  2167. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  2168. //setting attackerName
  2169. attackerName = owner->attackingHeroInstance->name;
  2170. }
  2171. else //a monster attacked
  2172. {
  2173. int bestMonsterID = -1;
  2174. int bestPower = 0;
  2175. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  2176. {
  2177. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  2178. {
  2179. bestPower = CGI->creh->creatures[it->first].AIValue;
  2180. bestMonsterID = it->first;
  2181. }
  2182. }
  2183. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  2184. //setting attackerName
  2185. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  2186. }
  2187. if(owner->defendingHeroInstance) //a hero defended
  2188. {
  2189. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  2190. //setting defenderName
  2191. defenderName = owner->defendingHeroInstance->name;
  2192. }
  2193. else //a monster defended
  2194. {
  2195. int bestMonsterID = -1;
  2196. int bestPower = 0;
  2197. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  2198. {
  2199. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  2200. {
  2201. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  2202. bestMonsterID = it->second.first;
  2203. }
  2204. }
  2205. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  2206. //setting defenderName
  2207. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  2208. }
  2209. //printing attacker and defender's names
  2210. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  2211. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  2212. //printing casualities
  2213. for(int step = 0; step < 2; ++step)
  2214. {
  2215. if(br.casualties[step].size()==0)
  2216. {
  2217. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  2218. }
  2219. else
  2220. {
  2221. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  2222. int yPos = 344 + step*97;
  2223. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  2224. {
  2225. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  2226. std::stringstream amount;
  2227. amount<<it->second;
  2228. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  2229. xPos += 42;
  2230. }
  2231. }
  2232. }
  2233. //printing result description
  2234. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  2235. switch(br.result)
  2236. {
  2237. case 0: //normal victory
  2238. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2239. {
  2240. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  2241. }
  2242. else
  2243. {
  2244. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  2245. }
  2246. break;
  2247. case 1: //flee
  2248. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2249. {
  2250. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  2251. }
  2252. else
  2253. {
  2254. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  2255. }
  2256. break;
  2257. case 2: //surrender
  2258. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2259. {
  2260. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  2261. }
  2262. else
  2263. {
  2264. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  2265. }
  2266. break;
  2267. }
  2268. }
  2269. CBattleReslutWindow::~CBattleReslutWindow()
  2270. {
  2271. SDL_FreeSurface(background);
  2272. }
  2273. void CBattleReslutWindow::activate()
  2274. {
  2275. LOCPLINT->showingDialog->set(true);
  2276. exit->activate();
  2277. }
  2278. void CBattleReslutWindow::deactivate()
  2279. {
  2280. exit->deactivate();
  2281. }
  2282. void CBattleReslutWindow::show(SDL_Surface *to)
  2283. {
  2284. //evaluating to
  2285. if(!to)
  2286. to = screen;
  2287. SDL_BlitSurface(background, NULL, to, &pos);
  2288. exit->show(to);
  2289. }
  2290. void CBattleReslutWindow::bExitf()
  2291. {
  2292. LOCPLINT->battleResultQuited();
  2293. }
  2294. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  2295. {
  2296. pos = position;
  2297. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  2298. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2299. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[427].first)(3,CGI->generaltexth->zelp[427].first), CGI->generaltexth->zelp[427].second, false, "sysopchk.def", NULL, 185, 140, false);
  2300. viewGrid->select(owner->printCellBorders);
  2301. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[428].first)(3,CGI->generaltexth->zelp[428].first), CGI->generaltexth->zelp[428].second, false, "sysopchk.def", NULL, 185, 173, false);
  2302. movementShadow->select(owner->printStackRange);
  2303. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[429].first)(3,CGI->generaltexth->zelp[429].first), CGI->generaltexth->zelp[429].second, false, "sysopchk.def", NULL, 185, 207, false);
  2304. mouseShadow->select(owner->printMouseShadow);
  2305. animSpeeds = new CHighlightableButtonsGroup(0);
  2306. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def",188, 309, 1);
  2307. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def",252, 309, 2);
  2308. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",315, 309, 4);
  2309. animSpeeds->select(owner->getAnimSpeed(), 1);
  2310. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  2311. setToDefault = new AdventureMapButton (CGI->generaltexth->zelp[392].first, CGI->generaltexth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def");
  2312. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  2313. exit = new AdventureMapButton (CGI->generaltexth->zelp[393].first, CGI->generaltexth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def",SDLK_RETURN);
  2314. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  2315. //printing texts to background
  2316. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  2317. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  2318. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  2319. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  2320. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  2321. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  2322. //auto - combat options
  2323. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  2324. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  2325. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  2326. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  2327. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  2328. //creature info
  2329. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  2330. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  2331. //general options
  2332. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  2333. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  2334. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  2335. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  2336. //texts printed
  2337. }
  2338. CBattleOptionsWindow::~CBattleOptionsWindow()
  2339. {
  2340. SDL_FreeSurface(background);
  2341. delete setToDefault;
  2342. delete exit;
  2343. delete viewGrid;
  2344. delete movementShadow;
  2345. delete animSpeeds;
  2346. delete mouseShadow;
  2347. }
  2348. void CBattleOptionsWindow::activate()
  2349. {
  2350. setToDefault->activate();
  2351. exit->activate();
  2352. viewGrid->activate();
  2353. movementShadow->activate();
  2354. animSpeeds->activate();
  2355. mouseShadow->activate();
  2356. }
  2357. void CBattleOptionsWindow::deactivate()
  2358. {
  2359. setToDefault->deactivate();
  2360. exit->deactivate();
  2361. viewGrid->deactivate();
  2362. movementShadow->deactivate();
  2363. animSpeeds->deactivate();
  2364. mouseShadow->deactivate();
  2365. }
  2366. void CBattleOptionsWindow::show(SDL_Surface *to)
  2367. {
  2368. if(!to) //"evaluating" to
  2369. to = screen;
  2370. SDL_BlitSurface(background, NULL, to, &pos);
  2371. setToDefault->show(to);
  2372. exit->show(to);
  2373. viewGrid->show(to);
  2374. movementShadow->show(to);
  2375. animSpeeds->show(to);
  2376. mouseShadow->show(to);
  2377. }
  2378. void CBattleOptionsWindow::bDefaultf()
  2379. {
  2380. }
  2381. void CBattleOptionsWindow::bExitf()
  2382. {
  2383. deactivate();
  2384. for(size_t g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  2385. {
  2386. if(dynamic_cast<CBattleOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  2387. {
  2388. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  2389. break;
  2390. }
  2391. }
  2392. delete this;
  2393. LOCPLINT->curint->activate();
  2394. }