CBattleInterface.cpp 110 KB

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