CBattleInterface.cpp 106 KB

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