CBattleInterface.cpp 88 KB

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