CBattleInterface.cpp 110 KB

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