CBattleInterface.cpp 103 KB

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