CBattleInterface.cpp 93 KB

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