CBattleInterface.cpp 75 KB

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