CBattleInterface.cpp 79 KB

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