CBattleInterface.cpp 101 KB

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