CBattleInterface.cpp 92 KB

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