CBattleInterface.cpp 95 KB

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