CBattleInterface.cpp 93 KB

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