CBattleInterface.cpp 75 KB

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