CBattleInterface.cpp 86 KB

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