CBattleInterface.cpp 94 KB

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