CBattleInterface.cpp 88 KB

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