CBattleInterface.cpp 95 KB

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