CBattleInterface.cpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190
  1. #include "StdInc.h"
  2. #include "CBattleInterface.h"
  3. #include "../CGameInfo.h"
  4. #include "../../lib/CLodHandler.h"
  5. #include "../SDL_Extensions.h"
  6. #include "../CAdvmapInterface.h"
  7. #include "../AdventureMapButton.h"
  8. #include "../CAnimation.h"
  9. #include "../../lib/CObjectHandler.h"
  10. #include "../../lib/CHeroHandler.h"
  11. #include "../CDefHandler.h"
  12. #include "../../lib/CSpellHandler.h"
  13. #include "../CMusicHandler.h"
  14. #include "../CMessage.h"
  15. #include "../CCursorHandler.h"
  16. #include "../../CCallback.h"
  17. #include "../../lib/BattleState.h"
  18. #include "../../lib/CGeneralTextHandler.h"
  19. #include "CCreatureAnimation.h"
  20. #include "../Graphics.h"
  21. #include "../CSpellWindow.h"
  22. #include "../CConfigHandler.h"
  23. #include "../../lib/CondSh.h"
  24. #include "../../lib/NetPacks.h"
  25. #include "../CPlayerInterface.h"
  26. #include "../CCreatureWindow.h"
  27. #include "../CVideoHandler.h"
  28. #include "../../lib/CTownHandler.h"
  29. #include "../../lib/map.h"
  30. #include "CBattleHero.h"
  31. #include "CStackQueue.h"
  32. #include "CBattleConsole.h"
  33. #include "CBattleResultWindow.h"
  34. #include "CBattleAnimation.h"
  35. #include "CBattleOptionsWindow.h"
  36. #include "CDummyAnimation.h"
  37. #include "CClickableHex.h"
  38. #include "CShootingAnimation.h"
  39. #include "CSpellEffectAnimation.h"
  40. #include "CMeleeAttackAnimation.h"
  41. #include "CReverseAnimation.h"
  42. #include "CMovementStartAnimation.h"
  43. #include "CMovementEndAnimation.h"
  44. #include "CDefenceAnimation.h"
  45. #include "CMovementAnimation.h"
  46. #include "../UIFramework/CGuiHandler.h"
  47. #ifndef __GNUC__
  48. const double M_PI = 3.14159265358979323846;
  49. #else
  50. #define _USE_MATH_DEFINES
  51. #include <cmath>
  52. #endif
  53. #include <boost/format.hpp>
  54. const time_t CBattleInterface::HOVER_ANIM_DELTA = 1;
  55. /*
  56. * CBattleInterface.cpp, part of VCMI engine
  57. *
  58. * Authors: listed in file AUTHORS in main folder
  59. *
  60. * License: GNU General Public License v2.0 or later
  61. * Full text of license available in license.txt file, in main folder
  62. *
  63. */
  64. extern SDL_Surface * screen;
  65. extern SDL_Color zwykly;
  66. CondSh<bool> CBattleInterface::animsAreDisplayed;
  67. struct CMP_stack2
  68. {
  69. inline bool operator ()(const CStack& a, const CStack& b)
  70. {
  71. return (a.Speed())>(b.Speed());
  72. }
  73. } cmpst2 ;
  74. static void transformPalette(SDL_Surface * surf, double rCor, double gCor, double bCor)
  75. {
  76. SDL_Color * colorsToChange = surf->format->palette->colors;
  77. for(int g=0; g<surf->format->palette->ncolors; ++g)
  78. {
  79. if((colorsToChange+g)->b != 132 &&
  80. (colorsToChange+g)->g != 231 &&
  81. (colorsToChange+g)->r != 255) //it's not yellow border
  82. {
  83. (colorsToChange+g)->r = static_cast<double>((colorsToChange+g)->r) * rCor;
  84. (colorsToChange+g)->g = static_cast<double>((colorsToChange+g)->g) * gCor;
  85. (colorsToChange+g)->b = static_cast<double>((colorsToChange+g)->b) * bCor;
  86. }
  87. }
  88. }
  89. //////////////////////
  90. void CBattleInterface::addNewAnim(CBattleAnimation * anim)
  91. {
  92. pendingAnims.push_back( std::make_pair(anim, false) );
  93. animsAreDisplayed.setn(true);
  94. }
  95. CBattleInterface::CBattleInterface(const CCreatureSet * army1, const CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect, CPlayerInterface * att, CPlayerInterface * defen)
  96. : queue(NULL), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0),
  97. activeStack(NULL), stackToActivate(NULL), mouseHoveredStack(-1), lastMouseHoveredStackAnimationTime(-1), previouslyHoveredHex(-1),
  98. currentlyHoveredHex(-1), attackingHex(-1), tacticianInterface(NULL), stackCanCastSpell(false), spellDestSelectMode(false), spellSelMode(NO_LOCATION), spellToCast(NULL),
  99. siegeH(NULL), attackerInt(att), defenderInt(defen), curInt(att), animIDhelper(0), givenCommand(NULL),
  100. myTurn(false), resWindow(NULL), moveStarted(false), moveSh(-1), bresult(NULL)
  101. {
  102. OBJ_CONSTRUCTION;
  103. if(!curInt) curInt = LOCPLINT; //may happen when we are defending during network MP game
  104. animsAreDisplayed.setn(false);
  105. pos = myRect;
  106. strongInterest = true;
  107. givenCommand = new CondSh<BattleAction *>(NULL);
  108. if(attackerInt && attackerInt->cb->battleGetTacticDist()) //hotseat -> check tactics for both players (defender may be local human)
  109. tacticianInterface = attackerInt;
  110. else if(defenderInt && defenderInt->cb->battleGetTacticDist())
  111. tacticianInterface = defenderInt;
  112. tacticsMode = tacticianInterface; //if we found interface of player with tactics, then enter tactics mode
  113. //create stack queue
  114. bool embedQueue = screen->h < 700;
  115. queue = new CStackQueue(embedQueue, this);
  116. if(!embedQueue)
  117. {
  118. if(curInt->sysOpts.showQueue)
  119. pos.y += queue->pos.h / 2; //center whole window
  120. queue->moveTo(SPoint(pos.x, pos.y - queue->pos.h));
  121. // queue->pos.x = pos.x;
  122. // queue->pos.y = pos.y - queue->pos.h;
  123. // pos.h += queue->pos.h;
  124. // center();
  125. }
  126. queue->update();
  127. //preparing siege info
  128. const CGTownInstance * town = curInt->cb->battleGetDefendedTown();
  129. if(town && town->hasFort())
  130. {
  131. siegeH = new SiegeHelper(town, this);
  132. }
  133. curInt->battleInt = this;
  134. //initializing armies
  135. this->army1 = army1;
  136. this->army2 = army2;
  137. std::vector<const CStack*> stacks = curInt->cb->battleGetAllStacks();
  138. BOOST_FOREACH(const CStack *s, stacks)
  139. {
  140. newStack(s);
  141. }
  142. //preparing menu background and terrain
  143. if(siegeH)
  144. {
  145. background = BitmapHandler::loadBitmap( siegeH->getSiegeName(0), false );
  146. ui8 siegeLevel = curInt->cb->battleGetSiegeLevel();
  147. if(siegeLevel >= 2) //citadel or castle
  148. {
  149. //print moat/mlip
  150. SDL_Surface * moat = BitmapHandler::loadBitmap( siegeH->getSiegeName(13) ),
  151. * mlip = BitmapHandler::loadBitmap( siegeH->getSiegeName(14) );
  152. SPoint moatPos = graphics->wallPositions[siegeH->town->town->typeID][12],
  153. mlipPos = graphics->wallPositions[siegeH->town->town->typeID][13];
  154. if(moat) //eg. tower has no moat
  155. blitAt(moat, moatPos.x,moatPos.y, background);
  156. if(mlip) //eg. tower has no mlip
  157. blitAt(mlip, mlipPos.x, mlipPos.y, background);
  158. SDL_FreeSurface(moat);
  159. SDL_FreeSurface(mlip);
  160. }
  161. }
  162. else
  163. {
  164. std::vector< std::string > & backref = graphics->battleBacks[ curInt->cb->battleGetBattlefieldType() ];
  165. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()], false );
  166. }
  167. //preparing menu background
  168. //graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  169. //preparing graphics for displaying amounts of creatures
  170. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  171. CSDL_Ext::alphaTransform(amountNormal);
  172. transformPalette(amountNormal, 0.59, 0.19, 0.93);
  173. amountPositive = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  174. CSDL_Ext::alphaTransform(amountPositive);
  175. transformPalette(amountPositive, 0.18, 1.00, 0.18);
  176. amountNegative = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  177. CSDL_Ext::alphaTransform(amountNegative);
  178. transformPalette(amountNegative, 1.00, 0.18, 0.18);
  179. amountEffNeutral = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  180. CSDL_Ext::alphaTransform(amountEffNeutral);
  181. transformPalette(amountEffNeutral, 1.00, 1.00, 0.18);
  182. ////blitting menu background and terrain
  183. // blitAt(background, pos.x, pos.y);
  184. // blitAt(menu, pos.x, 556 + pos.y);
  185. //preparing buttons and console
  186. 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);
  187. 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);
  188. 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);
  189. bFlee->block(!curInt->cb->battleCanFlee());
  190. bSurrender->block(curInt->cb->battleGetSurrenderCost() < 0);
  191. 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);
  192. 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);
  193. bSpell->block(true);
  194. 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);
  195. 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);
  196. bDefence->assignedKeys.insert(SDLK_SPACE);
  197. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624 + pos.x, 561 + pos.y, "ComSlide.def", SDLK_UP);
  198. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624 + pos.x, 580 + pos.y, "ComSlide.def", SDLK_DOWN);
  199. bConsoleDown->setOffset(2);
  200. console = new CBattleConsole();
  201. console->pos.x = 211 + pos.x;
  202. console->pos.y = 560 + pos.y;
  203. console->pos.w = 406;
  204. console->pos.h = 38;
  205. if(tacticsMode)
  206. {
  207. btactNext = new AdventureMapButton(std::string(), std::string(), boost::bind(&CBattleInterface::bTacticNextStack,this), 213 + pos.x, 560 + pos.y, "icm011.def", SDLK_SPACE);
  208. btactEnd = new AdventureMapButton(std::string(), std::string(), boost::bind(&CBattleInterface::bEndTacticPhase,this), 419 + pos.x, 560 + pos.y, "icm012.def", SDLK_RETURN);
  209. bDefence->block(true);
  210. bWait->block(true);
  211. menu = BitmapHandler::loadBitmap("COPLACBR.BMP");
  212. }
  213. else
  214. {
  215. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  216. btactEnd = btactNext = NULL;
  217. }
  218. graphics->blueToPlayersAdv(menu, curInt->playerID);
  219. //loading hero animations
  220. if(hero1) // attacking hero
  221. {
  222. int type = hero1->type->heroType;
  223. if ( type % 2 ) type--;
  224. if ( hero1->sex ) type++;
  225. attackingHero = new CBattleHero(graphics->battleHeroes[type], 0, 0, false, hero1->tempOwner, hero1->tempOwner == curInt->playerID ? hero1 : NULL, this);
  226. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, pos.x - 43, pos.y - 19);
  227. }
  228. else
  229. {
  230. attackingHero = NULL;
  231. }
  232. if(hero2) // defending hero
  233. {
  234. int type = hero2->type->heroType;
  235. if ( type % 2 ) type--;
  236. if ( hero2->sex ) type++;
  237. defendingHero = new CBattleHero(graphics->battleHeroes[type ], 0, 0, true, hero2->tempOwner, hero2->tempOwner == curInt->playerID ? hero2 : NULL, this);
  238. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, pos.x + 693, pos.y - 19);
  239. }
  240. else
  241. {
  242. defendingHero = NULL;
  243. }
  244. //preparing cells and hexes
  245. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  246. CSDL_Ext::alphaTransform(cellBorder);
  247. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  248. CSDL_Ext::alphaTransform(cellShade);
  249. for(int h = 0; h < ARRAY_COUNT(bfield); ++h)
  250. {
  251. bfield[h].myNumber = h;
  252. int x = 14 + ((h/GameConstants::BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(h%GameConstants::BFIELD_WIDTH);
  253. int y = 86 + 42 * (h/GameConstants::BFIELD_WIDTH);
  254. bfield[h].pos = genRect(cellShade->h, cellShade->w, x + pos.x, y + pos.y);
  255. bfield[h].accessible = true;
  256. bfield[h].myInterface = this;
  257. }
  258. //locking occupied positions on batlefield
  259. BOOST_FOREACH(const CStack *s, stacks) //stacks gained at top of this function
  260. if(s->position >= 0) //turrets have position < 0
  261. bfield[s->position].accessible = false;
  262. //loading projectiles for units
  263. BOOST_FOREACH(const CStack *s, stacks)
  264. {
  265. int creID = (s->getCreature()->idNumber == 149) ? CGI->creh->factionToTurretCreature[siegeH->town->town->typeID] : s->getCreature()->idNumber; //id of creature whose shots should be loaded
  266. if(s->getCreature()->isShooting() && vstd::contains(CGI->creh->idToProjectile, creID))
  267. {
  268. CDefHandler *&projectile = idToProjectile[s->getCreature()->idNumber];
  269. projectile = CDefHandler::giveDef(CGI->creh->idToProjectile[creID]);
  270. if(projectile->ourImages.size() > 2) //add symmetric images
  271. {
  272. for(int k = projectile->ourImages.size()-2; k > 1; --k)
  273. {
  274. Cimage ci;
  275. ci.bitmap = CSDL_Ext::rotate01(projectile->ourImages[k].bitmap);
  276. ci.groupNumber = 0;
  277. ci.imName = std::string();
  278. projectile->ourImages.push_back(ci);
  279. }
  280. }
  281. for(size_t s = 0; s < projectile->ourImages.size(); ++s) //alpha transforming
  282. {
  283. CSDL_Ext::alphaTransform(projectile->ourImages[s].bitmap);
  284. }
  285. }
  286. }
  287. //preparing graphic with cell borders
  288. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  289. //copying palette
  290. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  291. {
  292. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  293. }
  294. //palette copied
  295. for(int i=0; i<GameConstants::BFIELD_HEIGHT; ++i) //rows
  296. {
  297. for(int j=0; j<GameConstants::BFIELD_WIDTH-2; ++j) //columns
  298. {
  299. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  300. int y = 86 + 42 * i;
  301. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  302. {
  303. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  304. {
  305. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  306. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  307. }
  308. }
  309. }
  310. }
  311. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  312. //preparing obstacle defs
  313. std::vector<CObstacleInstance> obst = curInt->cb->battleGetAllObstacles();
  314. for(size_t t = 0; t < obst.size(); ++t)
  315. {
  316. idToObstacle[obst[t].ID] = CDefHandler::giveDef(CGI->heroh->obstacles.find(obst[t].ID)->second.defName);
  317. for(size_t n = 0; n < idToObstacle[obst[t].ID]->ourImages.size(); ++n)
  318. {
  319. SDL_SetColorKey(idToObstacle[obst[t].ID]->ourImages[n].bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(idToObstacle[obst[t].ID]->ourImages[n].bitmap->format,0,255,255));
  320. }
  321. }
  322. for (int i = 0; i < ARRAY_COUNT(bfield); i++)
  323. {
  324. children.push_back(&bfield[i]);
  325. }
  326. if(tacticsMode)
  327. {
  328. active = 1;
  329. bTacticNextStack();
  330. active = 0;
  331. }
  332. CCS->musich->stopMusic();
  333. int channel = CCS->soundh->playSoundFromSet(CCS->soundh->battleIntroSounds);
  334. CCS->soundh->setCallback(channel, boost::bind(&CMusicHandler::playMusicFromSet, CCS->musich, CCS->musich->battleMusics, -1));
  335. memset(stackCountOutsideHexes, 1, GameConstants::BFIELD_SIZE * sizeof(bool)); //initialize array with trues
  336. }
  337. CBattleInterface::~CBattleInterface()
  338. {
  339. if (active) //dirty fix for #485
  340. {
  341. deactivate();
  342. }
  343. SDL_FreeSurface(background);
  344. SDL_FreeSurface(menu);
  345. SDL_FreeSurface(amountNormal);
  346. SDL_FreeSurface(amountNegative);
  347. SDL_FreeSurface(amountPositive);
  348. SDL_FreeSurface(amountEffNeutral);
  349. SDL_FreeSurface(cellBorders);
  350. SDL_FreeSurface(backgroundWithHexes);
  351. delete bOptions;
  352. delete bSurrender;
  353. delete bFlee;
  354. delete bAutofight;
  355. delete bSpell;
  356. delete bWait;
  357. delete bDefence;
  358. delete bConsoleUp;
  359. delete bConsoleDown;
  360. delete console;
  361. delete givenCommand;
  362. delete attackingHero;
  363. delete defendingHero;
  364. delete queue;
  365. SDL_FreeSurface(cellBorder);
  366. SDL_FreeSurface(cellShade);
  367. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  368. delete g->second;
  369. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  370. delete g->second;
  371. for(std::map< int, CDefHandler * >::iterator g=idToObstacle.begin(); g!=idToObstacle.end(); ++g)
  372. delete g->second;
  373. delete siegeH;
  374. curInt->battleInt = NULL;
  375. //TODO: play AI tracks if battle was during AI turn
  376. //if (!curInt->makingTurn)
  377. //CCS->musich->playMusicFromSet(CCS->musich->aiMusics, -1);
  378. if(adventureInt && adventureInt->selection)
  379. {
  380. int terrain = LOCPLINT->cb->getTile(adventureInt->selection->visitablePos())->tertype;
  381. CCS->musich->playMusic(CCS->musich->terrainMusics[terrain], -1);
  382. }
  383. }
  384. void CBattleInterface::setPrintCellBorders(bool set)
  385. {
  386. curInt->sysOpts.printCellBorders = set;
  387. curInt->sysOpts.settingsChanged();
  388. redrawBackgroundWithHexes(activeStack);
  389. GH.totalRedraw();
  390. }
  391. void CBattleInterface::setPrintStackRange(bool set)
  392. {
  393. curInt->sysOpts.printStackRange = set;
  394. curInt->sysOpts.settingsChanged();
  395. redrawBackgroundWithHexes(activeStack);
  396. GH.totalRedraw();
  397. }
  398. void CBattleInterface::setPrintMouseShadow(bool set)
  399. {
  400. curInt->sysOpts.printMouseShadow = set;
  401. curInt->sysOpts.settingsChanged();
  402. }
  403. void CBattleInterface::activate()
  404. {
  405. activateKeys();
  406. activateMouseMove();
  407. activateRClick();
  408. bOptions->activate();
  409. bSurrender->activate();
  410. bFlee->activate();
  411. bAutofight->activate();
  412. bSpell->activate();
  413. bWait->activate();
  414. bDefence->activate();
  415. for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
  416. {
  417. bfield[b].activate();
  418. }
  419. if(attackingHero)
  420. attackingHero->activate();
  421. if(defendingHero)
  422. defendingHero->activate();
  423. if(curInt->sysOpts.showQueue)
  424. queue->activate();
  425. if(tacticsMode)
  426. {
  427. btactNext->activate();
  428. btactEnd->activate();
  429. }
  430. else
  431. {
  432. bConsoleUp->activate();
  433. bConsoleDown->activate();
  434. }
  435. LOCPLINT->cingconsole->activate();
  436. }
  437. void CBattleInterface::deactivate()
  438. {
  439. deactivateKeys();
  440. deactivateMouseMove();
  441. deactivateRClick();
  442. bOptions->deactivate();
  443. bSurrender->deactivate();
  444. bFlee->deactivate();
  445. bAutofight->deactivate();
  446. bSpell->deactivate();
  447. bWait->deactivate();
  448. bDefence->deactivate();
  449. for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
  450. {
  451. bfield[b].deactivate();
  452. }
  453. if(attackingHero)
  454. attackingHero->deactivate();
  455. if(defendingHero)
  456. defendingHero->deactivate();
  457. if(curInt->sysOpts.showQueue)
  458. queue->deactivate();
  459. if(tacticsMode)
  460. {
  461. btactNext->deactivate();
  462. btactEnd->deactivate();
  463. }
  464. else
  465. {
  466. bConsoleUp->deactivate();
  467. bConsoleDown->deactivate();
  468. }
  469. LOCPLINT->cingconsole->deactivate();
  470. }
  471. void CBattleInterface::show(SDL_Surface * to)
  472. {
  473. std::vector<const CStack*> stacks = curInt->cb->battleGetAllStacks(); //used in a few places
  474. ++animCount;
  475. if(!to) //"evaluating" to
  476. to = screen;
  477. SDL_Rect buf;
  478. SDL_GetClipRect(to, &buf);
  479. SDL_SetClipRect(to, &pos);
  480. //printing background and hexes
  481. if(activeStack != NULL && creAnims[activeStack->ID]->getType() != 0) //show everything with range
  482. {
  483. blitAt(backgroundWithHexes, pos.x, pos.y, to);
  484. }
  485. else
  486. {
  487. //showing background
  488. blitAt(background, pos.x, pos.y, to);
  489. if(curInt->sysOpts.printCellBorders)
  490. {
  491. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, &pos);
  492. }
  493. }
  494. //printing hovered cell
  495. for(int b=0; b<GameConstants::BFIELD_SIZE; ++b)
  496. {
  497. if(bfield[b].strictHovered && bfield[b].hovered)
  498. {
  499. if(previouslyHoveredHex == -1) previouslyHoveredHex = b; //something to start with
  500. if(currentlyHoveredHex == -1) currentlyHoveredHex = b; //something to start with
  501. if(currentlyHoveredHex != b) //repair hover info
  502. {
  503. previouslyHoveredHex = currentlyHoveredHex;
  504. currentlyHoveredHex = b;
  505. }
  506. //print shade
  507. if(spellToCast) //when casting spell
  508. {
  509. //calculating spell school level
  510. const CSpell & spToCast = *CGI->spellh->spells[spellToCast->additionalInfo];
  511. ui8 schoolLevel = 0;
  512. if( activeStack->attackerOwned )
  513. {
  514. if(attackingHeroInstance)
  515. schoolLevel = attackingHeroInstance->getSpellSchoolLevel(&spToCast);
  516. }
  517. else
  518. {
  519. if(defendingHeroInstance)
  520. schoolLevel = defendingHeroInstance->getSpellSchoolLevel(&spToCast);
  521. }
  522. //obtaining range and printing it
  523. std::set<ui16> shaded = spToCast.rangeInHexes(b, schoolLevel);
  524. for(std::set<ui16>::iterator it = shaded.begin(); it != shaded.end(); ++it) //for spells with range greater then one hex
  525. {
  526. if(curInt->sysOpts.printMouseShadow && (*it % GameConstants::BFIELD_WIDTH != 0) && (*it % GameConstants::BFIELD_WIDTH != 16))
  527. {
  528. int x = 14 + ((*it/GameConstants::BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(*it%GameConstants::BFIELD_WIDTH) + pos.x;
  529. int y = 86 + 42 * (*it/GameConstants::BFIELD_WIDTH) + pos.y;
  530. SDL_Rect temp_rect = genRect(cellShade->h, cellShade->w, x, y);
  531. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &temp_rect);
  532. }
  533. }
  534. }
  535. else if(curInt->sysOpts.printMouseShadow) //when not casting spell
  536. {//TODO: do not check it every frame
  537. if (activeStack) //highlight all attackable hexes
  538. {
  539. std::set<SBattleHex> set = curInt->cb->battleGetAttackedHexes(activeStack, currentlyHoveredHex, attackingHex);
  540. BOOST_FOREACH(SBattleHex hex, set)
  541. {
  542. int x = 14 + ((hex/GameConstants::BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(hex%GameConstants::BFIELD_WIDTH) + pos.x;
  543. int y = 86 + 42 * (hex/GameConstants::BFIELD_WIDTH) + pos.y;
  544. SDL_Rect temp_rect = genRect(cellShade->h, cellShade->w, x, y);
  545. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &temp_rect);
  546. }
  547. }
  548. //always highlight pointed hex
  549. int x = 14 + ((b/GameConstants::BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(b%GameConstants::BFIELD_WIDTH) + pos.x;
  550. int y = 86 + 42 * (b/GameConstants::BFIELD_WIDTH) + pos.y;
  551. SDL_Rect temp_rect = genRect(cellShade->h, cellShade->w, x, y);
  552. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &temp_rect);
  553. }
  554. }
  555. }
  556. SDL_SetClipRect(to, &buf); //restoring previous clip_rect
  557. //prevents blitting outside this window
  558. SDL_GetClipRect(to, &buf);
  559. SDL_SetClipRect(to, &pos);
  560. //preparing obstacles to be shown
  561. std::vector<CObstacleInstance> obstacles = curInt->cb->battleGetAllObstacles();
  562. std::multimap<SBattleHex, int> hexToObstacle;
  563. for(size_t b = 0; b < obstacles.size(); ++b)
  564. {
  565. SBattleHex position = CGI->heroh->obstacles.find(obstacles[b].ID)->second.getMaxBlocked(obstacles[b].pos);
  566. hexToObstacle.insert(std::make_pair(position, b));
  567. }
  568. ////showing units //a lot of work...
  569. std::vector<const CStack *> stackAliveByHex[GameConstants::BFIELD_SIZE];
  570. //double loop because dead stacks should be printed first
  571. for (size_t i = 0; i < stacks.size(); i++)
  572. {
  573. const CStack *s = stacks[i];
  574. if(creAnims.find(s->ID) == creAnims.end()) //e.g. for summoned but not yet handled stacks
  575. continue;
  576. if(creAnims[s->ID]->getType() != 5 && s->position >= 0) //don't show turrets here
  577. stackAliveByHex[s->position].push_back(s);
  578. }
  579. std::vector<const CStack *> stackDeadByHex[GameConstants::BFIELD_SIZE];
  580. for (size_t i = 0; i < stacks.size(); i++)
  581. {
  582. const CStack *s = stacks[i];
  583. if(creAnims.find(s->ID) == creAnims.end()) //e.g. for summoned but not yet handled stacks
  584. continue;
  585. if(creAnims[s->ID]->getType() == 5)
  586. stackDeadByHex[s->position].push_back(s);
  587. }
  588. //handle animations
  589. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = pendingAnims.begin(); it != pendingAnims.end(); ++it)
  590. {
  591. if(!it->first) //this animation should be deleted
  592. continue;
  593. if(!it->second)
  594. {
  595. it->second = it->first->init();
  596. }
  597. if(it->second && it->first)
  598. it->first->nextFrame();
  599. }
  600. //delete anims
  601. int preSize = pendingAnims.size();
  602. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = pendingAnims.begin(); it != pendingAnims.end(); ++it)
  603. {
  604. if(it->first == NULL)
  605. {
  606. pendingAnims.erase(it);
  607. it = pendingAnims.begin();
  608. break;
  609. }
  610. }
  611. if(preSize > 0 && pendingAnims.size() == 0)
  612. {
  613. //action finished, restore the interface
  614. if(!active)
  615. activate();
  616. //activation of next stack
  617. if(pendingAnims.size() == 0 && stackToActivate != NULL)
  618. {
  619. activateStack();
  620. }
  621. //anims ended
  622. animsAreDisplayed.setn(false);
  623. }
  624. for(int b=0; b<GameConstants::BFIELD_SIZE; ++b) //showing dead stacks
  625. {
  626. for(size_t v=0; v<stackDeadByHex[b].size(); ++v)
  627. {
  628. creAnims[stackDeadByHex[b][v]->ID]->nextFrame(to, creAnims[stackDeadByHex[b][v]->ID]->pos.x, creAnims[stackDeadByHex[b][v]->ID]->pos.y, creDir[stackDeadByHex[b][v]->ID], animCount, false); //increment always when moving, never if stack died
  629. }
  630. }
  631. std::vector<const CStack *> flyingStacks; //flying stacks should be displayed later, over other stacks and obstacles
  632. if (!siegeH)
  633. {
  634. for(int b = 0; b < GameConstants::BFIELD_SIZE; ++b) //showing alive stacks
  635. {
  636. showAliveStacks(stackAliveByHex, b, &flyingStacks, to);
  637. showObstacles(&hexToObstacle, obstacles, b, to);
  638. showPieceOfWall(to, b, stacks);
  639. }
  640. }
  641. // Siege drawing
  642. else
  643. {
  644. for (int i = 0; i < 4; i++)
  645. {
  646. // xMin, xMax => go from hex x pos to hex x pos
  647. // yMin, yMax => go from hex y pos to hex y pos
  648. // xMove => 0: left side, 1: right side
  649. // xMoveDir => 0: decrement, 1: increment, alters every second hex line either xMin or xMax depending on xMove
  650. int xMin, xMax, yMin, yMax, xMove, xMoveDir = 0;
  651. switch (i)
  652. {
  653. // display units shown at the upper left side
  654. case 0:
  655. xMin = 0;
  656. yMin = 0;
  657. xMax = 11;
  658. yMax = 4;
  659. xMove = 1;
  660. break;
  661. // display wall/units shown at the upper wall area/right upper side
  662. case 1:
  663. xMin = 12;
  664. yMin = 0;
  665. xMax = 16;
  666. yMax = 4;
  667. xMove = 0;
  668. break;
  669. // display units shown at the lower wall area/right lower side
  670. case 2:
  671. xMin = 10;
  672. yMin = 5;
  673. xMax = 16;
  674. yMax = 10;
  675. xMove = 0;
  676. xMoveDir = 1;
  677. break;
  678. // display units shown at the left lower side
  679. case 3:
  680. xMin = 0;
  681. yMin = 5;
  682. xMax = 9;
  683. yMax = 10;
  684. xMove = 1;
  685. xMoveDir = 1;
  686. break;
  687. }
  688. int runNum = 0;
  689. for (int j = yMin; j <= yMax; j++)
  690. {
  691. if (runNum > 0)
  692. {
  693. if (xMin == xMax)
  694. xMax = xMin = ((runNum % 2) == 0) ? (xMin + (xMoveDir == 0 ? -1 : 1)) : xMin;
  695. else if (xMove == 1)
  696. xMax = ((runNum % 2) == 0) ? (xMax + (xMoveDir == 0 ? -1 : 1)) : xMax;
  697. else if (xMove == 0)
  698. xMin = ((runNum % 2) == 0) ? (xMin + (xMoveDir == 0 ? -1 : 1)) : xMin;
  699. }
  700. for (int k = xMin; k <= xMax; k++)
  701. {
  702. int hex = j * 17 + k;
  703. showAliveStacks(stackAliveByHex, hex, &flyingStacks, to);
  704. showObstacles(&hexToObstacle, obstacles, hex, to);
  705. showPieceOfWall(to, hex, stacks);
  706. }
  707. ++runNum;
  708. }
  709. }
  710. }
  711. for(size_t b = 0; b < flyingStacks.size(); ++b) //showing flying stacks
  712. showAliveStack(flyingStacks[b], to);
  713. //units shown
  714. // Show projectiles
  715. projectileShowHelper(to);
  716. //showing spell effects
  717. if(battleEffects.size())
  718. {
  719. for(std::list<SBattleEffect>::iterator it = battleEffects.begin(); it!=battleEffects.end(); ++it)
  720. {
  721. SDL_Surface * bitmapToBlit = it->anim->ourImages[(it->frame)%it->anim->ourImages.size()].bitmap;
  722. SDL_Rect temp_rect = genRect(bitmapToBlit->h, bitmapToBlit->w, it->x, it->y);
  723. SDL_BlitSurface(bitmapToBlit, NULL, to, &temp_rect);
  724. }
  725. }
  726. SDL_SetClipRect(to, &buf); //restoring previous clip_rect
  727. //showing menu background and console
  728. blitAt(menu, pos.x, 556 + pos.y, to);
  729. if(tacticsMode)
  730. {
  731. btactNext->showAll(to);
  732. btactEnd->showAll(to);
  733. }
  734. else
  735. {
  736. console->showAll(to);
  737. bConsoleUp->showAll(to);
  738. bConsoleDown->showAll(to);
  739. }
  740. //showing buttons
  741. bOptions->showAll(to);
  742. bSurrender->showAll(to);
  743. bFlee->showAll(to);
  744. bAutofight->showAll(to);
  745. bSpell->showAll(to);
  746. bWait->showAll(to);
  747. bDefence->showAll(to);
  748. //showing window with result of battle
  749. if(resWindow)
  750. {
  751. resWindow->show(to);
  752. }
  753. //showing in-game console
  754. LOCPLINT->cingconsole->show(to);
  755. SRect posWithQueue = SRect(pos.x, pos.y, 800, 600);
  756. if(curInt->sysOpts.showQueue)
  757. {
  758. if(!queue->embedded)
  759. {
  760. posWithQueue.y -= queue->pos.h;
  761. posWithQueue.h += queue->pos.h;
  762. }
  763. //showing queue
  764. if(!bresult)
  765. queue->showAll(to);
  766. else
  767. queue->blitBg(to); //blit only background, stacks are deleted
  768. }
  769. //printing border around interface
  770. if(screen->w != 800 || screen->h !=600)
  771. {
  772. CMessage::drawBorder(curInt->playerID,to,posWithQueue.w + 28, posWithQueue.h + 28, posWithQueue.x-14, posWithQueue.y-15);
  773. }
  774. }
  775. void CBattleInterface::showAliveStacks(std::vector<const CStack *> *aliveStacks, int hex, std::vector<const CStack *> *flyingStacks, SDL_Surface *to)
  776. {
  777. //showing hero animations
  778. if (hex == 0)
  779. if(attackingHero)
  780. attackingHero->show(to);
  781. if (hex == 16)
  782. if(defendingHero)
  783. defendingHero->show(to);
  784. for(size_t v = 0; v < aliveStacks[hex].size(); ++v)
  785. {
  786. const CStack *s = aliveStacks[hex][v];
  787. if(!s->hasBonusOfType(Bonus::FLYING) || creAnims[s->ID]->getType() != 0)
  788. showAliveStack(s, to);
  789. else
  790. flyingStacks->push_back(s);
  791. }
  792. }
  793. void CBattleInterface::showObstacles(std::multimap<SBattleHex, int> *hexToObstacle, std::vector<CObstacleInstance> &obstacles, int hex, SDL_Surface *to)
  794. {
  795. std::pair<std::multimap<SBattleHex, int>::const_iterator, std::multimap<SBattleHex, int>::const_iterator> obstRange =
  796. hexToObstacle->equal_range(hex);
  797. for(std::multimap<SBattleHex, int>::const_iterator it = obstRange.first; it != obstRange.second; ++it)
  798. {
  799. CObstacleInstance & curOb = obstacles[it->second];
  800. std::pair<si16, si16> shift = CGI->heroh->obstacles.find(curOb.ID)->second.posShift;
  801. int x = ((curOb.pos/GameConstants::BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(curOb.pos%GameConstants::BFIELD_WIDTH) + pos.x + shift.first;
  802. int y = 86 + 42 * (curOb.pos/GameConstants::BFIELD_WIDTH) + pos.y + shift.second;
  803. std::vector<Cimage> &images = idToObstacle[curOb.ID]->ourImages; //reference to animation of obstacle
  804. blitAt(images[((animCount+1)/(4/curInt->sysOpts.animSpeed))%images.size()].bitmap, x, y, to);
  805. }
  806. }
  807. void CBattleInterface::keyPressed(const SDL_KeyboardEvent & key)
  808. {
  809. if(key.keysym.sym == SDLK_q && key.state == SDL_PRESSED)
  810. {
  811. if(curInt->sysOpts.showQueue) //hide queue
  812. hideQueue();
  813. else
  814. showQueue();
  815. curInt->sysOpts.settingsChanged();
  816. }
  817. else if(key.keysym.sym == SDLK_ESCAPE && spellDestSelectMode)
  818. {
  819. endCastingSpell();
  820. }
  821. }
  822. void CBattleInterface::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  823. {
  824. if(activeStack && !spellDestSelectMode)
  825. {
  826. int lastMouseHoveredStack = mouseHoveredStack;
  827. bool stackCastsSpell;
  828. mouseHoveredStack = -1;
  829. int myNumber = -1; //number of hovered tile
  830. for(int g = 0; g < GameConstants::BFIELD_SIZE; ++g)
  831. {
  832. if(bfield[g].hovered && bfield[g].strictHovered)
  833. {
  834. myNumber = g;
  835. break;
  836. }
  837. }
  838. if(myNumber == -1)
  839. {
  840. CCS->curh->changeGraphic(1, 6);
  841. if(console->whoSetAlter == 0)
  842. {
  843. console->alterTxt = "";
  844. }
  845. }
  846. else //battlefield hex
  847. {
  848. if(!vstd::contains(occupyableHexes, myNumber) || activeStack->coversPos(myNumber))
  849. {
  850. const CStack *shere = curInt->cb->battleGetStackByPos(myNumber, false);
  851. const CStack *sactive = activeStack;
  852. if(shere)
  853. {
  854. bool ourStack = shere->owner == curInt->playerID;
  855. //determine if creature spell is going to be cast
  856. stackCastsSpell = false;
  857. if (stackCanCastSpell && spellSelMode > STACK_SPELL_CANCELLED) //player did not decide to cancel this spell
  858. {
  859. if ((int)creatureSpellToCast > -1) //use randomized spell (Faerie Dragon), or only avaliable spell (Archangel)
  860. {
  861. const CSpell * spell = CGI->spellh->spells[creatureSpellToCast];
  862. if (curInt->cb->battleCanCastThisSpell(spell, SBattleHex(myNumber)) == ESpellCastProblem::OK)
  863. {
  864. if ((spell->positiveness > -1 && ourStack) || (spell->positiveness < 1 && !ourStack))
  865. {
  866. CCS->curh->changeGraphic(3, 0);
  867. stackCastsSpell = true;
  868. std::string buf = CGI->generaltexth->allTexts[27]; //cast %s on &s
  869. boost::replace_first (buf, "%s", spell->name);
  870. boost::replace_first (buf, "%s", shere->getName());
  871. console->alterTxt = buf;
  872. console->whoSetAlter = 0;
  873. }
  874. }
  875. }
  876. else if (ourStack) //must have only random positive spell (genie)
  877. {
  878. if (shere != sactive) //can't cast on itself
  879. {
  880. int spellID = curInt->cb->battleGetRandomStackSpell(shere, CBattleInfoCallback::RANDOM_GENIE);
  881. if (spellID > -1) //can cast any spell on target stack
  882. {
  883. CCS->curh->changeGraphic(3, 0);
  884. stackCastsSpell = true;
  885. std::string buf = CGI->generaltexth->allTexts[301]; //Cast spell on %s
  886. boost::replace_first (buf, "%s", shere->getName());
  887. console->alterTxt = buf;
  888. console->whoSetAlter = 0;
  889. }
  890. }
  891. }
  892. }
  893. if(ourStack) //our stack
  894. {
  895. if (shere->alive())
  896. {
  897. if (!stackCastsSpell) //use other abilities or display info
  898. {
  899. if(sactive->hasBonusOfType(Bonus::HEALER))
  900. {
  901. //display the possibility to heal this creature
  902. CCS->curh->changeGraphic(1, 17);
  903. }
  904. else
  905. {
  906. //info about creature
  907. CCS->curh->changeGraphic(1,5);
  908. }
  909. //setting console text
  910. char buf[500];
  911. sprintf(buf, CGI->generaltexth->allTexts[297].c_str(), shere->count == 1 ? shere->getCreature()->nameSing.c_str() : shere->getCreature()->namePl.c_str());
  912. console->alterTxt = buf;
  913. console->whoSetAlter = 0;
  914. const time_t curTime = time(NULL);
  915. if (shere->ID != lastMouseHoveredStack &&
  916. curTime > lastMouseHoveredStackAnimationTime + HOVER_ANIM_DELTA &&
  917. creAnims[shere->ID]->getType() == CCreatureAnim::HOLDING &&
  918. creAnims[shere->ID]->framesInGroup(CCreatureAnim::MOUSEON) > 0)
  919. {
  920. creAnims[shere->ID]->playOnce(CCreatureAnim::MOUSEON);
  921. lastMouseHoveredStackAnimationTime = curTime;
  922. }
  923. }
  924. } //end of alive
  925. else if (sactive->hasBonusOfType(Bonus::DAEMON_SUMMONING) && sactive->casts)
  926. {
  927. CCS->curh->changeGraphic(3, 0);
  928. }
  929. mouseHoveredStack = shere->ID; //for dead also?
  930. }
  931. //end of our stack
  932. else if (!stackCastsSpell) //if not, then try attack
  933. {
  934. if (curInt->cb->battleCanShoot(activeStack,myNumber)) //we can shoot enemy
  935. {
  936. if(curInt->cb->battleHasDistancePenalty(activeStack, myNumber) ||
  937. curInt->cb->battleHasWallPenalty(activeStack, myNumber))
  938. {
  939. CCS->curh->changeGraphic(1,15);
  940. }
  941. else
  942. {
  943. CCS->curh->changeGraphic(1,3);
  944. }
  945. //setting console text
  946. char buf[500];
  947. //calculating estimated dmg
  948. std::pair<ui32, ui32> estimatedDmg = curInt->cb->battleEstimateDamage(sactive, shere);
  949. std::ostringstream estDmg;
  950. estDmg << estimatedDmg.first << " - " << estimatedDmg.second;
  951. //printing
  952. sprintf(buf, CGI->generaltexth->allTexts[296].c_str(), shere->count == 1 ? shere->getCreature()->nameSing.c_str() : shere->getCreature()->namePl.c_str(),
  953. sactive->shots, estDmg.str().c_str());
  954. console->alterTxt = buf;
  955. console->whoSetAlter = 0;
  956. }
  957. else if (isTileAttackable(myNumber)) //available enemy (melee attackable)
  958. {
  959. //handle direction of cursor and attackable tile
  960. setBattleCursor(myNumber);
  961. //setting console info
  962. char buf[500];
  963. //calculating estimated dmg
  964. std::pair<ui32, ui32> estimatedDmg = curInt->cb->battleEstimateDamage(sactive, shere);
  965. std::ostringstream estDmg;
  966. estDmg << estimatedDmg.first << " - " << estimatedDmg.second;
  967. //printing
  968. sprintf(buf, CGI->generaltexth->allTexts[36].c_str(),
  969. shere->count == 1 ? shere->getCreature()->nameSing.c_str() : shere->getCreature()->namePl.c_str(), estDmg.str().c_str());
  970. console->alterTxt = buf;
  971. console->whoSetAlter = 0;
  972. }
  973. else //unavailable enemy
  974. {
  975. CCS->curh->changeGraphic(1,0);
  976. console->alterTxt = "";
  977. console->whoSetAlter = 0;
  978. }
  979. }
  980. } //end of stack
  981. //TODO: allow aiming for creature spells
  982. else if (sactive && sactive->hasBonusOfType(Bonus::CATAPULT) && isCatapultAttackable(myNumber)) //catapulting
  983. {
  984. CCS->curh->changeGraphic(1,16);
  985. console->alterTxt = "";
  986. console->whoSetAlter = 0;
  987. }
  988. else //empty unavailable tile
  989. {
  990. CCS->curh->changeGraphic(1,0);
  991. console->alterTxt = "";
  992. console->whoSetAlter = 0;
  993. }
  994. }
  995. else //available tile
  996. {
  997. //setting console text and cursor
  998. if(activeStack) //there can be a moment when stack is dead ut next is not yet activated
  999. {
  1000. char buf[500];
  1001. if(activeStack->hasBonusOfType(Bonus::FLYING))
  1002. {
  1003. CCS->curh->changeGraphic(1,2);
  1004. sprintf(buf, CGI->generaltexth->allTexts[295].c_str(),
  1005. activeStack->count == 1 ? activeStack->getCreature()->nameSing.c_str() : activeStack->getCreature()->namePl.c_str());
  1006. }
  1007. else
  1008. {
  1009. CCS->curh->changeGraphic(1,1);
  1010. sprintf(buf, CGI->generaltexth->allTexts[294].c_str(),
  1011. activeStack->count == 1 ? activeStack->getCreature()->nameSing.c_str() : activeStack->getCreature()->namePl.c_str());
  1012. }
  1013. console->alterTxt = buf;
  1014. console->whoSetAlter = 0;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. else if(spellDestSelectMode)
  1020. {
  1021. int myNumber = -1; //number of hovered tile
  1022. for(int g=0; g<GameConstants::BFIELD_SIZE; ++g)
  1023. {
  1024. if(bfield[g].hovered && bfield[g].strictHovered)
  1025. {
  1026. myNumber = g;
  1027. break;
  1028. }
  1029. }
  1030. if(myNumber == -1)
  1031. {
  1032. CCS->curh->changeGraphic(1, 0);
  1033. //setting console text
  1034. console->alterTxt = CGI->generaltexth->allTexts[23];
  1035. console->whoSetAlter = 0;
  1036. }
  1037. else
  1038. {
  1039. //get dead stack if we cast resurrection or animate dead
  1040. const CStack * stackUnder = curInt->cb->battleGetStackByPos(myNumber, spellToCast->additionalInfo != 38 && spellToCast->additionalInfo != 39);
  1041. if(stackUnder && spellToCast->additionalInfo == 39 && !stackUnder->hasBonusOfType(Bonus::UNDEAD)) //animate dead can be cast only on undead creatures
  1042. stackUnder = NULL;
  1043. bool whichCase; //for cases 1, 2 and 3
  1044. switch(spellSelMode)
  1045. {
  1046. case 1:
  1047. whichCase = stackUnder != NULL && curInt->playerID == stackUnder->owner;
  1048. break;
  1049. case 2:
  1050. whichCase = stackUnder != NULL && curInt->playerID != stackUnder->owner;
  1051. break;
  1052. case 3:
  1053. whichCase = stackUnder != NULL;
  1054. break;
  1055. }
  1056. switch(spellSelMode)
  1057. {
  1058. case 0:
  1059. CCS->curh->changeGraphic(3, 0);
  1060. //setting console text
  1061. char buf[500];
  1062. sprintf(buf, CGI->generaltexth->allTexts[26].c_str(), CGI->spellh->spells[spellToCast->additionalInfo]->name.c_str());
  1063. console->alterTxt = buf;
  1064. console->whoSetAlter = 0;
  1065. break;
  1066. case 1: case 2: case 3:
  1067. if( whichCase )
  1068. {
  1069. CCS->curh->changeGraphic(3, 0);
  1070. //setting console text
  1071. char buf[500];
  1072. std::string creName = stackUnder->count > 1 ? stackUnder->getCreature()->namePl : stackUnder->getCreature()->nameSing;
  1073. sprintf(buf, CGI->generaltexth->allTexts[27].c_str(), CGI->spellh->spells[spellToCast->additionalInfo]->name.c_str(), creName.c_str());
  1074. console->alterTxt = buf;
  1075. console->whoSetAlter = 0;
  1076. break;
  1077. }
  1078. else
  1079. {
  1080. CCS->curh->changeGraphic(1, 0);
  1081. //setting console text
  1082. console->alterTxt = CGI->generaltexth->allTexts[23];
  1083. console->whoSetAlter = 0;
  1084. }
  1085. break;
  1086. case 4: //TODO: implement this case
  1087. if( blockedByObstacle(myNumber) )
  1088. {
  1089. CCS->curh->changeGraphic(3, 0);
  1090. }
  1091. else
  1092. {
  1093. CCS->curh->changeGraphic(1, 0);
  1094. }
  1095. break;
  1096. }
  1097. }
  1098. }
  1099. }
  1100. void CBattleInterface::setBattleCursor(const int myNumber)
  1101. {
  1102. const CClickableHex & hoveredHex = bfield[myNumber];
  1103. CCursorHandler *cursor = CCS->curh;
  1104. const double subdividingAngle = 2.0*M_PI/6.0; // Divide a hex into six sectors.
  1105. const double hexMidX = hoveredHex.pos.x + hoveredHex.pos.w/2;
  1106. const double hexMidY = hoveredHex.pos.y + hoveredHex.pos.h/2;
  1107. const double cursorHexAngle = M_PI - atan2(hexMidY - cursor->ypos, cursor->xpos - hexMidX) + subdividingAngle/2; //TODO: refactor this nightmare
  1108. const double sector = fmod(cursorHexAngle/subdividingAngle, 6.0);
  1109. const int zigzagCorrection = !((myNumber/GameConstants::BFIELD_WIDTH)%2); // Off-by-one correction needed to deal with the odd battlefield rows.
  1110. std::vector<int> sectorCursor; // From left to bottom left.
  1111. sectorCursor.push_back(8);
  1112. sectorCursor.push_back(9);
  1113. sectorCursor.push_back(10);
  1114. sectorCursor.push_back(11);
  1115. sectorCursor.push_back(12);
  1116. sectorCursor.push_back(7);
  1117. const bool doubleWide = activeStack->doubleWide();
  1118. bool aboveAttackable = true, belowAttackable = true;
  1119. // Exclude directions which cannot be attacked from.
  1120. // Check to the left.
  1121. if (myNumber%GameConstants::BFIELD_WIDTH <= 1 || !vstd::contains(occupyableHexes, myNumber - 1))
  1122. {
  1123. sectorCursor[0] = -1;
  1124. }
  1125. // Check top left, top right as well as above for 2-hex creatures.
  1126. if (myNumber/GameConstants::BFIELD_WIDTH == 0)
  1127. {
  1128. sectorCursor[1] = -1;
  1129. sectorCursor[2] = -1;
  1130. aboveAttackable = false;
  1131. }
  1132. else
  1133. {
  1134. if (doubleWide)
  1135. {
  1136. bool attackRow[4] = {true, true, true, true};
  1137. if (myNumber%GameConstants::BFIELD_WIDTH <= 1 || !vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH - 2 + zigzagCorrection))
  1138. attackRow[0] = false;
  1139. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  1140. attackRow[1] = false;
  1141. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH + zigzagCorrection))
  1142. attackRow[2] = false;
  1143. if (myNumber%GameConstants::BFIELD_WIDTH >= GameConstants::BFIELD_WIDTH - 2 || !vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH + 1 + zigzagCorrection))
  1144. attackRow[3] = false;
  1145. if (!(attackRow[0] && attackRow[1]))
  1146. sectorCursor[1] = -1;
  1147. if (!(attackRow[1] && attackRow[2]))
  1148. aboveAttackable = false;
  1149. if (!(attackRow[2] && attackRow[3]))
  1150. sectorCursor[2] = -1;
  1151. }
  1152. else
  1153. {
  1154. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  1155. sectorCursor[1] = -1;
  1156. if (!vstd::contains(occupyableHexes, myNumber - GameConstants::BFIELD_WIDTH + zigzagCorrection))
  1157. sectorCursor[2] = -1;
  1158. }
  1159. }
  1160. // Check to the right.
  1161. if (myNumber%GameConstants::BFIELD_WIDTH >= GameConstants::BFIELD_WIDTH - 2 || !vstd::contains(occupyableHexes, myNumber + 1))
  1162. {
  1163. sectorCursor[3] = -1;
  1164. }
  1165. // Check bottom right, bottom left as well as below for 2-hex creatures.
  1166. if (myNumber/GameConstants::BFIELD_WIDTH == GameConstants::BFIELD_HEIGHT - 1)
  1167. {
  1168. sectorCursor[4] = -1;
  1169. sectorCursor[5] = -1;
  1170. belowAttackable = false;
  1171. }
  1172. else
  1173. {
  1174. if (doubleWide)
  1175. {
  1176. bool attackRow[4] = {true, true, true, true};
  1177. if (myNumber%GameConstants::BFIELD_WIDTH <= 1 || !vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH - 2 + zigzagCorrection))
  1178. attackRow[0] = false;
  1179. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  1180. attackRow[1] = false;
  1181. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH + zigzagCorrection))
  1182. attackRow[2] = false;
  1183. if (myNumber%GameConstants::BFIELD_WIDTH >= GameConstants::BFIELD_WIDTH - 2 || !vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH + 1 + zigzagCorrection))
  1184. attackRow[3] = false;
  1185. if (!(attackRow[0] && attackRow[1]))
  1186. sectorCursor[5] = -1;
  1187. if (!(attackRow[1] && attackRow[2]))
  1188. belowAttackable = false;
  1189. if (!(attackRow[2] && attackRow[3]))
  1190. sectorCursor[4] = -1;
  1191. }
  1192. else
  1193. {
  1194. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH + zigzagCorrection))
  1195. sectorCursor[4] = -1;
  1196. if (!vstd::contains(occupyableHexes, myNumber + GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection))
  1197. sectorCursor[5] = -1;
  1198. }
  1199. }
  1200. // Determine index from sector.
  1201. int cursorIndex;
  1202. if (doubleWide)
  1203. {
  1204. sectorCursor.insert(sectorCursor.begin() + 5, belowAttackable ? 13 : -1);
  1205. sectorCursor.insert(sectorCursor.begin() + 2, aboveAttackable ? 14 : -1);
  1206. if (sector < 1.5)
  1207. cursorIndex = sector;
  1208. else if (sector >= 1.5 && sector < 2.5)
  1209. cursorIndex = 2;
  1210. else if (sector >= 2.5 && sector < 4.5)
  1211. cursorIndex = (int) sector + 1;
  1212. else if (sector >= 4.5 && sector < 5.5)
  1213. cursorIndex = 6;
  1214. else
  1215. cursorIndex = (int) sector + 2;
  1216. }
  1217. else
  1218. {
  1219. cursorIndex = sector;
  1220. }
  1221. // Find the closest direction attackable, starting with the right one.
  1222. // FIXME: Is this really how the original H3 client does it?
  1223. int i = 0;
  1224. while (sectorCursor[(cursorIndex + i)%sectorCursor.size()] == -1) //Why hast thou forsaken me?
  1225. i = i <= 0 ? 1 - i : -i; // 0, 1, -1, 2, -2, 3, -3 etc..
  1226. int index = (cursorIndex + i)%sectorCursor.size(); //hopefully we get elements from sectorCursor
  1227. cursor->changeGraphic(1, sectorCursor[index]);
  1228. switch (index)
  1229. {
  1230. case 0:
  1231. attackingHex = myNumber - 1; //left
  1232. break;
  1233. case 1:
  1234. attackingHex = myNumber - GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection; //top left
  1235. break;
  1236. case 2:
  1237. attackingHex = myNumber - GameConstants::BFIELD_WIDTH + zigzagCorrection; //top right
  1238. break;
  1239. case 3:
  1240. break;
  1241. attackingHex = myNumber + 1; //right
  1242. case 4:
  1243. break;
  1244. attackingHex = myNumber + GameConstants::BFIELD_WIDTH + zigzagCorrection; //bottom right
  1245. case 5:
  1246. attackingHex = myNumber + GameConstants::BFIELD_WIDTH - 1 + zigzagCorrection; //bottom left
  1247. break;
  1248. }
  1249. SBattleHex hex(attackingHex);
  1250. if (!hex.isValid())
  1251. attackingHex = -1;
  1252. }
  1253. void CBattleInterface::clickRight(tribool down, bool previousState)
  1254. {
  1255. if(!down && spellDestSelectMode)
  1256. {
  1257. endCastingSpell();
  1258. }
  1259. }
  1260. void CBattleInterface::bOptionsf()
  1261. {
  1262. if(spellDestSelectMode) //we are casting a spell
  1263. return;
  1264. CCS->curh->changeGraphic(0,0);
  1265. SRect tempRect = genRect(431, 481, 160, 84);
  1266. tempRect += pos.topLeft();
  1267. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(tempRect, this);
  1268. GH.pushInt(optionsWin);
  1269. }
  1270. void CBattleInterface::bSurrenderf()
  1271. {
  1272. if(spellDestSelectMode) //we are casting a spell
  1273. return;
  1274. int cost = curInt->cb->battleGetSurrenderCost();
  1275. if(cost >= 0)
  1276. {
  1277. const CGHeroInstance *opponent = curInt->cb->battleGetFightingHero(1);
  1278. std::string enemyHeroName = opponent ? opponent->name : "#ENEMY#"; //TODO: should surrendering without enemy hero be enabled?
  1279. std::string surrenderMessage = boost::str(boost::format(CGI->generaltexth->allTexts[32]) % enemyHeroName % cost); //%s states: "I will accept your surrender and grant you and your troops safe passage for the price of %d gold."
  1280. curInt->showYesNoDialog(surrenderMessage, std::vector<SComponent*>(), boost::bind(&CBattleInterface::reallySurrender,this), 0, false);
  1281. }
  1282. }
  1283. void CBattleInterface::bFleef()
  1284. {
  1285. if(spellDestSelectMode) //we are casting a spell
  1286. return;
  1287. if( curInt->cb->battleCanFlee() )
  1288. {
  1289. CFunctionList<void()> ony = boost::bind(&CBattleInterface::reallyFlee,this);
  1290. curInt->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, 0, false); //Are you sure you want to retreat?
  1291. }
  1292. else
  1293. {
  1294. std::vector<SComponent*> comps;
  1295. std::string heroName;
  1296. //calculating fleeing hero's name
  1297. if(attackingHeroInstance)
  1298. if(attackingHeroInstance->tempOwner == curInt->cb->getMyColor())
  1299. heroName = attackingHeroInstance->name;
  1300. if(defendingHeroInstance)
  1301. if(defendingHeroInstance->tempOwner == curInt->cb->getMyColor())
  1302. heroName = defendingHeroInstance->name;
  1303. //calculating text
  1304. char buffer[1000];
  1305. sprintf(buffer, CGI->generaltexth->allTexts[340].c_str(), heroName.c_str()); //The Shackles of War are present. %s can not retreat!
  1306. //printing message
  1307. curInt->showInfoDialog(std::string(buffer), comps);
  1308. }
  1309. }
  1310. void CBattleInterface::reallyFlee()
  1311. {
  1312. giveCommand(BattleAction::RETREAT,0,0);
  1313. CCS->curh->changeGraphic(0, 0);
  1314. }
  1315. void CBattleInterface::reallySurrender()
  1316. {
  1317. if(curInt->cb->getResourceAmount(Res::GOLD) < curInt->cb->battleGetSurrenderCost())
  1318. {
  1319. curInt->showInfoDialog(CGI->generaltexth->allTexts[29]); //You don't have enough gold!
  1320. }
  1321. else
  1322. {
  1323. giveCommand(BattleAction::SURRENDER,0,0);
  1324. CCS->curh->changeGraphic(0, 0);
  1325. }
  1326. }
  1327. void CBattleInterface::bAutofightf()
  1328. {
  1329. if(spellDestSelectMode) //we are casting a spell
  1330. return;
  1331. }
  1332. void CBattleInterface::bSpellf()
  1333. {
  1334. if(spellDestSelectMode) //we are casting a spell
  1335. return;
  1336. CCS->curh->changeGraphic(0,0);
  1337. const CGHeroInstance * chi = NULL;
  1338. if(attackingHeroInstance->tempOwner == curInt->playerID)
  1339. chi = attackingHeroInstance;
  1340. else
  1341. chi = defendingHeroInstance;
  1342. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), chi, curInt);
  1343. GH.pushInt(spellWindow);
  1344. }
  1345. void CBattleInterface::bWaitf()
  1346. {
  1347. if(spellDestSelectMode) //we are casting a spell
  1348. return;
  1349. if(activeStack != NULL)
  1350. giveCommand(8,0,activeStack->ID);
  1351. }
  1352. void CBattleInterface::bDefencef()
  1353. {
  1354. if(spellDestSelectMode) //we are casting a spell
  1355. return;
  1356. if(activeStack != NULL)
  1357. giveCommand(3,0,activeStack->ID);
  1358. }
  1359. void CBattleInterface::bConsoleUpf()
  1360. {
  1361. if(spellDestSelectMode) //we are casting a spell
  1362. return;
  1363. console->scrollUp();
  1364. }
  1365. void CBattleInterface::bConsoleDownf()
  1366. {
  1367. if(spellDestSelectMode) //we are casting a spell
  1368. return;
  1369. console->scrollDown();
  1370. }
  1371. void CBattleInterface::newStack(const CStack * stack)
  1372. {
  1373. SPoint coords = CClickableHex::getXYUnitAnim(stack->position, stack->owner == attackingHeroInstance->tempOwner, stack, this);;
  1374. if(stack->position < 0) //turret
  1375. {
  1376. const CCreature & turretCreature = *CGI->creh->creatures[ CGI->creh->factionToTurretCreature[siegeH->town->town->typeID] ];
  1377. creAnims[stack->ID] = new CCreatureAnimation(turretCreature.animDefName);
  1378. // Turret positions are read out of the /config/wall_pos.txt
  1379. int posID = 0;
  1380. switch (stack->position)
  1381. {
  1382. case -2: // keep creature
  1383. posID = 18;
  1384. break;
  1385. case -3: // bottom creature
  1386. posID = 19;
  1387. break;
  1388. case -4: // upper creature
  1389. posID = 20;
  1390. break;
  1391. }
  1392. if (posID != 0)
  1393. {
  1394. coords.x = graphics->wallPositions[siegeH->town->town->typeID][posID - 1].x + this->pos.x;
  1395. coords.y = graphics->wallPositions[siegeH->town->town->typeID][posID - 1].y + this->pos.y;
  1396. }
  1397. }
  1398. else
  1399. {
  1400. creAnims[stack->ID] = new CCreatureAnimation(stack->getCreature()->animDefName);
  1401. }
  1402. creAnims[stack->ID]->setType(CCreatureAnim::HOLDING);
  1403. creAnims[stack->ID]->pos = SRect(coords.x, coords.y, creAnims[stack->ID]->fullWidth, creAnims[stack->ID]->fullHeight);
  1404. creDir[stack->ID] = stack->attackerOwned;
  1405. }
  1406. void CBattleInterface::stackRemoved(int stackID)
  1407. {
  1408. delete creAnims[stackID];
  1409. creAnims.erase(stackID);
  1410. creDir.erase(stackID);
  1411. }
  1412. void CBattleInterface::stackActivated(const CStack * stack) //TODO: check it all before game state is changed due to abilities
  1413. {
  1414. //givenCommand = NULL;
  1415. stackToActivate = stack;
  1416. if(pendingAnims.size() == 0)
  1417. activateStack();
  1418. }
  1419. void CBattleInterface::stackMoved(const CStack * stack, std::vector<SBattleHex> destHex, int distance)
  1420. {
  1421. addNewAnim(new CMovementAnimation(this, stack, destHex, distance));
  1422. waitForAnims();
  1423. }
  1424. void CBattleInterface::stacksAreAttacked(std::vector<SStackAttackedInfo> attackedInfos)
  1425. {
  1426. for(size_t h = 0; h < attackedInfos.size(); ++h)
  1427. {
  1428. addNewAnim(new CDefenceAnimation(attackedInfos[h], this));
  1429. if (attackedInfos[h].rebirth)
  1430. {
  1431. displayEffect(50, attackedInfos[h].defender->position); //TODO: play reverse death animation
  1432. CCS->soundh->playSound(soundBase::RESURECT);
  1433. }
  1434. }
  1435. waitForAnims();
  1436. int targets = 0, killed = 0, damage = 0;
  1437. for(size_t h = 0; h < attackedInfos.size(); ++h)
  1438. {
  1439. ++targets;
  1440. killed += attackedInfos[h].killed;
  1441. damage += attackedInfos[h].dmg;
  1442. }
  1443. if (targets > 1)
  1444. printConsoleAttacked(attackedInfos.front().defender, damage, killed, attackedInfos.front().attacker, true); //creatures perish
  1445. else
  1446. printConsoleAttacked(attackedInfos.front().defender, damage, killed, attackedInfos.front().attacker, false);
  1447. for(size_t h = 0; h < attackedInfos.size(); ++h)
  1448. {
  1449. if (attackedInfos[h].rebirth)
  1450. creAnims[attackedInfos[h].defender->ID]->setType(CCreatureAnim::HOLDING);
  1451. }
  1452. }
  1453. void CBattleInterface::stackAttacking( const CStack * attacker, SBattleHex dest, const CStack * attacked, bool shooting )
  1454. {
  1455. if (shooting)
  1456. {
  1457. addNewAnim(new CShootingAnimation(this, attacker, dest, attacked));
  1458. }
  1459. else
  1460. {
  1461. addNewAnim(new CMeleeAttackAnimation(this, attacker, dest, attacked));
  1462. }
  1463. waitForAnims();
  1464. }
  1465. void CBattleInterface::newRoundFirst( int round )
  1466. {
  1467. //handle regeneration
  1468. std::vector<const CStack*> stacks = curInt->cb->battleGetStacks(); //gets only alive stacks
  1469. // BOOST_FOREACH(const CStack *s, stacks)
  1470. // {
  1471. // }
  1472. waitForAnims();
  1473. }
  1474. void CBattleInterface::newRound(int number)
  1475. {
  1476. console->addText(CGI->generaltexth->allTexts[412]);
  1477. //unlock spellbook
  1478. //bSpell->block(!curInt->cb->battleCanCastSpell());
  1479. //don't unlock spellbook - this should be done when we have axctive creature
  1480. }
  1481. void CBattleInterface::giveCommand(ui8 action, SBattleHex tile, ui32 stack, si32 additional)
  1482. {
  1483. if(!curInt->cb->battleGetStackByID(stack) && action != 1 && action != 4 && action != 5)
  1484. {
  1485. return;
  1486. }
  1487. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  1488. ba->side = defendingHeroInstance ? (curInt->playerID == defendingHeroInstance->tempOwner) : false;
  1489. ba->actionType = action;
  1490. ba->destinationTile = tile;
  1491. ba->stackNumber = stack;
  1492. ba->additionalInfo = additional;
  1493. //some basic validations
  1494. switch(action)
  1495. {
  1496. case BattleAction::WALK_AND_ATTACK:
  1497. assert(curInt->cb->battleGetStackByPos(additional)); //stack to attack must exist
  1498. case BattleAction::WALK:
  1499. case BattleAction::SHOOT:
  1500. case BattleAction::CATAPULT:
  1501. assert(tile < GameConstants::BFIELD_SIZE);
  1502. break;
  1503. }
  1504. if(!tacticsMode)
  1505. {
  1506. myTurn = false;
  1507. activeStack = NULL;
  1508. givenCommand->setn(ba);
  1509. }
  1510. else
  1511. {
  1512. curInt->cb->battleMakeTacticAction(ba);
  1513. vstd::clear_pointer(ba);
  1514. bTacticNextStack();
  1515. }
  1516. }
  1517. bool CBattleInterface::isTileAttackable(const SBattleHex & number) const
  1518. {
  1519. for(size_t b=0; b<occupyableHexes.size(); ++b)
  1520. {
  1521. if(SBattleHex::mutualPosition(occupyableHexes[b], number) != -1 || occupyableHexes[b] == number)
  1522. return true;
  1523. }
  1524. return false;
  1525. }
  1526. bool CBattleInterface::blockedByObstacle(SBattleHex hex) const
  1527. {
  1528. std::vector<CObstacleInstance> obstacles = curInt->cb->battleGetAllObstacles();
  1529. std::set<SBattleHex> coveredHexes;
  1530. for(size_t b = 0; b < obstacles.size(); ++b)
  1531. {
  1532. std::vector<SBattleHex> blocked = CGI->heroh->obstacles.find(obstacles[b].ID)->second.getBlocked(obstacles[b].pos);
  1533. for(size_t w = 0; w < blocked.size(); ++w)
  1534. coveredHexes.insert(blocked[w]);
  1535. }
  1536. return vstd::contains(coveredHexes, hex);
  1537. }
  1538. bool CBattleInterface::isCatapultAttackable(SBattleHex hex) const
  1539. {
  1540. if(!siegeH)
  1541. return false;
  1542. int wallUnder = curInt->cb->battleGetWallUnderHex(hex);
  1543. if(wallUnder == -1)
  1544. return false;
  1545. return curInt->cb->battleGetWallState(wallUnder) < 3;
  1546. }
  1547. const CGHeroInstance * CBattleInterface::getActiveHero()
  1548. {
  1549. const CStack * attacker = activeStack;
  1550. if (!attacker)
  1551. {
  1552. return NULL;
  1553. }
  1554. if (attacker->attackerOwned)
  1555. {
  1556. return attackingHeroInstance;
  1557. }
  1558. return defendingHeroInstance;
  1559. }
  1560. void CBattleInterface::hexLclicked(int whichOne)
  1561. {
  1562. const CStack * actSt = activeStack;
  1563. const CStack* dest = curInt->cb->battleGetStackByPos(whichOne, false); //creature at destination tile; -1 if there is no one
  1564. if(!actSt)
  1565. {
  1566. tlog3 << "Hex l-clicked when no active stack!\n";
  1567. return;
  1568. }
  1569. if( ((whichOne%GameConstants::BFIELD_WIDTH)!=0 && (whichOne%GameConstants::BFIELD_WIDTH)!=(GameConstants::BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  1570. || (actSt->hasBonusOfType(Bonus::CATAPULT) && !spellDestSelectMode || dest ) //enemy's first aid tent can stand there and we want to shoot it
  1571. )
  1572. {
  1573. if(!myTurn)
  1574. return; //we are not permit to do anything
  1575. if(spellDestSelectMode) //TODO: choose target for area creature spell
  1576. {
  1577. //checking destination
  1578. bool allowCasting = true;
  1579. bool onlyAlive = vstd::contains(CGI->spellh->risingSpells, spellToCast->additionalInfo); //when casting resurrection or animate dead we should be allow to select dead stack
  1580. //TODO: more general handling of dead targets
  1581. switch(spellSelMode)
  1582. {
  1583. case FRIENDLY_CREATURE:
  1584. if(!curInt->cb->battleGetStackByPos(whichOne, onlyAlive) || curInt->playerID != dest->owner )
  1585. allowCasting = false;
  1586. break;
  1587. case HOSTILE_CREATURE:
  1588. if(!curInt->cb->battleGetStackByPos(whichOne, onlyAlive) || curInt->playerID == dest->owner )
  1589. allowCasting = false;
  1590. break;
  1591. case ANY_CREATURE:
  1592. if(!curInt->cb->battleGetStackByPos(whichOne, onlyAlive))
  1593. allowCasting = false;
  1594. break;
  1595. case OBSTACLE:
  1596. if(!blockedByObstacle(whichOne))
  1597. allowCasting = false;
  1598. case TELEPORT: //teleport
  1599. const CSpell *s = CGI->spellh->spells[spellToCast->additionalInfo];
  1600. ui8 skill = getActiveHero()->getSpellSchoolLevel(s); //skill level
  1601. if (!curInt->cb->battleCanTeleportTo(activeStack, whichOne, skill))
  1602. {
  1603. allowCasting = false;
  1604. }
  1605. break;
  1606. }
  1607. //destination checked
  1608. if(allowCasting)
  1609. {
  1610. spellToCast->destinationTile = whichOne;
  1611. curInt->cb->battleMakeAction(spellToCast);
  1612. endCastingSpell();
  1613. }
  1614. }
  1615. else //we don't aim for spell target area
  1616. {
  1617. bool walkableTile = false;
  1618. bool spellCast = false;
  1619. if (dest)
  1620. {
  1621. bool ourStack = actSt->owner == dest->owner;
  1622. //try to cast stack spell first
  1623. if (stackCanCastSpell && spellSelMode > STACK_SPELL_CANCELLED) //player did not decide to cancel this spell
  1624. {
  1625. if ((int)creatureSpellToCast > -1) //use randomized spell (Faerie Dragon), or only avaliable spell (Archangel)
  1626. {
  1627. const CSpell * spell = CGI->spellh->spells[creatureSpellToCast];
  1628. if (curInt->cb->battleCanCastThisSpell(spell, SBattleHex(whichOne)) == ESpellCastProblem::OK)
  1629. {
  1630. if ((spell->positiveness > -1 && ourStack) || (spell->positiveness < 1 && !ourStack))
  1631. {
  1632. giveCommand(BattleAction::MONSTER_SPELL, whichOne, actSt->ID, creatureSpellToCast);
  1633. spellCast = true;
  1634. }
  1635. }
  1636. }
  1637. else if (ourStack) //must have only random positive spell (genie)
  1638. {
  1639. if (dest != actSt) //can't cast on itself
  1640. {
  1641. int spellID = curInt->cb->battleGetRandomStackSpell(dest, CBattleInfoCallback::RANDOM_GENIE);
  1642. if (spellID > -1) //can cast any spell on target stack
  1643. {
  1644. giveCommand(BattleAction::MONSTER_SPELL, whichOne, actSt->ID, spellID); //use randomized spell
  1645. spellCast = true;
  1646. }
  1647. }
  1648. }
  1649. if (spellCast)
  1650. {
  1651. creatureSpellToCast = -1;
  1652. return; //no further action after cast
  1653. }
  1654. }
  1655. if (dest->alive())
  1656. {
  1657. if(dest->owner != actSt->owner && curInt->cb->battleCanShoot(activeStack, whichOne)) //shooting
  1658. {
  1659. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  1660. giveCommand (BattleAction::SHOOT, whichOne, activeStack->ID);
  1661. }
  1662. else if(!ourStack) //attacking
  1663. {
  1664. const CStack * actStack = activeStack;
  1665. int attackFromHex = -1; //hex from which we will attack chosen stack
  1666. switch(CCS->curh->number)
  1667. {
  1668. case 12: //from bottom right
  1669. {
  1670. bool doubleWide = actStack->doubleWide();
  1671. int destHex = whichOne + ( (whichOne/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH+1 ) +
  1672. (actStack->attackerOwned && doubleWide ? 1 : 0);
  1673. if(vstd::contains(occupyableHexes, destHex))
  1674. attackFromHex = destHex;
  1675. else if(actStack->attackerOwned) //if we are attacker
  1676. {
  1677. if(vstd::contains(occupyableHexes, destHex+1))
  1678. attackFromHex = destHex+1;
  1679. }
  1680. else //if we are defender
  1681. {
  1682. if(vstd::contains(occupyableHexes, destHex-1))
  1683. attackFromHex = destHex-1;
  1684. }
  1685. break;
  1686. }
  1687. case 7: //from bottom left
  1688. {
  1689. int destHex = whichOne + ( (whichOne/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH-1 : GameConstants::BFIELD_WIDTH );
  1690. if(vstd::contains(occupyableHexes, destHex))
  1691. attackFromHex = destHex;
  1692. else if(actStack->attackerOwned) //if we are attacker
  1693. {
  1694. if(vstd::contains(occupyableHexes, destHex+1))
  1695. attackFromHex = destHex+1;
  1696. }
  1697. else //if we are defender
  1698. {
  1699. if(vstd::contains(occupyableHexes, destHex-1))
  1700. attackFromHex = destHex-1;
  1701. }
  1702. break;
  1703. }
  1704. case 8: //from left
  1705. {
  1706. if(actStack->doubleWide() && !actStack->attackerOwned)
  1707. {
  1708. std::vector<SBattleHex> acc = curInt->cb->battleGetAvailableHexes(activeStack, false);
  1709. if(vstd::contains(acc, whichOne))
  1710. attackFromHex = whichOne - 1;
  1711. else
  1712. attackFromHex = whichOne - 2;
  1713. }
  1714. else
  1715. {
  1716. attackFromHex = whichOne - 1;
  1717. }
  1718. break;
  1719. }
  1720. case 9: //from top left
  1721. {
  1722. int destHex = whichOne - ( (whichOne/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH+1 : GameConstants::BFIELD_WIDTH );
  1723. if(vstd::contains(occupyableHexes, destHex))
  1724. attackFromHex = destHex;
  1725. else if(actStack->attackerOwned) //if we are attacker
  1726. {
  1727. if(vstd::contains(occupyableHexes, destHex+1))
  1728. attackFromHex = destHex+1;
  1729. }
  1730. else //if we are defender
  1731. {
  1732. if(vstd::contains(occupyableHexes, destHex-1))
  1733. attackFromHex = destHex-1;
  1734. }
  1735. break;
  1736. }
  1737. case 10: //from top right
  1738. {
  1739. bool doubleWide = actStack->doubleWide();
  1740. int destHex = whichOne - ( (whichOne/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH-1 ) +
  1741. (actStack->attackerOwned && doubleWide ? 1 : 0);
  1742. if(vstd::contains(occupyableHexes, destHex))
  1743. attackFromHex = destHex;
  1744. else if(actStack->attackerOwned) //if we are attacker
  1745. {
  1746. if(vstd::contains(occupyableHexes, destHex+1))
  1747. attackFromHex = destHex+1;
  1748. }
  1749. else //if we are defender
  1750. {
  1751. if(vstd::contains(occupyableHexes, destHex-1))
  1752. attackFromHex = destHex-1;
  1753. }
  1754. break;
  1755. }
  1756. case 11: //from right
  1757. {
  1758. if(actStack->doubleWide() && actStack->attackerOwned)
  1759. {
  1760. std::vector<SBattleHex> acc = curInt->cb->battleGetAvailableHexes(activeStack, false);
  1761. if(vstd::contains(acc, whichOne))
  1762. attackFromHex = whichOne + 1;
  1763. else
  1764. attackFromHex = whichOne + 2;
  1765. }
  1766. else
  1767. {
  1768. attackFromHex = whichOne + 1;
  1769. }
  1770. break;
  1771. }
  1772. case 13: //from bottom
  1773. {
  1774. int destHex = whichOne + ( (whichOne/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH+1 );
  1775. if(vstd::contains(occupyableHexes, destHex))
  1776. attackFromHex = destHex;
  1777. else if(attackingHeroInstance->tempOwner == curInt->cb->getMyColor()) //if we are attacker
  1778. {
  1779. if(vstd::contains(occupyableHexes, destHex+1))
  1780. attackFromHex = destHex+1;
  1781. }
  1782. else //if we are defender
  1783. {
  1784. if(vstd::contains(occupyableHexes, destHex-1))
  1785. attackFromHex = destHex-1;
  1786. }
  1787. break;
  1788. }
  1789. case 14: //from top
  1790. {
  1791. int destHex = whichOne - ( (whichOne/GameConstants::BFIELD_WIDTH)%2 ? GameConstants::BFIELD_WIDTH : GameConstants::BFIELD_WIDTH-1 );
  1792. if(vstd::contains(occupyableHexes, destHex))
  1793. attackFromHex = destHex;
  1794. else if(attackingHeroInstance->tempOwner == curInt->cb->getMyColor()) //if we are attacker
  1795. {
  1796. if(vstd::contains(occupyableHexes, destHex+1))
  1797. attackFromHex = destHex+1;
  1798. }
  1799. else //if we are defender
  1800. {
  1801. if(vstd::contains(occupyableHexes, destHex-1))
  1802. attackFromHex = destHex-1;
  1803. }
  1804. break;
  1805. }
  1806. }
  1807. if(attackFromHex >= 0) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  1808. {
  1809. giveCommand(BattleAction::WALK_AND_ATTACK, attackFromHex, activeStack->ID, whichOne);
  1810. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  1811. }
  1812. }
  1813. else if (actSt->hasBonusOfType(Bonus::HEALER) && actSt->owner == dest->owner) //friendly creature we can heal
  1814. { //TODO: spellDestSelectMode > -2 if we don't want to heal but perform some other (?) action
  1815. giveCommand(BattleAction::STACK_HEAL, whichOne, activeStack->ID); //command healing
  1816. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  1817. }
  1818. } //stack is not alive
  1819. else if (actSt->hasBonusOfType(Bonus::DAEMON_SUMMONING) && actSt->casts &&
  1820. actSt->owner == dest->owner && spellSelMode > -2)//friendly body we can (and want) rise
  1821. {
  1822. giveCommand(BattleAction::DAEMON_SUMMONING, whichOne, activeStack->ID);
  1823. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  1824. }
  1825. else //not a subject of resurrection
  1826. walkableTile = true;
  1827. }
  1828. else
  1829. {
  1830. walkableTile = true;
  1831. }
  1832. if (walkableTile) // we can try to move to this tile
  1833. {
  1834. if(std::find(occupyableHexes.begin(), occupyableHexes.end(), whichOne) != occupyableHexes.end())// and it's in our range
  1835. {
  1836. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  1837. if(activeStack->doubleWide())
  1838. {
  1839. std::vector<SBattleHex> acc = curInt->cb->battleGetAvailableHexes(activeStack, false);
  1840. int shiftedDest = whichOne + (activeStack->attackerOwned ? 1 : -1);
  1841. if(vstd::contains(acc, whichOne))
  1842. giveCommand (BattleAction::WALK ,whichOne, activeStack->ID);
  1843. else if(vstd::contains(acc, shiftedDest))
  1844. giveCommand (BattleAction::WALK, shiftedDest, activeStack->ID);
  1845. }
  1846. else
  1847. {
  1848. giveCommand(BattleAction::WALK, whichOne, activeStack->ID);
  1849. }
  1850. }
  1851. else if(actSt->hasBonusOfType(Bonus::CATAPULT) && isCatapultAttackable(whichOne)) //attacking (catapult)
  1852. {
  1853. giveCommand(BattleAction::CATAPULT, whichOne, activeStack->ID);
  1854. }
  1855. }
  1856. }
  1857. }
  1858. }
  1859. void CBattleInterface::stackIsCatapulting(const CatapultAttack & ca)
  1860. {
  1861. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = ca.attackedParts.begin(); it != ca.attackedParts.end(); ++it)
  1862. {
  1863. const CStack * stack = curInt->cb->battleGetStackByID(ca.attacker);
  1864. addNewAnim(new CShootingAnimation(this, stack, it->first.second, NULL, true, it->second));
  1865. SDL_FreeSurface(siegeH->walls[it->first.first + 2]);
  1866. siegeH->walls[it->first.first + 2] = BitmapHandler::loadBitmap(
  1867. siegeH->getSiegeName(it->first.first + 2, curInt->cb->battleGetWallState(it->first.first)) );
  1868. }
  1869. waitForAnims();
  1870. }
  1871. void CBattleInterface::battleFinished(const BattleResult& br)
  1872. {
  1873. bresult = &br;
  1874. LOCPLINT->pim->unlock();
  1875. animsAreDisplayed.waitUntil(false);
  1876. LOCPLINT->pim->lock();
  1877. displayBattleFinished();
  1878. }
  1879. void CBattleInterface::displayBattleFinished()
  1880. {
  1881. CCS->curh->changeGraphic(0,0);
  1882. SDL_Rect temp_rect = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  1883. resWindow = new CBattleResultWindow(*bresult, temp_rect, this);
  1884. GH.pushInt(resWindow);
  1885. }
  1886. void CBattleInterface::spellCast( const BattleSpellCast * sc )
  1887. {
  1888. const CSpell &spell = *CGI->spellh->spells[sc->id];
  1889. //spell opening battle is cast when no stack is active
  1890. if(sc->castedByHero && ( activeStack == NULL || sc->side == !activeStack->attackerOwned) )
  1891. bSpell->block(true);
  1892. std::vector< std::string > anims; //for magic arrow and ice bolt
  1893. if (vstd::contains(CCS->soundh->spellSounds, &spell))
  1894. CCS->soundh->playSound(CCS->soundh->spellSounds[&spell]);
  1895. switch(sc->id)
  1896. {
  1897. case Spells::MAGIC_ARROW:
  1898. {
  1899. //initialization of anims
  1900. 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");
  1901. }
  1902. case Spells::ICE_BOLT:
  1903. {
  1904. if(anims.size() == 0) //initialization of anims
  1905. {
  1906. 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");
  1907. }
  1908. } //end of ice bolt only part
  1909. { //common ice bolt and magic arrow part
  1910. //initial variables
  1911. std::string animToDisplay;
  1912. SPoint srccoord = (sc->side ? SPoint(770, 60) : SPoint(30, 60)) + pos;
  1913. SPoint destcoord = CClickableHex::getXYUnitAnim(sc->tile, !sc->side, curInt->cb->battleGetStackByPos(sc->tile), this); //position attacked by arrow
  1914. destcoord.x += 250; destcoord.y += 240;
  1915. //animation angle
  1916. double angle = atan2(static_cast<double>(destcoord.x - srccoord.x), static_cast<double>(destcoord.y - srccoord.y));
  1917. bool Vflip = false;
  1918. if (angle < 0)
  1919. {
  1920. Vflip = true;
  1921. angle = -angle;
  1922. }
  1923. //choosing animation by angle
  1924. if(angle > 1.50)
  1925. animToDisplay = anims[0];
  1926. else if(angle > 1.20)
  1927. animToDisplay = anims[1];
  1928. else if(angle > 0.90)
  1929. animToDisplay = anims[2];
  1930. else if(angle > 0.60)
  1931. animToDisplay = anims[3];
  1932. else
  1933. animToDisplay = anims[4];
  1934. //displaying animation
  1935. CDefEssential * animDef = CDefHandler::giveDefEss(animToDisplay);
  1936. int steps = sqrt(static_cast<double>((destcoord.x - srccoord.x)*(destcoord.x - srccoord.x) + (destcoord.y - srccoord.y) * (destcoord.y - srccoord.y))) / 40;
  1937. if(steps <= 0)
  1938. steps = 1;
  1939. int dx = (destcoord.x - srccoord.x - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.y - srccoord.y - animDef->ourImages[0].bitmap->h)/steps;
  1940. delete animDef;
  1941. addNewAnim(new CSpellEffectAnimation(this, animToDisplay, srccoord.x, srccoord.y, dx, dy, Vflip));
  1942. break; //for 15 and 16 cases
  1943. }
  1944. case Spells::LIGHTNING_BOLT:
  1945. case Spells::TITANS_LIGHTNING_BOLT:
  1946. case Spells::THUNDERBOLT:
  1947. displayEffect(1, sc->tile);
  1948. displayEffect(spell.mainEffectAnim, sc->tile);
  1949. break;
  1950. case Spells::DISPEL:
  1951. case Spells::CURE:
  1952. case Spells::RESURRECTION:
  1953. case Spells::ANIMATE_DEAD:
  1954. case Spells::DISPEL_HELPFUL_SPELLS:
  1955. for(std::set<ui32>::const_iterator it = sc->affectedCres.begin(); it != sc->affectedCres.end(); ++it)
  1956. {
  1957. displayEffect(spell.mainEffectAnim, curInt->cb->battleGetStackByID(*it, false)->position);
  1958. }
  1959. break;
  1960. case Spells::SUMMON_FIRE_ELEMENTAL:
  1961. case Spells::SUMMON_EARTH_ELEMENTAL:
  1962. case Spells::SUMMON_WATER_ELEMENTAL:
  1963. case Spells::SUMMON_AIR_ELEMENTAL:
  1964. addNewAnim(new CDummyAnimation(this, 2));
  1965. break;
  1966. } //switch(sc->id)
  1967. //support for resistance
  1968. for(size_t j = 0; j < sc->resisted.size(); ++j)
  1969. {
  1970. int tile = curInt->cb->battleGetStackByID(sc->resisted[j])->position;
  1971. displayEffect(78, tile);
  1972. }
  1973. //displaying message in console
  1974. bool customSpell = false;
  1975. bool plural = false; //add singular / plural form of creature text if this is true
  1976. int textID = 0;
  1977. if(sc->affectedCres.size() == 1)
  1978. {
  1979. std::string text = CGI->generaltexth->allTexts[195];
  1980. if(sc->castedByHero)
  1981. {
  1982. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetFightingHero(sc->side)->name);
  1983. boost::algorithm::replace_first(text, "%s", CGI->spellh->spells[sc->id]->name); //spell name
  1984. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin(), false)->getCreature()->namePl ); //target
  1985. }
  1986. else
  1987. {
  1988. switch(sc->id)
  1989. {
  1990. case Spells::STONE_GAZE:
  1991. customSpell = true;
  1992. plural = true;
  1993. textID = 558;
  1994. break;
  1995. case Spells::POISON:
  1996. customSpell = true;
  1997. plural = true;
  1998. textID = 561;
  1999. break;
  2000. case Spells::BIND:
  2001. customSpell = true;
  2002. text = CGI->generaltexth->allTexts[560];
  2003. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin(), false)->getCreature()->namePl );
  2004. break; //Roots and vines bind the %s to the ground!
  2005. case Spells::DISEASE:
  2006. customSpell = true;
  2007. plural = true;
  2008. textID = 553;
  2009. break;
  2010. case Spells::PARALYZE:
  2011. customSpell = true;
  2012. plural = true;
  2013. textID = 563;
  2014. break;
  2015. case Spells::AGE:
  2016. {
  2017. customSpell = true;
  2018. if (curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->count > 1)
  2019. {
  2020. text = CGI->generaltexth->allTexts[552];
  2021. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->type->namePl);
  2022. }
  2023. else
  2024. {
  2025. text = CGI->generaltexth->allTexts[551];
  2026. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->type->nameSing);
  2027. }
  2028. //The %s shrivel with age, and lose %d hit points."
  2029. TBonusListPtr bl = curInt->cb->battleGetStackByID(*sc->affectedCres.begin(), false)->getBonuses(Selector::type(Bonus::STACK_HEALTH));
  2030. bl->remove_if(Selector::source(Bonus::SPELL_EFFECT, 75));
  2031. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(bl->totalValue()/2));
  2032. }
  2033. break;
  2034. case Spells::THUNDERBOLT:
  2035. text = CGI->generaltexth->allTexts[367];
  2036. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->type->namePl);
  2037. console->addText(text);
  2038. text = CGI->generaltexth->allTexts[343].substr(1, CGI->generaltexth->allTexts[343].size() - 1); //Does %d points of damage.
  2039. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(sc->dmgToDisplay)); //no more text afterwards
  2040. console->addText(text);
  2041. customSpell = true;
  2042. text = ""; //yeah, it's a terrible mess
  2043. break;
  2044. case Spells::DISPEL_HELPFUL_SPELLS:
  2045. text = CGI->generaltexth->allTexts[555];
  2046. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->type->namePl);
  2047. customSpell = true;
  2048. break;
  2049. case Spells::DEATH_STARE:
  2050. customSpell = true;
  2051. if (sc->dmgToDisplay)
  2052. {
  2053. if (sc->dmgToDisplay > 1)
  2054. {
  2055. text = CGI->generaltexth->allTexts[119]; //%d %s die under the terrible gaze of the %s.
  2056. boost::algorithm::replace_first(text, "%d", boost::lexical_cast<std::string>(sc->dmgToDisplay));
  2057. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin(), false)->getCreature()->namePl );
  2058. }
  2059. else
  2060. {
  2061. text = CGI->generaltexth->allTexts[118]; //One %s dies under the terrible gaze of the %s.
  2062. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->type->nameSing);
  2063. }
  2064. boost::algorithm::replace_first(text, "%s", CGI->creh->creatures[sc->attackerType]->namePl); //casting stack
  2065. }
  2066. else
  2067. text = "";
  2068. break;
  2069. default:
  2070. text = CGI->generaltexth->allTexts[565]; //The %s casts %s
  2071. boost::algorithm::replace_first(text, "%s", CGI->creh->creatures[sc->attackerType]->namePl); //casting stack
  2072. }
  2073. if (plural)
  2074. {
  2075. if (curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->count > 1)
  2076. {
  2077. text = CGI->generaltexth->allTexts[textID + 1];
  2078. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->getName());
  2079. }
  2080. else
  2081. {
  2082. text = CGI->generaltexth->allTexts[textID];
  2083. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin())->getName());
  2084. }
  2085. }
  2086. }
  2087. if (!customSpell && !sc->dmgToDisplay)
  2088. boost::algorithm::replace_first(text, "%s", CGI->spellh->spells[sc->id]->name); //simple spell name
  2089. if (text.size())
  2090. console->addText(text);
  2091. }
  2092. else
  2093. {
  2094. std::string text = CGI->generaltexth->allTexts[196];
  2095. if(sc->castedByHero)
  2096. {
  2097. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetFightingHero(sc->side)->name);
  2098. }
  2099. else if(sc->attackerType < CGI->creh->creatures.size())
  2100. {
  2101. boost::algorithm::replace_first(text, "%s", CGI->creh->creatures[sc->attackerType]->namePl); //creature caster
  2102. }
  2103. else
  2104. {
  2105. //TODO artifacts that cast spell; scripts some day
  2106. boost::algorithm::replace_first(text, "Something", CGI->creh->creatures[sc->attackerType]->namePl); //creature caster
  2107. }
  2108. boost::algorithm::replace_first(text, "%s", CGI->spellh->spells[sc->id]->name);
  2109. console->addText(text);
  2110. }
  2111. if(sc->dmgToDisplay && !customSpell)
  2112. {
  2113. std::string dmgInfo = CGI->generaltexth->allTexts[376];
  2114. boost::algorithm::replace_first(dmgInfo, "%s", CGI->spellh->spells[sc->id]->name); //simple spell name
  2115. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(sc->dmgToDisplay));
  2116. console->addText(dmgInfo); //todo: casualties (?)
  2117. }
  2118. waitForAnims();
  2119. //mana absorption
  2120. if (sc->manaGained)
  2121. {
  2122. SPoint leftHero = SPoint(15, 30) + pos;
  2123. SPoint rightHero = SPoint(755, 30) + pos;
  2124. addNewAnim(new CSpellEffectAnimation(this, sc->side ? "SP07_A.DEF" : "SP07_B.DEF", leftHero.x, leftHero.y, 0, 0, false));
  2125. addNewAnim(new CSpellEffectAnimation(this, sc->side ? "SP07_B.DEF" : "SP07_A.DEF", rightHero.x, rightHero.y, 0, 0, false));
  2126. }
  2127. }
  2128. void CBattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
  2129. {
  2130. int effID = sse.effect.back().sid;
  2131. if(effID != -1) //can be -1 for defensive stance effect
  2132. {
  2133. for(std::vector<ui32>::const_iterator ci = sse.stacks.begin(); ci!=sse.stacks.end(); ++ci)
  2134. {
  2135. displayEffect(CGI->spellh->spells[effID]->mainEffectAnim, curInt->cb->battleGetStackByID(*ci)->position);
  2136. }
  2137. }
  2138. else if (sse.stacks.size() == 1 && sse.effect.size() == 2)
  2139. {
  2140. const Bonus & bns = sse.effect.front();
  2141. if (bns.source == Bonus::OTHER && bns.type == Bonus::PRIMARY_SKILL)
  2142. {
  2143. //defensive stance
  2144. const CStack * stack = LOCPLINT->cb->battleGetStackByID(*sse.stacks.begin());
  2145. int txtid = 120;
  2146. if(stack->count != 1)
  2147. txtid++; //move to plural text
  2148. char txt[4000];
  2149. BonusList defenseBonuses = *(stack->getBonuses(Selector::typeSubtype(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE)));
  2150. defenseBonuses.remove_if(Selector::durationType(Bonus::STACK_GETS_TURN)); //remove bonuses gained from defensive stance
  2151. int val = stack->Defense() - defenseBonuses.totalValue();
  2152. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str(), val);
  2153. console->addText(txt);
  2154. }
  2155. }
  2156. if (activeStack != NULL) //it can be -1 when a creature casts effect
  2157. {
  2158. redrawBackgroundWithHexes(activeStack);
  2159. }
  2160. }
  2161. void CBattleInterface::castThisSpell(int spellID)
  2162. {
  2163. BattleAction * ba = new BattleAction;
  2164. ba->actionType = BattleAction::HERO_SPELL;
  2165. ba->additionalInfo = spellID; //spell number
  2166. ba->destinationTile = -1;
  2167. ba->stackNumber = (attackingHeroInstance->tempOwner == curInt->playerID) ? -1 : -2;
  2168. ba->side = defendingHeroInstance ? (curInt->playerID == defendingHeroInstance->tempOwner) : false;
  2169. spellToCast = ba;
  2170. spellDestSelectMode = true;
  2171. //choosing possible tragets
  2172. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == curInt->playerID) ? attackingHeroInstance : defendingHeroInstance;
  2173. const CSpell & spell = *CGI->spellh->spells[spellID];
  2174. spellSelMode = ANY_LOCATION;
  2175. if(spell.getTargetType() == CSpell::CREATURE)
  2176. {
  2177. switch(spell.positiveness)
  2178. {
  2179. case -1 :
  2180. spellSelMode = HOSTILE_CREATURE;
  2181. break;
  2182. case 0:
  2183. spellSelMode = ANY_CREATURE;
  2184. break;
  2185. case 1:
  2186. spellSelMode = FRIENDLY_CREATURE;
  2187. break;
  2188. }
  2189. }
  2190. if(spell.getTargetType() == CSpell::CREATURE_EXPERT_MASSIVE)
  2191. {
  2192. if(castingHero && castingHero->getSpellSchoolLevel(&spell) < 3)
  2193. {
  2194. switch(spell.positiveness)
  2195. {
  2196. case -1 :
  2197. spellSelMode = HOSTILE_CREATURE;
  2198. break;
  2199. case 0:
  2200. spellSelMode = ANY_CREATURE;
  2201. break;
  2202. case 1:
  2203. spellSelMode = FRIENDLY_CREATURE;
  2204. break;
  2205. }
  2206. }
  2207. else
  2208. {
  2209. spellSelMode = NO_LOCATION;
  2210. }
  2211. }
  2212. if(spell.getTargetType() == CSpell::OBSTACLE)
  2213. {
  2214. spellSelMode = OBSTACLE;
  2215. }
  2216. if(spell.range[ castingHero->getSpellSchoolLevel(&spell) ] == "X") //spell has no range
  2217. {
  2218. spellSelMode = NO_LOCATION;
  2219. }
  2220. if(spell.id == 63) //teleport
  2221. {
  2222. spellSelMode = TELEPORT;
  2223. }
  2224. if(spell.range[ castingHero->getSpellSchoolLevel(&spell) ].size() > 1) //spell has many-hex range
  2225. {
  2226. spellSelMode = ANY_LOCATION;
  2227. }
  2228. if(spellSelMode == NO_LOCATION) //user does not have to select location
  2229. {
  2230. spellToCast->destinationTile = -1;
  2231. curInt->cb->battleMakeAction(spellToCast);
  2232. endCastingSpell();
  2233. }
  2234. else
  2235. {
  2236. CCS->curh->changeGraphic(3, 0);
  2237. }
  2238. }
  2239. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  2240. {
  2241. addNewAnim(new CSpellEffectAnimation(this, effect, destTile));
  2242. }
  2243. void CBattleInterface::battleTriggerEffect(const BattleTriggerEffect & bte)
  2244. {
  2245. const CStack * stack = curInt->cb->battleGetStackByID(bte.stackID);
  2246. //don't show animation when no HP is regenerated
  2247. switch (bte.effect)
  2248. {
  2249. case Bonus::HP_REGENERATION:
  2250. if( stack->hasBonusOfType(Bonus::HP_REGENERATION) || stack->hasBonusOfType(Bonus::FULL_HP_REGENERATION))
  2251. {
  2252. displayEffect(74, stack->position);
  2253. CCS->soundh->playSound(soundBase::REGENER);
  2254. }
  2255. break;
  2256. case Bonus::MANA_DRAIN:
  2257. displayEffect(77, stack->position);
  2258. CCS->soundh->playSound(soundBase::MANADRAI);
  2259. break;
  2260. case Bonus::POISON:
  2261. displayEffect(67, stack->position);
  2262. CCS->soundh->playSound(soundBase::POISON);
  2263. break;
  2264. case Bonus::FEAR:
  2265. displayEffect(15, stack->position);
  2266. CCS->soundh->playSound(soundBase::FEAR);
  2267. break;
  2268. default:
  2269. return;
  2270. }
  2271. //waitForAnims(); //fixme: freezes game :?
  2272. }
  2273. void CBattleInterface::setAnimSpeed(int set)
  2274. {
  2275. curInt->sysOpts.animSpeed = set;
  2276. curInt->sysOpts.settingsChanged();
  2277. }
  2278. int CBattleInterface::getAnimSpeed() const
  2279. {
  2280. return curInt->sysOpts.animSpeed;
  2281. }
  2282. void CBattleInterface::activateStack()
  2283. {
  2284. activeStack = stackToActivate;
  2285. stackToActivate = NULL;
  2286. const CStack *s = activeStack;
  2287. myTurn = true;
  2288. if(attackerInt && defenderInt) //hotseat -> need to pick which interface "takes over" as active
  2289. curInt = attackerInt->playerID == s->owner ? attackerInt : defenderInt;
  2290. queue->update();
  2291. redrawBackgroundWithHexes(activeStack);
  2292. bWait->block(vstd::contains(s->state, EBattleStackState::WAITING)); //block waiting button if stack has been already waiting
  2293. //block cast spell button if hero doesn't have a spellbook
  2294. bSpell->block(!curInt->cb->battleCanCastSpell());
  2295. bSurrender->block((curInt == attackerInt ? defendingHeroInstance : attackingHeroInstance) == NULL);
  2296. bFlee->block(!curInt->cb->battleCanFlee());
  2297. bSurrender->block(curInt->cb->battleGetSurrenderCost() < 0);
  2298. //set casting flag to true if creature can use it to not check it every time
  2299. if (s->casts && s->hasBonus(Selector::type(Bonus::SPELLCASTER) || Selector::type(Bonus::RANDOM_SPELLCASTER)))
  2300. {
  2301. stackCanCastSpell = true;
  2302. creatureSpellToCast = curInt->cb->battleGetRandomStackSpell(s, CBattleInfoCallback::RANDOM_AIMED); //faerie dragon can cast only one spell until their next move
  2303. }
  2304. else
  2305. {
  2306. stackCanCastSpell = false;
  2307. creatureSpellToCast = -1;
  2308. }
  2309. GH.fakeMouseMove();
  2310. if(!pendingAnims.size() && !active)
  2311. activate();
  2312. }
  2313. double CBattleInterface::getAnimSpeedMultiplier() const
  2314. {
  2315. switch(curInt->sysOpts.animSpeed)
  2316. {
  2317. case 1:
  2318. return 3.5;
  2319. case 2:
  2320. return 2.2;
  2321. case 4:
  2322. return 1.0;
  2323. default:
  2324. return 0.0;
  2325. }
  2326. }
  2327. void CBattleInterface::endCastingSpell()
  2328. {
  2329. assert(spellDestSelectMode);
  2330. delete spellToCast;
  2331. spellToCast = NULL;
  2332. spellDestSelectMode = false;
  2333. CCS->curh->changeGraphic(1, 6);
  2334. }
  2335. void CBattleInterface::showAliveStack(const CStack *stack, SDL_Surface * to)
  2336. {
  2337. int ID = stack->ID;
  2338. if(creAnims.find(ID) == creAnims.end()) //eg. for summoned but not yet handled stacks
  2339. return;
  2340. const CCreature *creature = stack->getCreature();
  2341. SDL_Rect unitRect = {creAnims[ID]->pos.x, creAnims[ID]->pos.y, creAnims[ID]->fullWidth, creAnims[ID]->fullHeight};
  2342. int animType = creAnims[ID]->getType();
  2343. int affectingSpeed = curInt->sysOpts.animSpeed;
  2344. if(animType == 1 || animType == 2) //standing stacks should not stand faster :)
  2345. affectingSpeed = 2;
  2346. bool incrementFrame = (animCount%(4/affectingSpeed)==0) && animType!=5 && animType!=20 && animType!=2;
  2347. if (creature->idNumber == 149)
  2348. {
  2349. // a turret creature has a limited height, so cut it at a certain position; turret creature has no standing anim
  2350. unitRect.h = graphics->wallPositions[siegeH->town->town->typeID][20].y;
  2351. }
  2352. else
  2353. {
  2354. // standing animation
  2355. if(animType == 2)
  2356. {
  2357. if(standingFrame.find(ID)!=standingFrame.end())
  2358. {
  2359. incrementFrame = (animCount%(8/affectingSpeed)==0);
  2360. if(incrementFrame)
  2361. {
  2362. ++standingFrame[ID];
  2363. if(standingFrame[ID] == creAnims[ID]->framesInGroup(CCreatureAnim::HOLDING))
  2364. {
  2365. standingFrame.erase(standingFrame.find(ID));
  2366. }
  2367. }
  2368. }
  2369. else
  2370. {
  2371. if((rand()%50) == 0)
  2372. {
  2373. standingFrame.insert(std::make_pair(ID, 0));
  2374. }
  2375. }
  2376. }
  2377. }
  2378. // As long as the projectile of the shooter-stack is flying incrementFrame should be false
  2379. bool shootingFinished = true;
  2380. for (std::list<SProjectileInfo>::iterator it = projectiles.begin(); it != projectiles.end(); ++it)
  2381. {
  2382. if (it->stackID == ID)
  2383. {
  2384. shootingFinished = false;
  2385. if (it->animStartDelay == 0)
  2386. incrementFrame = false;
  2387. }
  2388. }
  2389. // Increment always when moving, never if stack died
  2390. creAnims[ID]->nextFrame(to, unitRect.x, unitRect.y, creDir[ID], animCount, incrementFrame, activeStack && ID==activeStack->ID, ID==mouseHoveredStack, &unitRect);
  2391. //printing amount
  2392. if(stack->count > 0 //don't print if stack is not alive
  2393. && (!curInt->curAction
  2394. || (curInt->curAction->stackNumber != ID //don't print if stack is currently taking an action
  2395. && (curInt->curAction->actionType != BattleAction::WALK_AND_ATTACK || stack->position != curInt->curAction->additionalInfo) //nor if it's an object of attack
  2396. && (curInt->curAction->destinationTile != stack->position) //nor if it's on destination tile for current action
  2397. )
  2398. )
  2399. && !stack->hasBonusOfType(Bonus::SIEGE_WEAPON) //and not a war machine...
  2400. )
  2401. {
  2402. const SBattleHex nextPos = stack->position + (stack->attackerOwned ? 1 : -1);
  2403. const bool edge = stack->position % GameConstants::BFIELD_WIDTH == (stack->attackerOwned ? GameConstants::BFIELD_WIDTH - 2 : 1);
  2404. const bool moveInside = !edge && !stackCountOutsideHexes[nextPos];
  2405. int xAdd = (stack->attackerOwned ? 220 : 202) +
  2406. (stack->doubleWide() ? 44 : 0) * (stack->attackerOwned ? +1 : -1) +
  2407. (moveInside ? amountNormal->w + 10 : 0) * (stack->attackerOwned ? -1 : +1);
  2408. int yAdd = 260 + ((stack->attackerOwned || moveInside) ? 0 : -15);
  2409. //blitting amount background box
  2410. SDL_Surface *amountBG = NULL;
  2411. TBonusListPtr spellEffects = stack->getSpellBonuses();
  2412. if(!spellEffects->size())
  2413. {
  2414. amountBG = amountNormal;
  2415. }
  2416. else
  2417. {
  2418. int pos=0; //determining total positiveness of effects
  2419. std::vector<si32> spellIds = stack->activeSpells();
  2420. for(std::vector<si32>::const_iterator it = spellIds.begin(); it != spellIds.end(); it++)
  2421. {
  2422. pos += CGI->spellh->spells[ *it ]->positiveness;
  2423. }
  2424. if(pos > 0)
  2425. {
  2426. amountBG = amountPositive;
  2427. }
  2428. else if(pos < 0)
  2429. {
  2430. amountBG = amountNegative;
  2431. }
  2432. else
  2433. {
  2434. amountBG = amountEffNeutral;
  2435. }
  2436. }
  2437. SDL_Rect temp_rect = genRect(amountNormal->h, amountNormal->w, creAnims[ID]->pos.x + xAdd, creAnims[ID]->pos.y + yAdd);
  2438. SDL_BlitSurface(amountBG, NULL, to, &temp_rect);
  2439. //blitting amount
  2440. CSDL_Ext::printAtMiddle(
  2441. makeNumberShort(stack->count),
  2442. creAnims[ID]->pos.x + xAdd + 15,
  2443. creAnims[ID]->pos.y + yAdd + 5,
  2444. FONT_TINY,
  2445. zwykly,
  2446. to
  2447. );
  2448. }
  2449. }
  2450. void CBattleInterface::showPieceOfWall(SDL_Surface * to, int hex, const std::vector<const CStack*> & stacks)
  2451. {
  2452. if(!siegeH)
  2453. return;
  2454. using namespace boost::assign;
  2455. static const std::map<int, std::list<int> > hexToPart = map_list_of<int, std::list<int> >(12, list_of<int>(8)(1)(7))(45, list_of<int>(12)(6))
  2456. /*gate (78, list_of<int>(9))*/(101, list_of<int>(10))(118, list_of<int>(2))(165, list_of<int>(11))(186, list_of<int>(3));
  2457. std::map<int, std::list<int> >::const_iterator it = hexToPart.find(hex);
  2458. if(it != hexToPart.end())
  2459. {
  2460. BOOST_FOREACH(int wallNum, it->second)
  2461. {
  2462. siegeH->printPartOfWall(to, wallNum);
  2463. //print creature in turret
  2464. int posToSeek = -1;
  2465. switch(wallNum)
  2466. {
  2467. case 3: //bottom turret
  2468. posToSeek = -3;
  2469. break;
  2470. case 8: //upper turret
  2471. posToSeek = -4;
  2472. break;
  2473. case 2: //keep
  2474. posToSeek = -2;
  2475. break;
  2476. }
  2477. if(posToSeek != -1)
  2478. {
  2479. const CStack *turret = NULL;
  2480. BOOST_FOREACH(const CStack *s, stacks)
  2481. {
  2482. if(s->position == posToSeek)
  2483. {
  2484. turret = s;
  2485. break;
  2486. }
  2487. }
  2488. if(turret)
  2489. {
  2490. showAliveStack(turret, to);
  2491. //blitting creature cover
  2492. switch(posToSeek)
  2493. {
  2494. case -3: //bottom turret
  2495. siegeH->printPartOfWall(to, 16);
  2496. break;
  2497. case -4: //upper turret
  2498. siegeH->printPartOfWall(to, 17);
  2499. break;
  2500. case -2: //keep
  2501. siegeH->printPartOfWall(to, 15);
  2502. break;
  2503. }
  2504. }
  2505. }
  2506. }
  2507. }
  2508. // Damaged wall below gate have to be drawn earlier than a non-damaged wall below gate.
  2509. if ((hex == 112 && curInt->cb->battleGetWallState(3) == 3) || (hex == 147 && curInt->cb->battleGetWallState(3) != 3))
  2510. siegeH->printPartOfWall(to, 5);
  2511. // Damaged bottom wall have to be drawn earlier than a non-damaged bottom wall.
  2512. if ((hex == 165 && curInt->cb->battleGetWallState(4) == 3) || (hex == 185 && curInt->cb->battleGetWallState(4) != 3))
  2513. siegeH->printPartOfWall(to, 4);
  2514. }
  2515. void CBattleInterface::redrawBackgroundWithHexes(const CStack * activeStack)
  2516. {
  2517. attackableHexes.clear();
  2518. if (activeStack)
  2519. occupyableHexes = curInt->cb->battleGetAvailableHexes(activeStack, true, &attackableHexes);
  2520. curInt->cb->battleGetStackCountOutsideHexes(stackCountOutsideHexes);
  2521. //preparating background graphic with hexes and shaded hexes
  2522. blitAt(background, 0, 0, backgroundWithHexes);
  2523. if(curInt->sysOpts.printCellBorders)
  2524. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  2525. if(curInt->sysOpts.printStackRange)
  2526. {
  2527. std::vector<SBattleHex> hexesToShade = occupyableHexes;
  2528. hexesToShade.insert(hexesToShade.end(), attackableHexes.begin(), attackableHexes.end());
  2529. BOOST_FOREACH(SBattleHex hex, hexesToShade)
  2530. {
  2531. int i = hex.getY(); //row
  2532. int j = hex.getX()-1; //column
  2533. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  2534. int y = 86 + 42 * i;
  2535. SDL_Rect temp_rect = genRect(cellShade->h, cellShade->w, x, y);
  2536. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &temp_rect);
  2537. }
  2538. }
  2539. }
  2540. void CBattleInterface::printConsoleAttacked( const CStack * defender, int dmg, int killed, const CStack * attacker, bool multiple )
  2541. {
  2542. char tabh[200];
  2543. int end = 0;
  2544. if (attacker) //ignore if stacks were killed by spell
  2545. {
  2546. end = sprintf(tabh, CGI->generaltexth->allTexts[attacker->count > 1 ? 377 : 376].c_str(),
  2547. (attacker->count > 1 ? attacker->getCreature()->namePl.c_str() : attacker->getCreature()->nameSing.c_str()), dmg);
  2548. }
  2549. if(killed > 0)
  2550. {
  2551. if(killed > 1)
  2552. {
  2553. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed,
  2554. multiple ? CGI->generaltexth->allTexts[43].c_str() : defender->getCreature()->namePl.c_str()); // creatures perish
  2555. }
  2556. else //killed == 1
  2557. {
  2558. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(),
  2559. multiple ? CGI->generaltexth->allTexts[42].c_str() : defender->getCreature()->nameSing.c_str()); // creature perishes
  2560. }
  2561. }
  2562. console->addText(std::string(tabh));
  2563. }
  2564. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  2565. {
  2566. if(to == NULL)
  2567. to = screen;
  2568. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  2569. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  2570. {
  2571. // Creature have to be in a shooting anim and the anim start delay must be over.
  2572. // Otherwise abort to start moving the projectile.
  2573. if (it->animStartDelay > 0)
  2574. {
  2575. if(it->animStartDelay == creAnims[it->stackID]->getAnimationFrame() + 1
  2576. && creAnims[it->stackID]->getType() >= 14 && creAnims[it->stackID]->getType() <= 16)
  2577. it->animStartDelay = 0;
  2578. else
  2579. continue;
  2580. }
  2581. SDL_Rect dst;
  2582. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  2583. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  2584. dst.x = it->x;
  2585. dst.y = it->y;
  2586. // The equation below calculates the center pos of the canon, but we need the top left pos
  2587. // of it for drawing
  2588. if (it->catapultInfo)
  2589. {
  2590. dst.x -= 17.;
  2591. dst.y -= 10.;
  2592. }
  2593. if(it->reverse)
  2594. {
  2595. SDL_Surface * rev = CSDL_Ext::rotate01(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap);
  2596. CSDL_Ext::blit8bppAlphaTo24bpp(rev, NULL, to, &dst);
  2597. SDL_FreeSurface(rev);
  2598. }
  2599. else
  2600. {
  2601. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  2602. }
  2603. // Update projectile
  2604. ++it->step;
  2605. if(it->step == it->lastStep)
  2606. {
  2607. toBeDeleted.insert(toBeDeleted.end(), it);
  2608. }
  2609. else
  2610. {
  2611. if (it->catapultInfo)
  2612. {
  2613. // Parabolic shot of the trajectory, as follows: f(x) = ax^2 + bx + c
  2614. it->x += it->dx;
  2615. it->y = it->catapultInfo->calculateY(it->x - this->pos.x) + this->pos.y;
  2616. }
  2617. else
  2618. {
  2619. // Normal projectile, just add the calculated "deltas" to the x and y positions.
  2620. it->x += it->dx;
  2621. it->y += it->dy;
  2622. }
  2623. if(it->spin)
  2624. {
  2625. ++(it->frameNum);
  2626. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  2627. }
  2628. }
  2629. }
  2630. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  2631. {
  2632. projectiles.erase(*it);
  2633. }
  2634. }
  2635. void CBattleInterface::endAction(const BattleAction* action)
  2636. {
  2637. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  2638. // {
  2639. // activate();
  2640. // }
  2641. if(action->actionType == BattleAction::HERO_SPELL)
  2642. {
  2643. if(action->side)
  2644. defendingHero->setPhase(0);
  2645. else
  2646. attackingHero->setPhase(0);
  2647. }
  2648. if(action->actionType == BattleAction::WALK && creAnims[action->stackNumber]->getType() != 2) //walk or walk & attack
  2649. {
  2650. const CStack * stack = curInt->cb->battleGetStackByID(action->stackNumber);
  2651. pendingAnims.push_back(std::make_pair(new CMovementEndAnimation(this, stack, action->destinationTile), false));
  2652. }
  2653. if(action->actionType == BattleAction::CATAPULT) //catapult
  2654. {
  2655. }
  2656. //check if we should reverse stacks
  2657. //for some strange reason, it's not enough
  2658. // std::set<const CStack *> stacks;
  2659. // stacks.insert(LOCPLINT->cb->battleGetStackByID(action->stackNumber));
  2660. // stacks.insert(LOCPLINT->cb->battleGetStackByPos(action->destinationTile));
  2661. TStacks stacks = curInt->cb->battleGetStacks(CBattleCallback::MINE_AND_ENEMY);
  2662. BOOST_FOREACH(const CStack *s, stacks)
  2663. {
  2664. if(s && creDir[s->ID] != bool(s->attackerOwned) && s->alive())
  2665. {
  2666. addNewAnim(new CReverseAnimation(this, s, s->position, false));
  2667. }
  2668. }
  2669. queue->update();
  2670. if(tacticsMode) //we have activated next stack after sending request that has been just realized -> blockmap due to movement has changed
  2671. redrawBackgroundWithHexes(activeStack);
  2672. }
  2673. void CBattleInterface::hideQueue()
  2674. {
  2675. curInt->sysOpts.showQueue = false;
  2676. queue->deactivate();
  2677. if(!queue->embedded)
  2678. {
  2679. moveBy(SPoint(0, -queue->pos.h / 2));
  2680. GH.totalRedraw();
  2681. }
  2682. }
  2683. void CBattleInterface::showQueue()
  2684. {
  2685. curInt->sysOpts.showQueue = true;
  2686. queue->activate();
  2687. if(!queue->embedded)
  2688. {
  2689. moveBy(SPoint(0, +queue->pos.h / 2));
  2690. GH.totalRedraw();
  2691. }
  2692. }
  2693. void CBattleInterface::startAction(const BattleAction* action)
  2694. {
  2695. if(action->actionType == BattleAction::END_TACTIC_PHASE)
  2696. {
  2697. SDL_FreeSurface(menu);
  2698. menu = BitmapHandler::loadBitmap("CBAR.bmp");
  2699. graphics->blueToPlayersAdv(menu, curInt->playerID);
  2700. bDefence->block(false);
  2701. bWait->block(false);
  2702. if(active)
  2703. {
  2704. if(btactEnd && btactNext) //if the other side had tactics, there are no buttons
  2705. {
  2706. btactEnd->deactivate();
  2707. btactNext->deactivate();
  2708. bConsoleDown->activate();
  2709. bConsoleUp->activate();
  2710. }
  2711. }
  2712. redraw();
  2713. return;
  2714. }
  2715. const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
  2716. if(stack)
  2717. {
  2718. queue->update();
  2719. }
  2720. else
  2721. {
  2722. assert(action->actionType == BattleAction::HERO_SPELL); //only cast spell is valid action without acting stack number
  2723. }
  2724. if(action->actionType == BattleAction::WALK
  2725. || (action->actionType == BattleAction::WALK_AND_ATTACK && action->destinationTile != stack->position))
  2726. {
  2727. moveStarted = true;
  2728. if(creAnims[action->stackNumber]->framesInGroup(CCreatureAnim::MOVE_START))
  2729. {
  2730. const CStack * stack = curInt->cb->battleGetStackByID(action->stackNumber);
  2731. pendingAnims.push_back(std::make_pair(new CMovementStartAnimation(this, stack), false));
  2732. }
  2733. }
  2734. if(active)
  2735. deactivate();
  2736. char txt[400];
  2737. if(action->actionType == BattleAction::HERO_SPELL) //when hero casts spell
  2738. {
  2739. if(action->side)
  2740. defendingHero->setPhase(4);
  2741. else
  2742. attackingHero->setPhase(4);
  2743. return;
  2744. }
  2745. if(!stack)
  2746. {
  2747. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  2748. return;
  2749. }
  2750. int txtid = 0;
  2751. switch(action->actionType)
  2752. {
  2753. case BattleAction::WAIT:
  2754. txtid = 136;
  2755. break;
  2756. case BattleAction::BAD_MORALE:
  2757. txtid = -34; //negative -> no separate singular/plural form
  2758. displayEffect(30,stack->position);
  2759. break;
  2760. }
  2761. if(txtid > 0 && stack->count != 1)
  2762. txtid++; //move to plural text
  2763. else if(txtid < 0)
  2764. txtid = -txtid;
  2765. if(txtid)
  2766. {
  2767. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str(), 0);
  2768. console->addText(txt);
  2769. }
  2770. //displaying special abilities
  2771. switch (action->actionType)
  2772. {
  2773. case BattleAction::STACK_HEAL:
  2774. displayEffect(74, action->destinationTile);
  2775. CCS->soundh->playSound(soundBase::REGENER);
  2776. break;
  2777. }
  2778. }
  2779. void CBattleInterface::waitForAnims()
  2780. {
  2781. LOCPLINT->pim->unlock();
  2782. animsAreDisplayed.waitWhileTrue();
  2783. LOCPLINT->pim->lock();
  2784. }
  2785. void CBattleInterface::bEndTacticPhase()
  2786. {
  2787. btactEnd->block(true);
  2788. tacticsMode = false;
  2789. }
  2790. static bool immobile(const CStack *s)
  2791. {
  2792. return !s->Speed(0, true); //should bound stacks be immobile?
  2793. }
  2794. void CBattleInterface::bTacticNextStack()
  2795. {
  2796. TStacks stacksOfMine = tacticianInterface->cb->battleGetStacks(CBattleCallback::ONLY_MINE);
  2797. stacksOfMine.erase(std::remove_if(stacksOfMine.begin(), stacksOfMine.end(), &immobile), stacksOfMine.end());
  2798. TStacks::iterator it = vstd::find(stacksOfMine, activeStack);
  2799. if(it != stacksOfMine.end() && ++it != stacksOfMine.end())
  2800. stackActivated(*it);
  2801. else
  2802. stackActivated(stacksOfMine.front());
  2803. }
  2804. std::string CBattleInterface::SiegeHelper::townTypeInfixes[GameConstants::F_NUMBER] = {"CS", "RM", "TW", "IN", "NC", "DN", "ST", "FR", "EL"};
  2805. CBattleInterface::SiegeHelper::SiegeHelper(const CGTownInstance *siegeTown, const CBattleInterface * _owner)
  2806. : owner(_owner), town(siegeTown)
  2807. {
  2808. for(int g = 0; g < ARRAY_COUNT(walls); ++g)
  2809. {
  2810. walls[g] = BitmapHandler::loadBitmap( getSiegeName(g) );
  2811. }
  2812. }
  2813. CBattleInterface::SiegeHelper::~SiegeHelper()
  2814. {
  2815. for(int g = 0; g < ARRAY_COUNT(walls); ++g)
  2816. {
  2817. SDL_FreeSurface(walls[g]);
  2818. }
  2819. }
  2820. std::string CBattleInterface::SiegeHelper::getSiegeName(ui16 what, ui16 additInfo) const
  2821. {
  2822. if(what == 2 || what == 3 || what == 8)
  2823. {
  2824. if(additInfo == 3) additInfo = 2;
  2825. }
  2826. char buf[100];
  2827. SDL_itoa(additInfo, buf, 10);
  2828. std::string addit(buf);
  2829. switch(what)
  2830. {
  2831. case 0: //background
  2832. return "SG" + townTypeInfixes[town->town->typeID] + "BACK.BMP";
  2833. case 1: //background wall
  2834. {
  2835. switch(town->town->typeID)
  2836. {
  2837. case 5: case 4: case 1: case 6:
  2838. return "SG" + townTypeInfixes[town->town->typeID] + "TPW1.BMP";
  2839. case 0: case 2: case 3: case 7: case 8:
  2840. return "SG" + townTypeInfixes[town->town->typeID] + "TPWL.BMP";
  2841. default:
  2842. return "";
  2843. }
  2844. }
  2845. case 2: //keep
  2846. return "SG" + townTypeInfixes[town->town->typeID] + "MAN" + addit + ".BMP";
  2847. case 3: //bottom tower
  2848. return "SG" + townTypeInfixes[town->town->typeID] + "TW1" + addit + ".BMP";
  2849. case 4: //bottom wall
  2850. return "SG" + townTypeInfixes[town->town->typeID] + "WA1" + addit + ".BMP";
  2851. case 5: //below gate
  2852. return "SG" + townTypeInfixes[town->town->typeID] + "WA3" + addit + ".BMP";
  2853. case 6: //over gate
  2854. return "SG" + townTypeInfixes[town->town->typeID] + "WA4" + addit + ".BMP";
  2855. case 7: //upper wall
  2856. return "SG" + townTypeInfixes[town->town->typeID] + "WA6" + addit + ".BMP";
  2857. case 8: //upper tower
  2858. return "SG" + townTypeInfixes[town->town->typeID] + "TW2" + addit + ".BMP";
  2859. case 9: //gate
  2860. return "SG" + townTypeInfixes[town->town->typeID] + "DRW" + addit + ".BMP";
  2861. case 10: //gate arch
  2862. return "SG" + townTypeInfixes[town->town->typeID] + "ARCH.BMP";
  2863. case 11: //bottom static wall
  2864. return "SG" + townTypeInfixes[town->town->typeID] + "WA2.BMP";
  2865. case 12: //upper static wall
  2866. return "SG" + townTypeInfixes[town->town->typeID] + "WA5.BMP";
  2867. case 13: //moat
  2868. return "SG" + townTypeInfixes[town->town->typeID] + "MOAT.BMP";
  2869. case 14: //mlip
  2870. return "SG" + townTypeInfixes[town->town->typeID] + "MLIP.BMP";
  2871. case 15: //keep creature cover
  2872. return "SG" + townTypeInfixes[town->town->typeID] + "MANC.BMP";
  2873. case 16: //bottom turret creature cover
  2874. return "SG" + townTypeInfixes[town->town->typeID] + "TW1C.BMP";
  2875. case 17: //upper turret creature cover
  2876. return "SG" + townTypeInfixes[town->town->typeID] + "TW2C.BMP";
  2877. default:
  2878. return "";
  2879. }
  2880. }
  2881. /// What: 1. background wall, 2. keep, 3. bottom tower, 4. bottom wall, 5. wall below gate,
  2882. /// 6. wall over gate, 7. upper wall, 8. upper tower, 9. gate, 10. gate arch, 11. bottom static wall, 12. upper static wall, 13. moat, 14. mlip,
  2883. /// 15. keep turret cover, 16. lower turret cover, 17. upper turret cover
  2884. /// Positions are loaded from the config file: /config/wall_pos.txt
  2885. void CBattleInterface::SiegeHelper::printPartOfWall(SDL_Surface * to, int what)
  2886. {
  2887. SPoint pos = SPoint(-1, -1);
  2888. if (what >= 1 && what <= 17)
  2889. {
  2890. pos.x = graphics->wallPositions[town->town->typeID][what - 1].x + owner->pos.x;
  2891. pos.y = graphics->wallPositions[town->town->typeID][what - 1].y + owner->pos.y;
  2892. }
  2893. if(pos.x != -1)
  2894. {
  2895. blitAt(walls[what], pos.x, pos.y, to);
  2896. }
  2897. }
  2898. double SCatapultSProjectileInfo::calculateY(double x)
  2899. {
  2900. return (facA * pow(10., -3.)) * pow(x, 2.0) + facB * x + facC;
  2901. }