CBattleInterface.cpp 88 KB

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