CBattleInterface.cpp 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995
  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. if( LOCPLINT->cb->battleCanFlee() )
  907. {
  908. CFunctionList<void()> ony = boost::bind(&CBattleInterface::reallyFlee,this);
  909. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, 0, false);
  910. }
  911. else
  912. {
  913. std::vector<SComponent*> comps;
  914. std::string heroName;
  915. //calculating fleeing hero's name
  916. if(attackingHeroInstance)
  917. if(attackingHeroInstance->tempOwner == LOCPLINT->cb->getMyColor())
  918. heroName = attackingHeroInstance->name;
  919. if(defendingHeroInstance)
  920. if(defendingHeroInstance->tempOwner == LOCPLINT->cb->getMyColor())
  921. heroName = defendingHeroInstance->name;
  922. //calculating text
  923. char buffer[1000];
  924. sprintf(buffer, CGI->generaltexth->allTexts[340].c_str(), heroName.c_str());
  925. //printing message
  926. LOCPLINT->showInfoDialog(std::string(buffer), comps);
  927. }
  928. }
  929. void CBattleInterface::reallyFlee()
  930. {
  931. giveCommand(4,0,0);
  932. CGI->curh->changeGraphic(0, 0);
  933. }
  934. void CBattleInterface::bAutofightf()
  935. {
  936. }
  937. void CBattleInterface::bSpellf()
  938. {
  939. CGI->curh->changeGraphic(0,0);
  940. const CGHeroInstance * chi = NULL;
  941. if(attackingHeroInstance->tempOwner == LOCPLINT->playerID)
  942. chi = attackingHeroInstance;
  943. else
  944. chi = defendingHeroInstance;
  945. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), chi);
  946. GH.pushInt(spellWindow);
  947. }
  948. void CBattleInterface::bWaitf()
  949. {
  950. if(activeStack != -1)
  951. giveCommand(8,0,activeStack);
  952. }
  953. void CBattleInterface::bDefencef()
  954. {
  955. if(activeStack != -1)
  956. giveCommand(3,0,activeStack);
  957. }
  958. void CBattleInterface::bConsoleUpf()
  959. {
  960. console->scrollUp();
  961. }
  962. void CBattleInterface::bConsoleDownf()
  963. {
  964. console->scrollDown();
  965. }
  966. void CBattleInterface::newStack(int stackID)
  967. {
  968. const CStack * newStack = LOCPLINT->cb->battleGetStackByID(stackID);
  969. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(newStack->position, newStack->owner == attackingHeroInstance->tempOwner, newStack);
  970. creAnims[stackID] = (new CCreatureAnimation(newStack->creature->animDefName));
  971. creAnims[stackID]->setType(2);
  972. creAnims[stackID]->pos = genRect(creAnims[newStack->ID]->fullHeight, creAnims[newStack->ID]->fullWidth, coords.first, coords.second);
  973. creDir[stackID] = newStack->owner == attackingHeroInstance->tempOwner;
  974. }
  975. void CBattleInterface::stackRemoved(int stackID)
  976. {
  977. delete creAnims[stackID];
  978. creAnims.erase(stackID);
  979. }
  980. void CBattleInterface::stackActivated(int number)
  981. {
  982. //givenCommand = NULL;
  983. activeStack = number;
  984. myTurn = true;
  985. redrawBackgroundWithHexes(number);
  986. bWait->block(vstd::contains(LOCPLINT->cb->battleGetStackByID(number)->state,WAITING)); //block waiting button if stack has been already waiting
  987. //block cast spell button if hero doesn't have a spellbook
  988. if(attackingHeroInstance && attackingHeroInstance->tempOwner==LOCPLINT->cb->battleGetStackByID(number)->owner)
  989. {
  990. if(!attackingHeroInstance->getArt(17)) //don't unlock if already locked
  991. bSpell->block(!attackingHeroInstance->getArt(17));
  992. }
  993. else if(defendingHeroInstance && defendingHeroInstance->tempOwner==LOCPLINT->cb->battleGetStackByID(number)->owner)
  994. {
  995. if(!defendingHeroInstance->getArt(17)) //don't unlock if already locked
  996. bSpell->block(!defendingHeroInstance->getArt(17));
  997. }
  998. }
  999. void CBattleInterface::stackMoved(int number, int destHex, bool endMoving, int distance)
  1000. {
  1001. bool startMoving = creAnims[number]->getType()==20;
  1002. //a few useful variables
  1003. int curStackPos = LOCPLINT->cb->battleGetPos(number);
  1004. int steps = creAnims[number]->framesInGroup(0)*getAnimSpeedMultiplier()-1;
  1005. int hexWbase = 44, hexHbase = 42;
  1006. const CStack * movedStack = LOCPLINT->cb->battleGetStackByID(number);
  1007. bool twoTiles = movedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE);
  1008. std::pair<int, int> begPosition = CBattleHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack);
  1009. std::pair<int, int> endPosition = CBattleHex::getXYUnitAnim(destHex, movedStack->attackerOwned, movedStack);
  1010. if(startMoving) //animation of starting move; some units don't have this animation (ie. halberdier)
  1011. {
  1012. if (movedStack->creature->sounds.startMoving)
  1013. CGI->soundh->playSound(movedStack->creature->sounds.startMoving);
  1014. handleStartMoving(number);
  1015. }
  1016. if(moveStarted)
  1017. {
  1018. moveSh = CGI->soundh->playSound(movedStack->creature->sounds.move, -1);
  1019. CGI->curh->hide();
  1020. creAnims[number]->setType(0);
  1021. moveStarted = false;
  1022. }
  1023. int mutPos = BattleInfo::mutualPosition(curStackPos, destHex);
  1024. float stepX=0.0, stepY=0.0; //how far stack is moved in one frame; calculated later
  1025. //reverse unit if necessary
  1026. if((begPosition.first > endPosition.first) && creDir[number] == true)
  1027. {
  1028. reverseCreature(number, curStackPos, twoTiles);
  1029. }
  1030. else if ((begPosition.first < endPosition.first) && creDir[number] == false)
  1031. {
  1032. reverseCreature(number, curStackPos, twoTiles);
  1033. }
  1034. if(creAnims[number]->getType() != 0)
  1035. {
  1036. creAnims[number]->setType(0);
  1037. }
  1038. //unit reversed
  1039. //step shift calculation
  1040. float posX = creAnims[number]->pos.x, posY = creAnims[number]->pos.y; // for precise calculations ;]
  1041. if(mutPos == -1 && movedStack->hasFeatureOfType(StackFeature::FLYING))
  1042. {
  1043. steps *= distance;
  1044. stepX = (endPosition.first - (float)begPosition.first)/steps;
  1045. stepY = (endPosition.second - (float)begPosition.second)/steps;
  1046. }
  1047. else
  1048. {
  1049. switch(mutPos)
  1050. {
  1051. case 0:
  1052. stepX = (-1.0)*((float)hexWbase)/(2.0f*steps);
  1053. stepY = (-1.0)*((float)hexHbase)/((float)steps);
  1054. break;
  1055. case 1:
  1056. stepX = ((float)hexWbase)/(2.0f*steps);
  1057. stepY = (-1.0)*((float)hexHbase)/((float)steps);
  1058. break;
  1059. case 2:
  1060. stepX = ((float)hexWbase)/((float)steps);
  1061. stepY = 0.0;
  1062. break;
  1063. case 3:
  1064. stepX = ((float)hexWbase)/(2.0f*steps);
  1065. stepY = ((float)hexHbase)/((float)steps);
  1066. break;
  1067. case 4:
  1068. stepX = (-1.0)*((float)hexWbase)/(2.0f*steps);
  1069. stepY = ((float)hexHbase)/((float)steps);
  1070. break;
  1071. case 5:
  1072. stepX = (-1.0)*((float)hexWbase)/((float)steps);
  1073. stepY = 0.0;
  1074. break;
  1075. }
  1076. }
  1077. //step shifts calculated
  1078. //switch(mutPos) //reverse unit if necessary
  1079. //{
  1080. //case 0: case 4: case 5:
  1081. // if(creDir[number] == true)
  1082. // reverseCreature(number, curStackPos, twoTiles);
  1083. // break;
  1084. //case 1: case 2: case 3:
  1085. // if(creDir[number] == false)
  1086. // reverseCreature(number, curStackPos, twoTiles);
  1087. // break;
  1088. //}
  1089. //moving instructions
  1090. for(int i=0; i<steps; ++i)
  1091. {
  1092. posX += stepX;
  1093. creAnims[number]->pos.x = posX;
  1094. posY += stepY;
  1095. creAnims[number]->pos.y = posY;
  1096. show(screen);
  1097. CSDL_Ext::update(screen);
  1098. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1099. }
  1100. //unit moved
  1101. if(endMoving)
  1102. {
  1103. handleEndOfMove(number, destHex);
  1104. }
  1105. std::pair <int, int> coords = CBattleHex::getXYUnitAnim(destHex, creDir[number], movedStack);
  1106. creAnims[number]->pos.x = coords.first;
  1107. if(!endMoving && twoTiles && (movedStack->owner == attackingHeroInstance->tempOwner) && (creDir[number] != (movedStack->owner == attackingHeroInstance->tempOwner))) //big attacker creature is reversed
  1108. creAnims[number]->pos.x -= 44;
  1109. else if(!endMoving && twoTiles && (movedStack->owner != attackingHeroInstance->tempOwner) && (creDir[number] != (movedStack->owner == attackingHeroInstance->tempOwner))) //big defender creature is reversed
  1110. creAnims[number]->pos.x += 44;
  1111. creAnims[number]->pos.y = coords.second;
  1112. }
  1113. void CBattleInterface::stacksAreAttacked(std::vector<CBattleInterface::SStackAttackedInfo> attackedInfos)
  1114. {
  1115. //restoring default state of battleWindow by calling show func
  1116. while(true)
  1117. {
  1118. show(screen);
  1119. CSDL_Ext::update();
  1120. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1121. //checking break conditions
  1122. bool break_loop = true;
  1123. for(size_t g=0; g<attackedInfos.size(); ++g)
  1124. {
  1125. if(creAnims[attackedInfos[g].ID]->getType() != 2)
  1126. {
  1127. break_loop = false;
  1128. }
  1129. if(attackingInfo && attackingInfo->IDby == attackedInfos[g].IDby)
  1130. {
  1131. break_loop = false;
  1132. }
  1133. }
  1134. if(break_loop) break;
  1135. }
  1136. if(attackedInfos.size() == 1 && attackedInfos[0].byShooting) //delay hit animation
  1137. {
  1138. CStack attacker = *LOCPLINT->cb->battleGetStackByID(attackedInfos[0].IDby);
  1139. while(true)
  1140. {
  1141. bool found = false;
  1142. for(std::list<SProjectileInfo>::const_iterator it = projectiles.begin(); it!=projectiles.end(); ++it)
  1143. {
  1144. if(it->creID == attacker.creature->idNumber)
  1145. {
  1146. found = true;
  1147. break;
  1148. }
  1149. }
  1150. if(!found)
  1151. break;
  1152. else
  1153. {
  1154. show(screen);
  1155. CSDL_Ext::update(screen);
  1156. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1157. }
  1158. }
  1159. }
  1160. //initializing
  1161. int maxLen = 0;
  1162. for(size_t g=0; g<attackedInfos.size(); ++g)
  1163. {
  1164. const CStack * attacked = LOCPLINT->cb->battleGetStackByID(attackedInfos[g].ID, false);
  1165. if(attackedInfos[g].killed)
  1166. {
  1167. CGI->soundh->playSound(attacked->creature->sounds.killed);
  1168. creAnims[attackedInfos[g].ID]->setType(5); //death
  1169. }
  1170. else
  1171. {
  1172. // TODO: this block doesn't seems correct if the unit is defending.
  1173. CGI->soundh->playSound(attacked->creature->sounds.wince);
  1174. creAnims[attackedInfos[g].ID]->setType(3); //getting hit
  1175. }
  1176. }
  1177. //main showing loop
  1178. bool continueLoop = true;
  1179. while(continueLoop)
  1180. {
  1181. show(screen);
  1182. CSDL_Ext::update(screen);
  1183. SDL_Delay(5);
  1184. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1185. for(size_t g=0; g<attackedInfos.size(); ++g)
  1186. {
  1187. if((animCount+1)%(4/settings.animSpeed)==0 && !creAnims[attackedInfos[g].ID]->onLastFrameInGroup())
  1188. {
  1189. creAnims[attackedInfos[g].ID]->incrementFrame();
  1190. }
  1191. if(creAnims[attackedInfos[g].ID]->onLastFrameInGroup() && creAnims[attackedInfos[g].ID]->getType() == 3)
  1192. creAnims[attackedInfos[g].ID]->setType(2);
  1193. }
  1194. bool isAnotherOne = false; //if true, there is a stack whose hit/death anim must be continued
  1195. for(size_t g=0; g<attackedInfos.size(); ++g)
  1196. {
  1197. if(!creAnims[attackedInfos[g].ID]->onLastFrameInGroup())
  1198. {
  1199. isAnotherOne = true;
  1200. break;
  1201. }
  1202. }
  1203. if(!isAnotherOne)
  1204. continueLoop = false;
  1205. }
  1206. //restoring animType
  1207. for(size_t g=0; g<attackedInfos.size(); ++g)
  1208. {
  1209. if(creAnims[attackedInfos[g].ID]->getType() == 3)
  1210. creAnims[attackedInfos[g].ID]->setType(2);
  1211. }
  1212. //printing info to console
  1213. for(size_t g=0; g<attackedInfos.size(); ++g)
  1214. {
  1215. if(attackedInfos[g].IDby!=-1)
  1216. printConsoleAttacked(attackedInfos[g].ID, attackedInfos[g].dmg, attackedInfos[g].amountKilled, attackedInfos[g].IDby);
  1217. }
  1218. }
  1219. void CBattleInterface::stackAttacking(int ID, int dest)
  1220. {
  1221. if(attackingInfo == NULL && creAnims[ID]->getType() == 20)
  1222. {
  1223. handleStartMoving(ID);
  1224. creAnims[ID]->setType(2);
  1225. }
  1226. while(attackingInfo != NULL || creAnims[ID]->getType()!=2)
  1227. {
  1228. show(screen);
  1229. CSDL_Ext::update(screen);
  1230. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1231. }
  1232. CStack aStack = *LOCPLINT->cb->battleGetStackByID(ID); //attacking stack
  1233. int reversedShift = 0; //shift of attacking stack's position due to reversing
  1234. if(aStack.attackerOwned)
  1235. {
  1236. if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1237. {
  1238. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1239. {
  1240. case 0:
  1241. //reverseCreature(ID, aStack.position, true);
  1242. break;
  1243. case 1:
  1244. break;
  1245. case 2:
  1246. break;
  1247. case 3:
  1248. break;
  1249. case 4:
  1250. //reverseCreature(ID, aStack.position, true);
  1251. break;
  1252. case 5:
  1253. reverseCreature(ID, aStack.position, true);
  1254. break;
  1255. case -1:
  1256. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1257. {
  1258. reverseCreature(ID, aStack.position, true);
  1259. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1260. }
  1261. break;
  1262. }
  1263. }
  1264. else //else for if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1265. {
  1266. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1267. {
  1268. case 0:
  1269. reverseCreature(ID, aStack.position, true);
  1270. break;
  1271. case 1:
  1272. break;
  1273. case 2:
  1274. break;
  1275. case 3:
  1276. break;
  1277. case 4:
  1278. reverseCreature(ID, aStack.position, true);
  1279. break;
  1280. case 5:
  1281. reverseCreature(ID, aStack.position, true);
  1282. break;
  1283. }
  1284. }
  1285. }
  1286. else //if(aStack.attackerOwned)
  1287. {
  1288. if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1289. {
  1290. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1291. {
  1292. case 0:
  1293. //reverseCreature(ID, aStack.position, true);
  1294. break;
  1295. case 1:
  1296. break;
  1297. case 2:
  1298. reverseCreature(ID, aStack.position, true);
  1299. break;
  1300. case 3:
  1301. break;
  1302. case 4:
  1303. //reverseCreature(ID, aStack.position, true);
  1304. break;
  1305. case 5:
  1306. //reverseCreature(ID, aStack.position, true);
  1307. break;
  1308. case -1:
  1309. if(BattleInfo::mutualPosition(aStack.position + (aStack.attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  1310. {
  1311. reverseCreature(ID, aStack.position, true);
  1312. reversedShift = (aStack.attackerOwned ? -1 : 1);
  1313. }
  1314. break;
  1315. }
  1316. }
  1317. else //else for if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1318. {
  1319. switch(BattleInfo::mutualPosition(aStack.position, dest)) //attack direction
  1320. {
  1321. case 0:
  1322. //reverseCreature(ID, aStack.position, true);
  1323. break;
  1324. case 1:
  1325. reverseCreature(ID, aStack.position, true);
  1326. break;
  1327. case 2:
  1328. reverseCreature(ID, aStack.position, true);
  1329. break;
  1330. case 3:
  1331. reverseCreature(ID, aStack.position, true);
  1332. break;
  1333. case 4:
  1334. //reverseCreature(ID, aStack.position, true);
  1335. break;
  1336. case 5:
  1337. //reverseCreature(ID, aStack.position, true);
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. attackingInfo = new CAttHelper;
  1343. attackingInfo->dest = dest;
  1344. attackingInfo->frame = 0;
  1345. attackingInfo->hitCount = 0;
  1346. attackingInfo->ID = ID;
  1347. attackingInfo->IDby = LOCPLINT->cb->battleGetStackByPos(dest)->ID;
  1348. attackingInfo->reversing = false;
  1349. attackingInfo->posShiftDueToDist = reversedShift;
  1350. attackingInfo->shooting = false;
  1351. attackingInfo->sh = -1;
  1352. switch(BattleInfo::mutualPosition(aStack.position + reversedShift, dest)) //attack direction
  1353. {
  1354. case 0:
  1355. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1356. break;
  1357. case 1:
  1358. attackingInfo->maxframe = creAnims[ID]->framesInGroup(11);
  1359. break;
  1360. case 2:
  1361. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1362. break;
  1363. case 3:
  1364. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1365. break;
  1366. case 4:
  1367. attackingInfo->maxframe = creAnims[ID]->framesInGroup(13);
  1368. break;
  1369. case 5:
  1370. attackingInfo->maxframe = creAnims[ID]->framesInGroup(12);
  1371. break;
  1372. default:
  1373. tlog1<<"Critical Error! Wrong dest in stackAttacking! dest: "<<dest<<" attacking stack pos: "<<aStack.position<<" reversed shift: "<<reversedShift<<std::endl;
  1374. }
  1375. }
  1376. void CBattleInterface::newRound(int number)
  1377. {
  1378. console->addText(CGI->generaltexth->allTexts[412]);
  1379. //unlock spellbook
  1380. bSpell->block(!LOCPLINT->cb->battleCanCastSpell());
  1381. }
  1382. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  1383. {
  1384. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  1385. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1386. ba->actionType = action;
  1387. ba->destinationTile = tile;
  1388. ba->stackNumber = stack;
  1389. ba->additionalInfo = additional;
  1390. givenCommand->setn(ba);
  1391. myTurn = false;
  1392. activeStack = -1;
  1393. }
  1394. bool CBattleInterface::isTileAttackable(const int & number) const
  1395. {
  1396. for(size_t b=0; b<shadedHexes.size(); ++b)
  1397. {
  1398. if(BattleInfo::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  1399. return true;
  1400. }
  1401. return false;
  1402. }
  1403. void CBattleInterface::handleEndOfMove(int stackNumber, int destinationTile)
  1404. {
  1405. const CStack * movedStack = LOCPLINT->cb->battleGetStackByID(stackNumber);
  1406. if(!movedStack)
  1407. return;
  1408. if(creAnims[stackNumber]->framesInGroup(21)!=0) // some units don't have this animation (ie. halberdier)
  1409. {
  1410. if (movedStack->creature->sounds.endMoving)
  1411. {
  1412. CGI->soundh->playSound(movedStack->creature->sounds.endMoving);
  1413. }
  1414. creAnims[stackNumber]->setType(21);
  1415. //for(int i=0; i<creAnims[number]->framesInGroup(21)*getAnimSpeedMultiplier()-1; ++i)
  1416. while(!creAnims[stackNumber]->onLastFrameInGroup())
  1417. {
  1418. show(screen);
  1419. CSDL_Ext::update(screen);
  1420. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1421. }
  1422. }
  1423. creAnims[stackNumber]->setType(2); //resetting to default
  1424. CGI->curh->show();
  1425. CGI->soundh->stopSound(moveSh);
  1426. if(creDir[stackNumber] != (movedStack->owner == attackingHeroInstance->tempOwner))
  1427. reverseCreature(stackNumber, destinationTile, movedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE));
  1428. }
  1429. void CBattleInterface::hexLclicked(int whichOne)
  1430. {
  1431. if((whichOne%BFIELD_WIDTH)!=0 && (whichOne%BFIELD_WIDTH)!=(BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  1432. {
  1433. if(!myTurn)
  1434. return; //we are not permit to do anything
  1435. if(spellDestSelectMode)
  1436. {
  1437. //checking destination
  1438. bool allowCasting = true;
  1439. bool onlyAlive = spellToCast->additionalInfo != 38 && spellToCast->additionalInfo != 39; //when casting resurrection or animate dead we should be allow to select dead stack
  1440. switch(spellSelMode)
  1441. {
  1442. case 1:
  1443. if(!LOCPLINT->cb->battleGetStackByPos(whichOne, onlyAlive) || LOCPLINT->playerID != LOCPLINT->cb->battleGetStackByPos(whichOne, onlyAlive)->owner )
  1444. allowCasting = false;
  1445. break;
  1446. case 2:
  1447. if(!LOCPLINT->cb->battleGetStackByPos(whichOne, onlyAlive) || LOCPLINT->playerID == LOCPLINT->cb->battleGetStackByPos(whichOne, onlyAlive)->owner )
  1448. allowCasting = false;
  1449. break;
  1450. case 3:
  1451. if(!LOCPLINT->cb->battleGetStackByPos(whichOne, onlyAlive))
  1452. allowCasting = false;
  1453. break;
  1454. case 4: //TODO: implement this case
  1455. break;
  1456. }
  1457. //destination checked
  1458. if(allowCasting)
  1459. {
  1460. spellToCast->destinationTile = whichOne;
  1461. LOCPLINT->cb->battleMakeAction(spellToCast);
  1462. endCastingSpell();
  1463. }
  1464. }
  1465. else
  1466. {
  1467. const CStack* dest = LOCPLINT->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  1468. if(!dest || !dest->alive()) //no creature at that tile
  1469. {
  1470. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  1471. {
  1472. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1473. if(LOCPLINT->cb->battleGetStackByID(activeStack)->hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1474. {
  1475. std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
  1476. int shiftedDest = whichOne + (LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned ? 1 : -1);
  1477. if(vstd::contains(acc, whichOne))
  1478. giveCommand(2,whichOne,activeStack);
  1479. else if(vstd::contains(acc, shiftedDest))
  1480. giveCommand(2,shiftedDest,activeStack);
  1481. }
  1482. else
  1483. {
  1484. giveCommand(2,whichOne,activeStack);
  1485. }
  1486. }
  1487. }
  1488. else if(dest->owner != attackingHeroInstance->tempOwner
  1489. && LOCPLINT->cb->battleCanShoot(activeStack, whichOne) ) //shooting
  1490. {
  1491. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1492. giveCommand(7,whichOne,activeStack);
  1493. }
  1494. else if(dest->owner != attackingHeroInstance->tempOwner) //attacking
  1495. {
  1496. switch(CGI->curh->number)
  1497. {
  1498. case 12: //from bottom right
  1499. {
  1500. int destHex = whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH+1 );
  1501. if(vstd::contains(shadedHexes, destHex))
  1502. giveCommand(6,destHex,activeStack,whichOne);
  1503. else if(attackingHeroInstance->tempOwner == LOCPLINT->cb->getMyColor()) //if we are attacker
  1504. {
  1505. if(vstd::contains(shadedHexes, destHex+1))
  1506. giveCommand(6,destHex+1,activeStack,whichOne);
  1507. }
  1508. else //if we are defneder
  1509. {
  1510. if(vstd::contains(shadedHexes, destHex-1))
  1511. giveCommand(6,destHex-1,activeStack,whichOne);
  1512. }
  1513. break;
  1514. }
  1515. case 7: //from bottom left
  1516. {
  1517. int destHex = whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH-1 : BFIELD_WIDTH );
  1518. if(vstd::contains(shadedHexes, destHex))
  1519. giveCommand(6,destHex,activeStack,whichOne);
  1520. else if(attackingHeroInstance->tempOwner == LOCPLINT->cb->getMyColor()) //if we are attacker
  1521. {
  1522. if(vstd::contains(shadedHexes, destHex+1))
  1523. giveCommand(6,destHex+1,activeStack,whichOne);
  1524. }
  1525. else //if we are defneder
  1526. {
  1527. if(vstd::contains(shadedHexes, destHex-1))
  1528. giveCommand(6,destHex-1,activeStack,whichOne);
  1529. }
  1530. break;
  1531. }
  1532. case 8: //from left
  1533. if(LOCPLINT->cb->battleGetStackByID(activeStack)->hasFeatureOfType(StackFeature::DOUBLE_WIDE) && !LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
  1534. {
  1535. std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
  1536. if(vstd::contains(acc, whichOne))
  1537. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1538. else
  1539. giveCommand(6,whichOne - 2,activeStack,whichOne);
  1540. }
  1541. else
  1542. {
  1543. giveCommand(6,whichOne - 1,activeStack,whichOne);
  1544. }
  1545. break;
  1546. case 9: //from top left
  1547. {
  1548. int destHex = whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH+1 : BFIELD_WIDTH );
  1549. if(vstd::contains(shadedHexes, destHex))
  1550. giveCommand(6,destHex,activeStack,whichOne);
  1551. else if(attackingHeroInstance->tempOwner == LOCPLINT->cb->getMyColor()) //if we are attacker
  1552. {
  1553. if(vstd::contains(shadedHexes, destHex+1))
  1554. giveCommand(6,destHex+1,activeStack,whichOne);
  1555. }
  1556. else //if we are defneder
  1557. {
  1558. if(vstd::contains(shadedHexes, destHex-1))
  1559. giveCommand(6,destHex-1,activeStack,whichOne);
  1560. }
  1561. break;
  1562. }
  1563. case 10: //from top right
  1564. {
  1565. int destHex = whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH-1 );
  1566. if(vstd::contains(shadedHexes, destHex))
  1567. giveCommand(6,destHex,activeStack,whichOne);
  1568. else if(attackingHeroInstance->tempOwner == LOCPLINT->cb->getMyColor()) //if we are attacker
  1569. {
  1570. if(vstd::contains(shadedHexes, destHex+1))
  1571. giveCommand(6,destHex+1,activeStack,whichOne);
  1572. }
  1573. else //if we are defneder
  1574. {
  1575. if(vstd::contains(shadedHexes, destHex-1))
  1576. giveCommand(6,destHex-1,activeStack,whichOne);
  1577. }
  1578. break;
  1579. }
  1580. case 11: //from right
  1581. if(LOCPLINT->cb->battleGetStackByID(activeStack)->hasFeatureOfType(StackFeature::DOUBLE_WIDE) && LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
  1582. {
  1583. std::vector<int> acc = LOCPLINT->cb->battleGetAvailableHexes(activeStack, false);
  1584. if(vstd::contains(acc, whichOne))
  1585. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1586. else
  1587. giveCommand(6,whichOne + 2,activeStack,whichOne);
  1588. }
  1589. else
  1590. {
  1591. giveCommand(6,whichOne + 1,activeStack,whichOne);
  1592. }
  1593. break;
  1594. }
  1595. CGI->curh->changeGraphic(1, 6); //cursor should be changed
  1596. }
  1597. }
  1598. }
  1599. }
  1600. void CBattleInterface::stackIsShooting(int ID, int dest)
  1601. {
  1602. if(attackingInfo != NULL)
  1603. {
  1604. return; //something went wrong
  1605. }
  1606. //projectile
  1607. float projectileAngle; //in radians; if positive, projectiles goes up
  1608. float straightAngle = 0.2f; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  1609. int fromHex = LOCPLINT->cb->battleGetPos(ID);
  1610. projectileAngle = atan2(float(abs(dest - fromHex)/BFIELD_WIDTH), float(abs(dest - fromHex)%BFIELD_WIDTH));
  1611. if(fromHex < dest)
  1612. projectileAngle = -projectileAngle;
  1613. SProjectileInfo spi;
  1614. spi.creID = LOCPLINT->cb->battleGetStackByID(ID)->creature->idNumber;
  1615. spi.reverse = !LOCPLINT->cb->battleGetStackByID(ID)->attackerOwned;
  1616. spi.step = 0;
  1617. spi.frameNum = 0;
  1618. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  1619. std::pair<int, int> xycoord = CBattleHex::getXYUnitAnim(LOCPLINT->cb->battleGetPos(ID), true, LOCPLINT->cb->battleGetStackByID(ID));
  1620. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(dest, false, LOCPLINT->cb->battleGetStackByID(ID));
  1621. destcoord.first += 250; destcoord.second += 210; //TODO: find a better place to shoot
  1622. if(projectileAngle > straightAngle) //upper shot
  1623. {
  1624. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetX;
  1625. spi.y = xycoord.second + 100 - LOCPLINT->cb->battleGetCreature(ID).upperRightMissleOffsetY;
  1626. }
  1627. else if(projectileAngle < -straightAngle) //lower shot
  1628. {
  1629. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetX;
  1630. spi.y = xycoord.second + 150 - LOCPLINT->cb->battleGetCreature(ID).lowerRightMissleOffsetY;
  1631. }
  1632. else //straight shot
  1633. {
  1634. spi.x = xycoord.first + 200 + LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetX;
  1635. spi.y = xycoord.second + 125 - LOCPLINT->cb->battleGetCreature(ID).rightMissleOffsetY;
  1636. }
  1637. spi.lastStep = sqrt((float)((destcoord.first - spi.x)*(destcoord.first - spi.x) + (destcoord.second - spi.y) * (destcoord.second - spi.y))) / 40;
  1638. if(spi.lastStep == 0)
  1639. spi.lastStep = 1;
  1640. spi.dx = (destcoord.first - spi.x) / spi.lastStep;
  1641. spi.dy = (destcoord.second - spi.y) / spi.lastStep;
  1642. //set starting frame
  1643. if(spi.spin)
  1644. {
  1645. spi.frameNum = 0;
  1646. }
  1647. else
  1648. {
  1649. spi.frameNum = ((M_PI/2.0f - projectileAngle) / (2.0f *M_PI) + 1/((float)(2*(idToProjectile[spi.creID]->ourImages.size()-1)))) * (idToProjectile[spi.creID]->ourImages.size()-1);
  1650. }
  1651. //set delay
  1652. spi.animStartDelay = CGI->creh->creatures[spi.creID].attackClimaxFrame;
  1653. projectiles.push_back(spi);
  1654. //attack aniamtion
  1655. attackingInfo = new CAttHelper;
  1656. attackingInfo->dest = dest;
  1657. attackingInfo->frame = 0;
  1658. attackingInfo->hitCount = 0;
  1659. attackingInfo->ID = ID;
  1660. attackingInfo->reversing = false;
  1661. attackingInfo->posShiftDueToDist = 0;
  1662. attackingInfo->shooting = true;
  1663. attackingInfo->sh = -1;
  1664. if(projectileAngle > straightAngle) //upper shot
  1665. attackingInfo->shootingGroup = 14;
  1666. else if(projectileAngle < -straightAngle) //lower shot
  1667. attackingInfo->shootingGroup = 16;
  1668. else //straight shot
  1669. attackingInfo->shootingGroup = 15;
  1670. attackingInfo->maxframe = creAnims[ID]->framesInGroup(attackingInfo->shootingGroup);
  1671. }
  1672. void CBattleInterface::battleFinished(const BattleResult& br)
  1673. {
  1674. CGI->curh->changeGraphic(0,0);
  1675. SDL_Rect temp_rect = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  1676. CGI->musich->stopMusic();
  1677. resWindow = new CBattleResultWindow(br, temp_rect, this);
  1678. GH.pushInt(resWindow);
  1679. }
  1680. void CBattleInterface::spellCast(SpellCast * sc)
  1681. {
  1682. CSpell &spell = CGI->spellh->spells[sc->id];
  1683. if(sc->side == !LOCPLINT->cb->battleGetStackByID(activeStack)->attackerOwned)
  1684. bSpell->block(true);
  1685. std::vector< std::string > anims; //for magic arrow and ice bolt
  1686. if (spell.soundID != soundBase::invalid)
  1687. CGI->soundh->playSound(spell.soundID);
  1688. switch(sc->id)
  1689. {
  1690. case 15: //magic arrow
  1691. {
  1692. //initialization of anims
  1693. anims.push_back("C20SPX0.DEF"); anims.push_back("C20SPX1.DEF"); anims.push_back("C20SPX2.DEF"); anims.push_back("C20SPX3.DEF"); anims.push_back("C20SPX4.DEF");
  1694. }
  1695. case 16: //ice bolt
  1696. {
  1697. if(anims.size() == 0) //initialization of anims
  1698. {
  1699. anims.push_back("C08SPW0.DEF"); anims.push_back("C08SPW1.DEF"); anims.push_back("C08SPW2.DEF"); anims.push_back("C08SPW3.DEF"); anims.push_back("C08SPW4.DEF");
  1700. }
  1701. } //end of ice bolt only part
  1702. { //common ice bolt and magic arrow part
  1703. //initial variables
  1704. std::string animToDisplay;
  1705. std::pair<int, int> srccoord = sc->side ? std::make_pair(770, 60) : std::make_pair(30, 60);
  1706. std::pair<int, int> destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, LOCPLINT->cb->battleGetStackByPos(sc->tile)); //position attacked by arrow
  1707. destcoord.first += 250; destcoord.second += 240;
  1708. //animation angle
  1709. float angle = atan2(float(destcoord.first - srccoord.first), float(destcoord.second - srccoord.second));
  1710. //choosing animation by angle
  1711. if(angle > 1.50)
  1712. animToDisplay = anims[0];
  1713. else if(angle > 1.20)
  1714. animToDisplay = anims[1];
  1715. else if(angle > 0.90)
  1716. animToDisplay = anims[2];
  1717. else if(angle > 0.60)
  1718. animToDisplay = anims[3];
  1719. else
  1720. animToDisplay = anims[4];
  1721. //displaying animation
  1722. int steps = sqrt((float)((destcoord.first - srccoord.first)*(destcoord.first - srccoord.first) + (destcoord.second - srccoord.second) * (destcoord.second - srccoord.second))) / 40;
  1723. if(steps <= 0)
  1724. steps = 1;
  1725. CDefHandler * animDef = CDefHandler::giveDef(animToDisplay);
  1726. int dx = (destcoord.first - srccoord.first - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.second - srccoord.second - animDef->ourImages[0].bitmap->h)/steps;
  1727. SDL_Rect buf;
  1728. SDL_GetClipRect(screen, &buf);
  1729. SDL_SetClipRect(screen, &pos); //setting rect we can blit to
  1730. for(int g=0; g<steps; ++g)
  1731. {
  1732. show(screen);
  1733. SDL_Rect & srcr = animDef->ourImages[g%animDef->ourImages.size()].bitmap->clip_rect;
  1734. SDL_Rect dstr = genRect(srcr.h, srcr.w, srccoord.first + g*dx, srccoord.second + g*dy);
  1735. SDL_BlitSurface(animDef->ourImages[g%animDef->ourImages.size()].bitmap, &srcr, screen, &dstr);
  1736. CSDL_Ext::update(screen);
  1737. SDL_framerateDelay(LOCPLINT->mainFPSmng);
  1738. }
  1739. SDL_SetClipRect(screen, &buf); //restoring previous clip rect
  1740. break; //for 15 and 16 cases
  1741. }
  1742. case 17: //lightning bolt
  1743. displayEffect(1, sc->tile);
  1744. displayEffect(spell.mainEffectAnim, sc->tile);
  1745. break;
  1746. case 35: //dispel
  1747. case 37: //cure
  1748. case 38: //resurrection
  1749. case 39: //animate dead
  1750. for(std::set<ui32>::const_iterator it = sc->affectedCres.begin(); it != sc->affectedCres.end(); ++it)
  1751. {
  1752. displayEffect(spell.mainEffectAnim, LOCPLINT->cb->battleGetStackByID(*it, false)->position);
  1753. }
  1754. break;
  1755. } //switch(sc->id)
  1756. //support for resistance
  1757. for(int j=0; j<sc->resisted.size(); ++j)
  1758. {
  1759. int tile = LOCPLINT->cb->battleGetStackByID(sc->resisted[j])->position;
  1760. displayEffect(78, tile);
  1761. }
  1762. }
  1763. void CBattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
  1764. {
  1765. for(std::set<ui32>::const_iterator ci = sse.stacks.begin(); ci!=sse.stacks.end(); ++ci)
  1766. {
  1767. displayEffect(CGI->spellh->spells[sse.effect.id].mainEffectAnim, LOCPLINT->cb->battleGetStackByID(*ci)->position);
  1768. }
  1769. redrawBackgroundWithHexes(activeStack);
  1770. }
  1771. void CBattleInterface::castThisSpell(int spellID)
  1772. {
  1773. BattleAction * ba = new BattleAction;
  1774. ba->actionType = 1;
  1775. ba->additionalInfo = spellID; //spell number
  1776. ba->destinationTile = -1;
  1777. ba->stackNumber = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? -1 : -2;
  1778. ba->side = defendingHeroInstance ? (LOCPLINT->playerID == defendingHeroInstance->tempOwner) : false;
  1779. spellToCast = ba;
  1780. spellDestSelectMode = true;
  1781. //choosing possible tragets
  1782. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == LOCPLINT->playerID) ? attackingHeroInstance : attackingHeroInstance;
  1783. spellSelMode = 0;
  1784. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET") != std::string::npos) //spell to be cast on one specific creature
  1785. {
  1786. switch(CGI->spellh->spells[spellID].positiveness)
  1787. {
  1788. case -1 :
  1789. spellSelMode = 2;
  1790. break;
  1791. case 0:
  1792. spellSelMode = 3;
  1793. break;
  1794. case 1:
  1795. spellSelMode = 1;
  1796. break;
  1797. }
  1798. }
  1799. if(CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET_1") != std::string::npos ||
  1800. CGI->spellh->spells[spellID].attributes.find("CREATURE_TARGET_2") != std::string::npos) //spell to be cast on a specific creature but massive on expert
  1801. {
  1802. if(castingHero && castingHero->getSpellSecLevel(spellID) < 3)
  1803. {
  1804. switch(CGI->spellh->spells[spellID].positiveness)
  1805. {
  1806. case -1 :
  1807. spellSelMode = 2;
  1808. break;
  1809. case 0:
  1810. spellSelMode = 3;
  1811. break;
  1812. case 1:
  1813. spellSelMode = 1;
  1814. break;
  1815. }
  1816. }
  1817. else
  1818. {
  1819. spellSelMode = -1;
  1820. }
  1821. }
  1822. if(spellSelMode == -1) //user does not have to select location
  1823. {
  1824. spellToCast->destinationTile = -1;
  1825. LOCPLINT->cb->battleMakeAction(spellToCast);
  1826. delete spellToCast;
  1827. spellToCast = NULL;
  1828. spellDestSelectMode = false;
  1829. CGI->curh->changeGraphic(1, 6);
  1830. }
  1831. else
  1832. {
  1833. CGI->curh->changeGraphic(3, 0);
  1834. }
  1835. }
  1836. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  1837. {
  1838. if(effect == 12) //armageddon
  1839. {
  1840. if(graphics->battleACToDef[effect].size() != 0)
  1841. {
  1842. CDefHandler * anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1843. for(int i=0; i * anim->width < pos.w ; ++i)
  1844. {
  1845. for(int j=0; j * anim->height < pos.h ; ++j)
  1846. {
  1847. SBattleEffect be;
  1848. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1849. be.frame = 0;
  1850. be.maxFrame = be.anim->ourImages.size();
  1851. be.x = i * anim->width;
  1852. be.y = j * anim->height;
  1853. battleEffects.push_back(be);
  1854. }
  1855. }
  1856. }
  1857. }
  1858. else
  1859. {
  1860. if(graphics->battleACToDef[effect].size() != 0)
  1861. {
  1862. SBattleEffect be;
  1863. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  1864. be.frame = 0;
  1865. be.maxFrame = be.anim->ourImages.size();
  1866. be.x = 22 * ( ((destTile/BFIELD_WIDTH) + 1)%2 ) + 44 * (destTile % BFIELD_WIDTH) + 45;
  1867. be.y = 105 + 42 * (destTile/BFIELD_WIDTH);
  1868. if(effect != 1 && effect != 0)
  1869. {
  1870. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1871. be.y -= be.anim->ourImages[0].bitmap->h/2;
  1872. }
  1873. else if(effect == 1)
  1874. {
  1875. be.x -= be.anim->ourImages[0].bitmap->w;
  1876. be.y -= be.anim->ourImages[0].bitmap->h;
  1877. }
  1878. else if (effect == 0)
  1879. {
  1880. be.x -= be.anim->ourImages[0].bitmap->w/2;
  1881. be.y -= be.anim->ourImages[0].bitmap->h;
  1882. }
  1883. battleEffects.push_back(be);
  1884. }
  1885. }
  1886. //battleEffects
  1887. }
  1888. void CBattleInterface::setAnimSpeed(int set)
  1889. {
  1890. settings.animSpeed = set;
  1891. }
  1892. int CBattleInterface::getAnimSpeed() const
  1893. {
  1894. return settings.animSpeed;
  1895. }
  1896. float CBattleInterface::getAnimSpeedMultiplier() const
  1897. {
  1898. switch(settings.animSpeed)
  1899. {
  1900. case 1:
  1901. return 3.5f;
  1902. case 2:
  1903. return 2.2f;
  1904. case 4:
  1905. return 1.0f;
  1906. default:
  1907. return 0.0f;
  1908. }
  1909. }
  1910. void CBattleInterface::endCastingSpell()
  1911. {
  1912. assert(spellDestSelectMode);
  1913. delete spellToCast;
  1914. spellToCast = NULL;
  1915. spellDestSelectMode = false;
  1916. CGI->curh->changeGraphic(1, 6);
  1917. }
  1918. void CBattleInterface::attackingShowHelper()
  1919. {
  1920. if(attackingInfo && !attackingInfo->reversing)
  1921. {
  1922. if(attackingInfo->frame == 0)
  1923. {
  1924. const CStack * aStack = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1925. if(attackingInfo->shooting)
  1926. {
  1927. // TODO: I see that we enter this function twice with
  1928. // attackingInfo->frame==0, so all the inits are done
  1929. // twice. The following is just a workaround until
  1930. // that is fixed. Once done, we can get rid of
  1931. // attackingInfo->sh
  1932. if (attackingInfo->sh == -1)
  1933. attackingInfo->sh = CGI->soundh->playSound(aStack->creature->sounds.shoot);
  1934. creAnims[attackingInfo->ID]->setType(attackingInfo->shootingGroup);
  1935. }
  1936. else
  1937. {
  1938. // TODO: see comment above
  1939. if (attackingInfo->sh == -1)
  1940. attackingInfo->sh = CGI->soundh->playSound(aStack->creature->sounds.attack);
  1941. std::map<int, int> dirToType = boost::assign::map_list_of (0, 11)(1, 11)(2, 12)(3, 13)(4, 13)(5, 12);
  1942. int type; //dependent on attack direction
  1943. if(aStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1944. {
  1945. type = dirToType[ BattleInfo::mutualPosition(aStack->position + attackingInfo->posShiftDueToDist, attackingInfo->dest) ]; //attack direction
  1946. }
  1947. else //else for if(aStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1948. {
  1949. type = BattleInfo::mutualPosition(aStack->position, attackingInfo->dest);
  1950. }
  1951. creAnims[attackingInfo->ID]->setType(type);
  1952. }
  1953. }
  1954. else if(attackingInfo->frame == (attackingInfo->maxframe - 1))
  1955. {
  1956. attackingInfo->reversing = true;
  1957. const CStack* aStackp = LOCPLINT->cb->battleGetStackByID(attackingInfo->ID); //attacking stack
  1958. if(aStackp == NULL)
  1959. return;
  1960. CStack aStack = *aStackp;
  1961. if(aStack.attackerOwned)
  1962. {
  1963. if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1964. {
  1965. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1966. {
  1967. case 0:
  1968. //reverseCreature(ID, aStack.position, true);
  1969. break;
  1970. case 1:
  1971. break;
  1972. case 2:
  1973. break;
  1974. case 3:
  1975. break;
  1976. case 4:
  1977. //reverseCreature(ID, aStack.position, true);
  1978. break;
  1979. case 5:
  1980. reverseCreature(attackingInfo->ID, aStack.position, true);
  1981. break;
  1982. case -1:
  1983. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  1984. {
  1985. reverseCreature(attackingInfo->ID, aStack.position, true);
  1986. }
  1987. break;
  1988. }
  1989. }
  1990. else //else for if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  1991. {
  1992. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  1993. {
  1994. case 0:
  1995. reverseCreature(attackingInfo->ID, aStack.position, true);
  1996. break;
  1997. case 1:
  1998. break;
  1999. case 2:
  2000. break;
  2001. case 3:
  2002. break;
  2003. case 4:
  2004. reverseCreature(attackingInfo->ID, aStack.position, true);
  2005. break;
  2006. case 5:
  2007. reverseCreature(attackingInfo->ID, aStack.position, true);
  2008. break;
  2009. }
  2010. }
  2011. }
  2012. else //if(aStack.attackerOwned)
  2013. {
  2014. if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  2015. {
  2016. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  2017. {
  2018. case 0:
  2019. //reverseCreature(ID, aStack.position, true);
  2020. break;
  2021. case 1:
  2022. break;
  2023. case 2:
  2024. reverseCreature(attackingInfo->ID, aStack.position, true);
  2025. break;
  2026. case 3:
  2027. break;
  2028. case 4:
  2029. //reverseCreature(ID, aStack.position, true);
  2030. break;
  2031. case 5:
  2032. //reverseCreature(ID, aStack.position, true);
  2033. break;
  2034. case -1:
  2035. if(attackingInfo->posShiftDueToDist) //if reversing stack will make its position adjacent to dest
  2036. {
  2037. reverseCreature(attackingInfo->ID, aStack.position, true);
  2038. }
  2039. break;
  2040. }
  2041. }
  2042. else //else for if(aStack.hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  2043. {
  2044. switch(BattleInfo::mutualPosition(aStack.position, attackingInfo->dest)) //attack direction
  2045. {
  2046. case 0:
  2047. //reverseCreature(ID, aStack.position, true);
  2048. break;
  2049. case 1:
  2050. reverseCreature(attackingInfo->ID, aStack.position, true);
  2051. break;
  2052. case 2:
  2053. reverseCreature(attackingInfo->ID, aStack.position, true);
  2054. break;
  2055. case 3:
  2056. reverseCreature(attackingInfo->ID, aStack.position, true);
  2057. break;
  2058. case 4:
  2059. //reverseCreature(ID, aStack.position, true);
  2060. break;
  2061. case 5:
  2062. //reverseCreature(ID, aStack.position, true);
  2063. break;
  2064. }
  2065. }
  2066. }
  2067. attackingInfo->reversing = false;
  2068. creAnims[attackingInfo->ID]->setType(2);
  2069. delete attackingInfo;
  2070. attackingInfo = NULL;
  2071. }
  2072. if(attackingInfo)
  2073. {
  2074. attackingInfo->hitCount++;
  2075. if(attackingInfo->hitCount%(4/settings.animSpeed) == 0)
  2076. attackingInfo->frame++;
  2077. }
  2078. }
  2079. }
  2080. void CBattleInterface::redrawBackgroundWithHexes(int activeStack)
  2081. {
  2082. shadedHexes = LOCPLINT->cb->battleGetAvailableHexes(activeStack, true);
  2083. //preparating background graphic with hexes and shaded hexes
  2084. blitAt(background, 0, 0, backgroundWithHexes);
  2085. if(settings.printCellBorders)
  2086. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  2087. if(settings.printStackRange)
  2088. {
  2089. for(size_t m=0; m<shadedHexes.size(); ++m) //rows
  2090. {
  2091. int i = shadedHexes[m]/BFIELD_WIDTH; //row
  2092. int j = shadedHexes[m]%BFIELD_WIDTH-1; //column
  2093. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  2094. int y = 86 + 42 * i;
  2095. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  2096. }
  2097. }
  2098. }
  2099. void CBattleInterface::printConsoleAttacked(int ID, int dmg, int killed, int IDby)
  2100. {
  2101. char tabh[200];
  2102. const CStack * attacker = LOCPLINT->cb->battleGetStackByID(IDby, false);
  2103. const CStack * defender = LOCPLINT->cb->battleGetStackByID(ID, false);
  2104. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker->amount > 1 ? 377 : 376].c_str(),
  2105. (attacker->amount > 1 ? attacker->creature->namePl.c_str() : attacker->creature->nameSing.c_str()),
  2106. dmg);
  2107. if(killed > 0)
  2108. {
  2109. if(killed > 1)
  2110. {
  2111. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender->creature->namePl.c_str());
  2112. }
  2113. else //killed == 1
  2114. {
  2115. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender->creature->nameSing.c_str());
  2116. }
  2117. }
  2118. console->addText(std::string(tabh));
  2119. }
  2120. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  2121. {
  2122. if(to == NULL)
  2123. to = screen;
  2124. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  2125. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  2126. {
  2127. if(it->animStartDelay>0)
  2128. {
  2129. --(it->animStartDelay);
  2130. continue;
  2131. }
  2132. SDL_Rect dst;
  2133. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  2134. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  2135. dst.x = it->x;
  2136. dst.y = it->y;
  2137. if(it->reverse)
  2138. {
  2139. SDL_Surface * rev = CSDL_Ext::rotate01(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap);
  2140. CSDL_Ext::blit8bppAlphaTo24bpp(rev, NULL, to, &dst);
  2141. SDL_FreeSurface(rev);
  2142. }
  2143. else
  2144. {
  2145. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  2146. }
  2147. //actualizing projectile
  2148. ++it->step;
  2149. if(it->step == it->lastStep)
  2150. {
  2151. toBeDeleted.insert(toBeDeleted.end(), it);
  2152. }
  2153. else
  2154. {
  2155. it->x += it->dx;
  2156. it->y += it->dy;
  2157. if(it->spin)
  2158. {
  2159. ++(it->frameNum);
  2160. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  2161. }
  2162. }
  2163. }
  2164. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  2165. {
  2166. projectiles.erase(*it);
  2167. }
  2168. }
  2169. void CBattleHero::show(SDL_Surface *to)
  2170. {
  2171. //animation of flag
  2172. if(flip)
  2173. {
  2174. CSDL_Ext::blit8bppAlphaTo24bpp(
  2175. flag->ourImages[flagAnim].bitmap,
  2176. NULL,
  2177. screen,
  2178. &genRect(
  2179. flag->ourImages[flagAnim].bitmap->h,
  2180. flag->ourImages[flagAnim].bitmap->w,
  2181. 62 + pos.x,
  2182. 39 + pos.y
  2183. )
  2184. );
  2185. }
  2186. else
  2187. {
  2188. CSDL_Ext::blit8bppAlphaTo24bpp(
  2189. flag->ourImages[flagAnim].bitmap,
  2190. NULL,
  2191. screen,
  2192. &genRect(
  2193. flag->ourImages[flagAnim].bitmap->h,
  2194. flag->ourImages[flagAnim].bitmap->w,
  2195. 71 + pos.x,
  2196. 39 + pos.y
  2197. )
  2198. );
  2199. }
  2200. ++flagAnimCount;
  2201. if(flagAnimCount%4==0)
  2202. {
  2203. ++flagAnim;
  2204. flagAnim %= flag->ourImages.size();
  2205. }
  2206. //animation of hero
  2207. int tick=-1;
  2208. for(int i=0; i<dh->ourImages.size(); ++i)
  2209. {
  2210. if(dh->ourImages[i].groupNumber==phase)
  2211. ++tick;
  2212. if(tick==image)
  2213. {
  2214. SDL_Rect posb = pos;
  2215. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  2216. if(phase != 4 || nextPhase != -1 || image < 4)
  2217. {
  2218. if(flagAnimCount%2==0)
  2219. {
  2220. ++image;
  2221. }
  2222. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  2223. {
  2224. image = 0;
  2225. }
  2226. }
  2227. if(phase == 4 && nextPhase != -1 && image == 7)
  2228. {
  2229. phase = nextPhase;
  2230. nextPhase = -1;
  2231. image = 0;
  2232. }
  2233. break;
  2234. }
  2235. }
  2236. }
  2237. void CBattleHero::activate()
  2238. {
  2239. activateLClick();
  2240. }
  2241. void CBattleHero::deactivate()
  2242. {
  2243. deactivateLClick();
  2244. }
  2245. void CBattleHero::setPhase(int newPhase)
  2246. {
  2247. if(phase != 4)
  2248. {
  2249. phase = newPhase;
  2250. image = 0;
  2251. }
  2252. else
  2253. {
  2254. nextPhase = newPhase;
  2255. }
  2256. }
  2257. void CBattleHero::clickLeft(tribool down, bool previousState)
  2258. {
  2259. if(!down && myHero && LOCPLINT->cb->battleCanCastSpell()) //check conditions
  2260. {
  2261. for(int it=0; it<BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  2262. {
  2263. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  2264. return;
  2265. }
  2266. CGI->curh->changeGraphic(0,0);
  2267. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), myHero);
  2268. GH.pushInt(spellWindow);
  2269. }
  2270. }
  2271. 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)
  2272. {
  2273. dh = CDefHandler::giveDef( defName );
  2274. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  2275. {
  2276. if(flip)
  2277. {
  2278. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  2279. SDL_FreeSurface(dh->ourImages[i].bitmap);
  2280. dh->ourImages[i].bitmap = hlp;
  2281. }
  2282. CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  2283. }
  2284. dh->alphaTransformed = true;
  2285. if(flip)
  2286. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  2287. else
  2288. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  2289. //coloring flag and adding transparency
  2290. for(int i=0; i<flag->ourImages.size(); ++i)
  2291. {
  2292. CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  2293. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  2294. }
  2295. }
  2296. CBattleHero::~CBattleHero()
  2297. {
  2298. delete dh;
  2299. delete flag;
  2300. }
  2301. std::pair<int, int> CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack)
  2302. {
  2303. std::pair<int, int> ret = std::make_pair(-500, -500); //returned value
  2304. ret.second = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
  2305. //counting x
  2306. if(attacker)
  2307. {
  2308. ret.first = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  2309. }
  2310. else
  2311. {
  2312. ret.first = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  2313. }
  2314. //shifting position for double - hex creatures
  2315. if(stack && stack->hasFeatureOfType(StackFeature::DOUBLE_WIDE))
  2316. {
  2317. if(attacker)
  2318. {
  2319. ret.first -= 42;
  2320. }
  2321. else
  2322. {
  2323. ret.first += 42;
  2324. }
  2325. }
  2326. //returning
  2327. return ret;
  2328. }
  2329. void CBattleHex::activate()
  2330. {
  2331. activateHover();
  2332. activateMouseMove();
  2333. activateLClick();
  2334. activateRClick();
  2335. }
  2336. void CBattleHex::deactivate()
  2337. {
  2338. deactivateHover();
  2339. deactivateMouseMove();
  2340. deactivateLClick();
  2341. deactivateRClick();
  2342. }
  2343. void CBattleHex::hover(bool on)
  2344. {
  2345. hovered = on;
  2346. //Hoverable::hover(on);
  2347. if(!on && setAlterText)
  2348. {
  2349. myInterface->console->alterTxt = std::string();
  2350. setAlterText = false;
  2351. }
  2352. }
  2353. CBattleHex::CBattleHex() : setAlterText(false), myNumber(-1), accesible(true), hovered(false), strictHovered(false), myInterface(NULL)
  2354. {
  2355. }
  2356. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  2357. {
  2358. if(myInterface->cellShade)
  2359. {
  2360. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  2361. {
  2362. strictHovered = false;
  2363. }
  2364. else //hovered pixel is inside hex
  2365. {
  2366. strictHovered = true;
  2367. }
  2368. }
  2369. if(hovered && strictHovered) //print attacked creature to console
  2370. {
  2371. if(myInterface->console->alterTxt.size() == 0 && LOCPLINT->cb->battleGetStack(myNumber) != -1 &&
  2372. LOCPLINT->cb->battleGetStackByPos(myNumber)->owner != LOCPLINT->playerID &&
  2373. LOCPLINT->cb->battleGetStackByPos(myNumber)->alive())
  2374. {
  2375. char tabh[160];
  2376. CStack attackedStack = *LOCPLINT->cb->battleGetStackByPos(myNumber);
  2377. const std::string & attackedName = attackedStack.amount == 1 ? attackedStack.creature->nameSing : attackedStack.creature->namePl;
  2378. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  2379. myInterface->console->alterTxt = std::string(tabh);
  2380. setAlterText = true;
  2381. }
  2382. }
  2383. else if(setAlterText)
  2384. {
  2385. myInterface->console->alterTxt = std::string();
  2386. setAlterText = false;
  2387. }
  2388. }
  2389. void CBattleHex::clickLeft(tribool down, bool previousState)
  2390. {
  2391. if(!down && hovered && strictHovered) //we've been really clicked!
  2392. {
  2393. myInterface->hexLclicked(myNumber);
  2394. }
  2395. }
  2396. void CBattleHex::clickRight(tribool down, bool previousState)
  2397. {
  2398. int stID = LOCPLINT->cb->battleGetStack(myNumber); //id of stack being on this tile
  2399. if(hovered && strictHovered && stID!=-1)
  2400. {
  2401. const CStack & myst = *LOCPLINT->cb->battleGetStackByID(stID); //stack info
  2402. if(!myst.alive()) return;
  2403. StackState *pom = NULL;
  2404. if(down)
  2405. {
  2406. pom = new StackState();
  2407. const CGHeroInstance *h = myst.owner == myInterface->attackingHeroInstance->tempOwner ? myInterface->attackingHeroInstance : myInterface->defendingHeroInstance;
  2408. pom->attackBonus = myst.Attack() - myst.creature->attack;
  2409. pom->defenseBonus = myst.Defense() - myst.creature->defence;
  2410. pom->luck = myst.Luck();
  2411. pom->morale = myst.Morale();
  2412. pom->speedBonus = myst.Speed() - myst.creature->speed;
  2413. pom->healthBonus = myst.MaxHealth() - myst.creature->hitPoints;
  2414. if(myst.hasFeatureOfType(StackFeature::SIEGE_WEAPON))
  2415. pom->dmgMultiplier = h->getPrimSkillLevel(0) + 1;
  2416. else
  2417. pom->dmgMultiplier = 1;
  2418. pom->shotsLeft = myst.shots;
  2419. for(int vb=0; vb<myst.effects.size(); ++vb)
  2420. {
  2421. pom->effects.insert(myst.effects[vb].id);
  2422. }
  2423. pom->currentHealth = myst.firstHPleft;
  2424. GH.pushInt(new CCreInfoWindow(myst.creature->idNumber, 0, myst.amount, pom, 0, 0, NULL));
  2425. }
  2426. delete pom;
  2427. }
  2428. }
  2429. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt(""), whoSetAlter(0)
  2430. {
  2431. }
  2432. CBattleConsole::~CBattleConsole()
  2433. {
  2434. texts.clear();
  2435. }
  2436. void CBattleConsole::show(SDL_Surface * to)
  2437. {
  2438. if(ingcAlter.size())
  2439. {
  2440. CSDL_Ext::printAtMiddleWB(ingcAlter, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2441. }
  2442. else if(alterTxt.size())
  2443. {
  2444. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2445. }
  2446. else if(texts.size())
  2447. {
  2448. if(texts.size()==1)
  2449. {
  2450. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2451. }
  2452. else
  2453. {
  2454. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 10, GEOR13, 80, zwykly, to);
  2455. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 26, GEOR13, 80, zwykly, to);
  2456. }
  2457. }
  2458. }
  2459. bool CBattleConsole::addText(const std::string & text)
  2460. {
  2461. if(text.size()>70)
  2462. return false; //text too long!
  2463. int firstInToken = 0;
  2464. for(int i=0; i<text.size(); ++i) //tokenize
  2465. {
  2466. if(text[i] == 10)
  2467. {
  2468. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  2469. firstInToken = i+1;
  2470. }
  2471. }
  2472. texts.push_back( text.substr(firstInToken, text.size()) );
  2473. lastShown = texts.size()-1;
  2474. return true;
  2475. }
  2476. void CBattleConsole::eraseText(unsigned int pos)
  2477. {
  2478. if(pos < texts.size())
  2479. {
  2480. texts.erase(texts.begin() + pos);
  2481. if(lastShown == texts.size())
  2482. --lastShown;
  2483. }
  2484. }
  2485. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  2486. {
  2487. if(pos >= texts.size()) //no such pos
  2488. return;
  2489. texts[pos] = text;
  2490. }
  2491. void CBattleConsole::scrollUp(unsigned int by)
  2492. {
  2493. if(lastShown > by)
  2494. lastShown -= by;
  2495. }
  2496. void CBattleConsole::scrollDown(unsigned int by)
  2497. {
  2498. if(lastShown + by < texts.size())
  2499. lastShown += by;
  2500. }
  2501. CBattleResultWindow::CBattleResultWindow(const BattleResult &br, const SDL_Rect & pos, const CBattleInterface * owner)
  2502. {
  2503. this->pos = pos;
  2504. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  2505. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2506. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  2507. SDL_FreeSurface(background);
  2508. background = pom;
  2509. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleResultWindow::bExitf,this), 384 + pos.x, 505 + pos.y, "iok6432.def", SDLK_RETURN);
  2510. if(br.winner==0) //attacker won
  2511. {
  2512. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 60, 122, GEOR13, zwykly, background);
  2513. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 410, 122, GEOR13, zwykly, background);
  2514. }
  2515. else //if(br.winner==1)
  2516. {
  2517. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 60, 122, GEOR13, zwykly, background);
  2518. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 410, 122, GEOR13, zwykly, background);
  2519. }
  2520. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 235, 299, GEOR16, tytulowy, background);
  2521. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 235, 329, GEOR16, zwykly, background);
  2522. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 235, 426, GEOR16, zwykly, background);
  2523. std::string attackerName, defenderName;
  2524. if(owner->attackingHeroInstance) //a hero attacked
  2525. {
  2526. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  2527. //setting attackerName
  2528. attackerName = owner->attackingHeroInstance->name;
  2529. }
  2530. else //a monster attacked
  2531. {
  2532. int bestMonsterID = -1;
  2533. int bestPower = 0;
  2534. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army1->slots.begin(); it!=owner->army1->slots.end(); ++it)
  2535. {
  2536. if( CGI->creh->creatures[it->first].AIValue > bestPower)
  2537. {
  2538. bestPower = CGI->creh->creatures[it->first].AIValue;
  2539. bestMonsterID = it->first;
  2540. }
  2541. }
  2542. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  2543. //setting attackerName
  2544. attackerName = CGI->creh->creatures[bestMonsterID].namePl;
  2545. }
  2546. if(owner->defendingHeroInstance) //a hero defended
  2547. {
  2548. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 391, 38));
  2549. //setting defenderName
  2550. defenderName = owner->defendingHeroInstance->name;
  2551. }
  2552. else //a monster defended
  2553. {
  2554. int bestMonsterID = -1;
  2555. int bestPower = 0;
  2556. for(std::map<si32,std::pair<ui32,si32> >::const_iterator it = owner->army2->slots.begin(); it!=owner->army2->slots.end(); ++it)
  2557. {
  2558. if( CGI->creh->creatures[it->second.first].AIValue > bestPower)
  2559. {
  2560. bestPower = CGI->creh->creatures[it->second.first].AIValue;
  2561. bestMonsterID = it->second.first;
  2562. }
  2563. }
  2564. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 391, 38));
  2565. //setting defenderName
  2566. defenderName = CGI->creh->creatures[bestMonsterID].namePl;
  2567. }
  2568. //printing attacker and defender's names
  2569. CSDL_Ext::printAtMiddle(attackerName, 156, 44, GEOR16, zwykly, background);
  2570. CSDL_Ext::printAtMiddle(defenderName, 314, 44, GEOR16, zwykly, background);
  2571. //printing casualities
  2572. for(int step = 0; step < 2; ++step)
  2573. {
  2574. if(br.casualties[step].size()==0)
  2575. {
  2576. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, GEOR16, zwykly, background);
  2577. }
  2578. else
  2579. {
  2580. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  2581. int yPos = 344 + step*97;
  2582. for(std::set<std::pair<ui32,si32> >::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  2583. {
  2584. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  2585. std::ostringstream amount;
  2586. amount<<it->second;
  2587. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, GEOR13, zwykly, background);
  2588. xPos += 42;
  2589. }
  2590. }
  2591. }
  2592. //printing result description
  2593. bool weAreAttacker = (LOCPLINT->playerID == owner->attackingHeroInstance->tempOwner);
  2594. switch(br.result)
  2595. {
  2596. case 0: //normal victory
  2597. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2598. {
  2599. CGI->musich->playMusic(musicBase::winBattle);
  2600. #ifdef _WIN32
  2601. CGI->videoh->open(VIDEO_WIN);
  2602. #else
  2603. CGI->videoh->open(VIDEO_WIN, true);
  2604. #endif
  2605. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[304], 235, 235, GEOR13, zwykly, background);
  2606. }
  2607. else
  2608. {
  2609. CGI->musich->playMusic(musicBase::loseCombat);
  2610. CGI->videoh->open(VIDEO_LOSE_BATTLE_START);
  2611. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, GEOR13, zwykly, background);
  2612. }
  2613. break;
  2614. case 1: //flee
  2615. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2616. {
  2617. CGI->musich->playMusic(musicBase::winBattle);
  2618. #ifdef _WIN32
  2619. CGI->videoh->open(VIDEO_WIN);
  2620. #else
  2621. CGI->videoh->open(VIDEO_WIN, true);
  2622. #endif
  2623. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[303], 235, 235, GEOR13, zwykly, background);
  2624. }
  2625. else
  2626. {
  2627. CGI->musich->playMusic(musicBase::retreatBattle);
  2628. CGI->videoh->open(VIDEO_RETREAT_START);
  2629. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, GEOR13, zwykly, background);
  2630. }
  2631. break;
  2632. case 2: //surrender
  2633. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  2634. {
  2635. CGI->musich->playMusic(musicBase::winBattle);
  2636. #ifdef _WIN32
  2637. CGI->videoh->open(VIDEO_WIN);
  2638. #else
  2639. CGI->videoh->open(VIDEO_WIN, true);
  2640. #endif
  2641. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[302], 235, 235, GEOR13, zwykly, background);
  2642. }
  2643. else
  2644. {
  2645. CGI->musich->playMusic(musicBase::surrenderBattle);
  2646. CGI->videoh->open(VIDEO_SURRENDER);
  2647. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 235, GEOR13, zwykly, background);
  2648. }
  2649. break;
  2650. }
  2651. }
  2652. CBattleResultWindow::~CBattleResultWindow()
  2653. {
  2654. SDL_FreeSurface(background);
  2655. }
  2656. void CBattleResultWindow::activate()
  2657. {
  2658. LOCPLINT->showingDialog->set(true);
  2659. exit->activate();
  2660. }
  2661. void CBattleResultWindow::deactivate()
  2662. {
  2663. exit->deactivate();
  2664. }
  2665. void CBattleResultWindow::show(SDL_Surface *to)
  2666. {
  2667. //evaluating to
  2668. if(!to)
  2669. to = screen;
  2670. CGI->videoh->update(107, 70, background, false, true);
  2671. SDL_BlitSurface(background, NULL, to, &pos);
  2672. exit->show(to);
  2673. }
  2674. void CBattleResultWindow::bExitf()
  2675. {
  2676. GH.popInts(2); //first - we; second - battle interface
  2677. LOCPLINT->showingDialog->setn(false);
  2678. CGI->videoh->close();
  2679. }
  2680. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  2681. {
  2682. pos = position;
  2683. background = BitmapHandler::loadBitmap("comopbck.bmp", true);
  2684. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  2685. 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);
  2686. viewGrid->select(owner->settings.printCellBorders);
  2687. 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);
  2688. movementShadow->select(owner->settings.printStackRange);
  2689. 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);
  2690. mouseShadow->select(owner->settings.printMouseShadow);
  2691. animSpeeds = new CHighlightableButtonsGroup(0);
  2692. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def",188, 309, 1);
  2693. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def",252, 309, 2);
  2694. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",315, 309, 4);
  2695. animSpeeds->select(owner->getAnimSpeed(), 1);
  2696. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  2697. setToDefault = new AdventureMapButton (CGI->generaltexth->zelp[392].first, CGI->generaltexth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def");
  2698. std::swap(setToDefault->imgs[0][0], setToDefault->imgs[0][1]);
  2699. exit = new AdventureMapButton (CGI->generaltexth->zelp[393].first, CGI->generaltexth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def",SDLK_RETURN);
  2700. std::swap(exit->imgs[0][0], exit->imgs[0][1]);
  2701. //printing texts to background
  2702. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 240, 32, GEOR16, tytulowy, background); //window title
  2703. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 211, GEOR16, tytulowy, background); //animation speed
  2704. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 292, GEOR16, tytulowy, background); //music volume
  2705. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 358, GEOR16, tytulowy, background); //effects' volume
  2706. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 64, GEOR16, tytulowy, background); //auto - combat options
  2707. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 264, GEOR16, tytulowy, background); //creature info
  2708. //auto - combat options
  2709. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 87, GEOR16, zwykly, background); //creatures
  2710. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 117, GEOR16, zwykly, background); //spells
  2711. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 147, GEOR16, zwykly, background); //catapult
  2712. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 177, GEOR16, zwykly, background); //ballista
  2713. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 207, GEOR16, zwykly, background); //first aid tent
  2714. //creature info
  2715. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 286, GEOR16, zwykly, background); //all stats
  2716. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 316, GEOR16, zwykly, background); //spells only
  2717. //general options
  2718. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 58, GEOR16, zwykly, background); //hex grid
  2719. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 91, GEOR16, zwykly, background); //movement shadow
  2720. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, GEOR16, zwykly, background); //cursor shadow
  2721. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 157, GEOR16, zwykly, background); //spellbook animation
  2722. //texts printed
  2723. }
  2724. CBattleOptionsWindow::~CBattleOptionsWindow()
  2725. {
  2726. SDL_FreeSurface(background);
  2727. delete setToDefault;
  2728. delete exit;
  2729. delete viewGrid;
  2730. delete movementShadow;
  2731. delete animSpeeds;
  2732. delete mouseShadow;
  2733. }
  2734. void CBattleOptionsWindow::activate()
  2735. {
  2736. setToDefault->activate();
  2737. exit->activate();
  2738. viewGrid->activate();
  2739. movementShadow->activate();
  2740. animSpeeds->activate();
  2741. mouseShadow->activate();
  2742. }
  2743. void CBattleOptionsWindow::deactivate()
  2744. {
  2745. setToDefault->deactivate();
  2746. exit->deactivate();
  2747. viewGrid->deactivate();
  2748. movementShadow->deactivate();
  2749. animSpeeds->deactivate();
  2750. mouseShadow->deactivate();
  2751. }
  2752. void CBattleOptionsWindow::show(SDL_Surface *to)
  2753. {
  2754. if(!to) //"evaluating" to
  2755. to = screen;
  2756. SDL_BlitSurface(background, NULL, to, &pos);
  2757. setToDefault->show(to);
  2758. exit->show(to);
  2759. viewGrid->show(to);
  2760. movementShadow->show(to);
  2761. animSpeeds->show(to);
  2762. mouseShadow->show(to);
  2763. }
  2764. void CBattleOptionsWindow::bDefaultf()
  2765. {
  2766. }
  2767. void CBattleOptionsWindow::bExitf()
  2768. {
  2769. GH.popIntTotally(this);
  2770. }