CBattleInterface.cpp 107 KB

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