CBattleInterface.cpp 95 KB

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