CBattleInterface.cpp 94 KB

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