CBattleInterface.cpp 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266
  1. #include "CBattleInterface.h"
  2. #include "CGameInfo.h"
  3. #include "../lib/CLodHandler.h"
  4. #include "SDL_Extensions.h"
  5. #include "CAdvmapInterface.h"
  6. #include "AdventureMapButton.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 "CCursorHandler.h"
  15. #include "../CCallback.h"
  16. #include "../lib/BattleState.h"
  17. #include "../lib/CGeneralTextHandler.h"
  18. #include "CCreatureAnimation.h"
  19. #include "Graphics.h"
  20. #include "CSpellWindow.h"
  21. #include "CConfigHandler.h"
  22. #include <queue>
  23. #include <sstream>
  24. #include "../lib/CondSh.h"
  25. #include "../lib/NetPacks.h"
  26. #include "CPlayerInterface.h"
  27. #include "CVideoHandler.h"
  28. #include "../lib/CTownHandler.h"
  29. #include <boost/assign/list_of.hpp>
  30. #include <boost/foreach.hpp>
  31. #include <boost/lexical_cast.hpp>
  32. #include <boost/algorithm/string/replace.hpp>
  33. #ifndef __GNUC__
  34. const double M_PI = 3.14159265358979323846;
  35. #else
  36. #define _USE_MATH_DEFINES
  37. #include <cmath>
  38. #endif
  39. /*
  40. * CBattleInterface.cpp, part of VCMI engine
  41. *
  42. * Authors: listed in file AUTHORS in main folder
  43. *
  44. * License: GNU General Public License v2.0 or later
  45. * Full text of license available in license.txt file, in main folder
  46. *
  47. */
  48. extern SDL_Surface * screen;
  49. extern SDL_Color zwykly;
  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, float rCor, float gCor, float 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 = (float)((colorsToChange+g)->r) * rCor;
  68. (colorsToChange+g)->g = (float)((colorsToChange+g)->g) * gCor;
  69. (colorsToChange+g)->b = (float)((colorsToChange+g)->b) * bCor;
  70. }
  71. }
  72. }
  73. ////////////////////////Battle helpers
  74. //general anim
  75. void CBattleAnimation::endAnim()
  76. {
  77. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  78. {
  79. if(it->first == this)
  80. {
  81. it->first = NULL;
  82. }
  83. }
  84. }
  85. bool CBattleAnimation::isEarliest(bool perStackConcurrency)
  86. {
  87. int lowestMoveID = owner->animIDhelper + 5;
  88. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  89. {
  90. CBattleStackAnimation * stAnim = dynamic_cast<CBattleStackAnimation *>(it->first);
  91. CBattleStackAnimation * thAnim = dynamic_cast<CBattleStackAnimation *>(this);
  92. if(perStackConcurrency && stAnim && thAnim && stAnim->stack->ID != thAnim->stack->ID)
  93. continue;
  94. CReverseAnim * revAnim = dynamic_cast<CReverseAnim *>(stAnim);
  95. if(revAnim && thAnim && stAnim && stAnim->stack->ID == thAnim->stack->ID && revAnim->priority)
  96. return false;
  97. if(it->first)
  98. amin(lowestMoveID, it->first->ID);
  99. }
  100. return ID == lowestMoveID;
  101. }
  102. CBattleAnimation::CBattleAnimation(CBattleInterface * _owner)
  103. : owner(_owner), ID(_owner->animIDhelper++)
  104. {}
  105. //Dummy animation
  106. bool CDummyAnim::init()
  107. {
  108. return true;
  109. }
  110. void CDummyAnim::nextFrame()
  111. {
  112. counter++;
  113. if(counter > howMany)
  114. endAnim();
  115. }
  116. void CDummyAnim::endAnim()
  117. {
  118. CBattleAnimation::endAnim();
  119. delete this;
  120. }
  121. CDummyAnim::CDummyAnim(CBattleInterface * _owner, int howManyFrames) : CBattleAnimation(_owner), counter(0), howMany(howManyFrames)
  122. {
  123. }
  124. //effect animation
  125. bool CSpellEffectAnim::init()
  126. {
  127. if(!isEarliest(false))
  128. return false;
  129. if(effect == 12) //armageddon
  130. {
  131. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  132. {
  133. CDefHandler * anim;
  134. if(customAnim.size())
  135. anim = CDefHandler::giveDef(customAnim);
  136. else
  137. anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  138. if (Vflip)
  139. {
  140. for (int v=0; v<anim->ourImages.size(); ++v)
  141. {
  142. CSDL_Ext::VflipSurf(anim->ourImages[v].bitmap);
  143. }
  144. }
  145. for(int i=0; i * anim->width < owner->pos.w ; ++i)
  146. {
  147. for(int j=0; j * anim->height < owner->pos.h ; ++j)
  148. {
  149. SBattleEffect be;
  150. be.effectID = ID;
  151. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  152. if (Vflip)
  153. {
  154. for (int v=0; v<be.anim->ourImages.size(); ++v)
  155. {
  156. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  157. }
  158. }
  159. be.frame = 0;
  160. be.maxFrame = be.anim->ourImages.size();
  161. be.x = i * anim->width + owner->pos.x;
  162. be.y = j * anim->height + owner->pos.y;
  163. owner->battleEffects.push_back(be);
  164. }
  165. }
  166. }
  167. else //there is nothing to play
  168. {
  169. endAnim();
  170. return false;
  171. }
  172. }
  173. else // Effects targeted at a specific creature/hex.
  174. {
  175. if(effect == -1 || graphics->battleACToDef[effect].size() != 0)
  176. {
  177. const CStack* destStack = owner->curInt->cb->battleGetStackByPos(destTile, false);
  178. Rect &tilePos = owner->bfield[destTile].pos;
  179. SBattleEffect be;
  180. be.effectID = ID;
  181. if(customAnim.size())
  182. be.anim = CDefHandler::giveDef(customAnim);
  183. else
  184. be.anim = CDefHandler::giveDef(graphics->battleACToDef[effect][0]);
  185. if (Vflip)
  186. {
  187. for (int v=0; v<be.anim->ourImages.size(); ++v)
  188. {
  189. CSDL_Ext::VflipSurf(be.anim->ourImages[v].bitmap);
  190. }
  191. }
  192. be.frame = 0;
  193. be.maxFrame = be.anim->ourImages.size();
  194. if(effect == 1)
  195. be.maxFrame = 3;
  196. switch (effect)
  197. {
  198. case -1:
  199. be.x = x;
  200. be.y = y;
  201. break;
  202. case 0: // Prayer and Lightning Bolt.
  203. case 1:
  204. // Position effect with it's bottom center touching the bottom center of affected tile(s).
  205. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  206. be.y = tilePos.y + tilePos.h - be.anim->height;
  207. break;
  208. default:
  209. // Position effect with it's center touching the top center of affected tile(s).
  210. be.x = tilePos.x + tilePos.w/2 - be.anim->width/2;
  211. be.y = tilePos.y - be.anim->height/2;
  212. break;
  213. }
  214. // Correction for 2-hex creatures.
  215. if (destStack != NULL && destStack->doubleWide())
  216. be.x += (destStack->attackerOwned ? -1 : 1)*tilePos.w/2;
  217. owner->battleEffects.push_back(be);
  218. }
  219. else //there is nothing to play
  220. {
  221. endAnim();
  222. return false;
  223. }
  224. }
  225. //battleEffects
  226. return true;
  227. }
  228. void CSpellEffectAnim::nextFrame()
  229. {
  230. //notice: there may be more than one effect in owner->battleEffects correcponding to this animation (ie. armageddon)
  231. for(std::list<SBattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  232. {
  233. if(it->effectID == ID)
  234. {
  235. ++(it->frame);
  236. if(it->frame == it->maxFrame)
  237. {
  238. endAnim();
  239. break;
  240. }
  241. else
  242. {
  243. it->x += dx;
  244. it->y += dy;
  245. }
  246. }
  247. }
  248. }
  249. void CSpellEffectAnim::endAnim()
  250. {
  251. CBattleAnimation::endAnim();
  252. std::vector<std::list<SBattleEffect>::iterator> toDel;
  253. for(std::list<SBattleEffect>::iterator it = owner->battleEffects.begin(); it != owner->battleEffects.end(); ++it)
  254. {
  255. if(it->effectID == ID)
  256. {
  257. toDel.push_back(it);
  258. }
  259. }
  260. for(int b=0; b<toDel.size(); ++b)
  261. {
  262. delete toDel[b]->anim;
  263. owner->battleEffects.erase(toDel[b]);
  264. }
  265. delete this;
  266. }
  267. CSpellEffectAnim::CSpellEffectAnim(CBattleInterface * _owner, ui32 _effect, THex _destTile, int _dx, int _dy, bool _Vflip)
  268. :CBattleAnimation(_owner), effect(_effect), destTile(_destTile), customAnim(""), dx(_dx), dy(_dy), Vflip(_Vflip)
  269. {
  270. }
  271. CSpellEffectAnim::CSpellEffectAnim(CBattleInterface * _owner, std::string _customAnim, int _x, int _y, int _dx, int _dy, bool _Vflip)
  272. :CBattleAnimation(_owner), effect(-1), destTile(0), customAnim(_customAnim), x(_x), y(_y), dx(_dx), dy(_dy), Vflip(_Vflip)
  273. {
  274. }
  275. //stack's aniamtion
  276. CBattleStackAnimation::CBattleStackAnimation(CBattleInterface * _owner, const CStack * _stack)
  277. : CBattleAnimation(_owner), stack(_stack)
  278. {
  279. }
  280. bool CBattleStackAnimation::isToReverseHlp(THex hexFrom, THex hexTo, bool curDir)
  281. {
  282. int fromMod = hexFrom % BFIELD_WIDTH;
  283. int fromDiv = hexFrom / BFIELD_WIDTH;
  284. int toMod = hexTo % BFIELD_WIDTH;
  285. if(curDir && fromMod < toMod)
  286. return false;
  287. else if(curDir && fromMod > toMod)
  288. return true;
  289. else if(curDir && fromMod == toMod)
  290. {
  291. return fromDiv % 2 == 0;
  292. }
  293. else if(!curDir && fromMod < toMod)
  294. return true;
  295. else if(!curDir && fromMod > toMod)
  296. return false;
  297. else if(!curDir && fromMod == toMod)
  298. {
  299. return fromDiv % 2 == 1;
  300. }
  301. tlog1 << "Catastrope in CBattleStackAnimation::isToReverse!" << std::endl;
  302. return false; //should never happen
  303. }
  304. bool CBattleStackAnimation::isToReverse(THex hexFrom, THex hexTo, bool curDir, bool toDoubleWide, bool toDir)
  305. {
  306. if(hexTo < 0) //turret
  307. return false;
  308. if(toDoubleWide)
  309. {
  310. return isToReverseHlp(hexFrom, hexTo, curDir) &&
  311. (toDir ? isToReverseHlp(hexFrom, hexTo-1, curDir) : isToReverseHlp(hexFrom, hexTo+1, curDir) );
  312. }
  313. else
  314. {
  315. return isToReverseHlp(hexFrom, hexTo, curDir);
  316. }
  317. }
  318. //revering animation
  319. bool CReverseAnim::init()
  320. {
  321. if(owner->creAnims[stack->ID] == NULL || owner->creAnims[stack->ID]->getType() == 5)
  322. {
  323. endAnim();
  324. return false; //there is no such creature
  325. }
  326. if(!priority && !isEarliest(false))
  327. return false;
  328. owner->creAnims[stack->ID]->setType(8);
  329. return true;
  330. }
  331. void CReverseAnim::nextFrame()
  332. {
  333. if(partOfAnim == 1) //first part of animation
  334. {
  335. if(owner->creAnims[stack->ID]->onLastFrameInGroup())
  336. {
  337. partOfAnim = 2;
  338. }
  339. }
  340. else if(partOfAnim == 2)
  341. {
  342. if(!secondPartSetup)
  343. {
  344. owner->creDir[stack->ID] = !owner->creDir[stack->ID];
  345. if(!stack)
  346. {
  347. endAnim();
  348. return;
  349. }
  350. Point coords = CBattleHex::getXYUnitAnim(hex, owner->creDir[stack->ID], stack, owner);
  351. owner->creAnims[stack->ID]->pos.x = coords.x;
  352. //creAnims[stackID]->pos.y = coords.second;
  353. if(stack->doubleWide())
  354. {
  355. if(stack->attackerOwned)
  356. {
  357. if(!owner->creDir[stack->ID])
  358. owner->creAnims[stack->ID]->pos.x -= 44;
  359. }
  360. else
  361. {
  362. if(owner->creDir[stack->ID])
  363. owner->creAnims[stack->ID]->pos.x += 44;
  364. }
  365. }
  366. owner->creAnims[stack->ID]->setType(7);
  367. secondPartSetup = true;
  368. }
  369. if(owner->creAnims[stack->ID]->onLastFrameInGroup())
  370. {
  371. endAnim();
  372. }
  373. }
  374. }
  375. void CReverseAnim::endAnim()
  376. {
  377. CBattleAnimation::endAnim();
  378. if( stack->alive() )//don't do that if stack is dead
  379. owner->creAnims[stack->ID]->setType(2);
  380. delete this;
  381. }
  382. CReverseAnim::CReverseAnim(CBattleInterface * _owner, const CStack * stack, THex dest, bool _priority)
  383. : CBattleStackAnimation(_owner, stack), partOfAnim(1), secondPartSetup(false), hex(dest), priority(_priority)
  384. {
  385. }
  386. //defence anim
  387. bool CDefenceAnim::init()
  388. {
  389. //checking initial conditions
  390. //if(owner->creAnims[stackID]->getType() != 2)
  391. //{
  392. // return false;
  393. //}
  394. if(attacker == NULL && owner->battleEffects.size() > 0)
  395. return false;
  396. int lowestMoveID = owner->animIDhelper + 5;
  397. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  398. {
  399. CDefenceAnim * defAnim = dynamic_cast<CDefenceAnim *>(it->first);
  400. if(defAnim && defAnim->stack->ID != stack->ID)
  401. continue;
  402. CBattleAttack * attAnim = dynamic_cast<CBattleAttack *>(it->first);
  403. if(attAnim && attAnim->stack->ID != stack->ID)
  404. continue;
  405. if(attacker != NULL)
  406. {
  407. int attackerAnimType = owner->creAnims[attacker->ID]->getType();
  408. if( attackerAnimType == 11 && attackerAnimType == 12 && attackerAnimType == 13 && owner->creAnims[attacker->ID]->getFrame() < attacker->getCreature()->attackClimaxFrame )
  409. return false;
  410. }
  411. CReverseAnim * animAsRev = dynamic_cast<CReverseAnim *>(it->first);
  412. if(animAsRev && animAsRev->priority)
  413. return false;
  414. if(it->first)
  415. amin(lowestMoveID, it->first->ID);
  416. }
  417. if(ID > lowestMoveID)
  418. return false;
  419. //reverse unit if necessary
  420. if(attacker && isToReverse(stack->position, attacker->position, owner->creDir[stack->ID], attacker->doubleWide(), owner->creDir[attacker->ID]))
  421. {
  422. owner->addNewAnim(new CReverseAnim(owner, stack, stack->position, true));
  423. return false;
  424. }
  425. //unit reversed
  426. if(byShooting) //delay hit animation
  427. {
  428. for(std::list<SProjectileInfo>::const_iterator it = owner->projectiles.begin(); it != owner->projectiles.end(); ++it)
  429. {
  430. if(it->creID == attacker->getCreature()->idNumber)
  431. {
  432. return false;
  433. }
  434. }
  435. }
  436. //initializing
  437. if(killed)
  438. {
  439. CCS->soundh->playSound(battle_sound(stack->getCreature(), killed));
  440. owner->creAnims[stack->ID]->setType(5); //death
  441. }
  442. else
  443. {
  444. // TODO: this block doesn't seems correct if the unit is defending.
  445. CCS->soundh->playSound(battle_sound(stack->getCreature(), wince));
  446. owner->creAnims[stack->ID]->setType(3); //getting hit
  447. }
  448. return true; //initialized successfuly
  449. }
  450. void CDefenceAnim::nextFrame()
  451. {
  452. if(!killed && owner->creAnims[stack->ID]->getType() != 3)
  453. {
  454. owner->creAnims[stack->ID]->setType(3);
  455. }
  456. if(!owner->creAnims[stack->ID]->onLastFrameInGroup())
  457. {
  458. if( owner->creAnims[stack->ID]->getType() == 5 && (owner->animCount+1)%(4/owner->curInt->sysOpts.animSpeed)==0
  459. && !owner->creAnims[stack->ID]->onLastFrameInGroup() )
  460. {
  461. owner->creAnims[stack->ID]->incrementFrame();
  462. }
  463. }
  464. else
  465. {
  466. endAnim();
  467. }
  468. }
  469. void CDefenceAnim::endAnim()
  470. {
  471. //restoring animType
  472. if(owner->creAnims[stack->ID]->getType() == 3)
  473. owner->creAnims[stack->ID]->setType(2);
  474. //printing info to console
  475. if(attacker!=NULL)
  476. owner->printConsoleAttacked(stack, dmg, amountKilled, attacker);
  477. //const CStack * attacker = owner->curInt->cb->battleGetStackByID(IDby, false);
  478. //const CStack * attacked = owner->curInt->cb->battleGetStackByID(stackID, false);
  479. CBattleAnimation::endAnim();
  480. delete this;
  481. }
  482. CDefenceAnim::CDefenceAnim(SStackAttackedInfo _attackedInfo, CBattleInterface * _owner)
  483. : CBattleStackAnimation(_owner, _attackedInfo.defender), dmg(_attackedInfo.dmg),
  484. amountKilled(_attackedInfo.amountKilled), attacker(_attackedInfo.attacker), byShooting(_attackedInfo.byShooting),
  485. killed(_attackedInfo.killed)
  486. {
  487. }
  488. ////move anim
  489. bool CBattleStackMoved::init()
  490. {
  491. if( !isEarliest(false) )
  492. return false;
  493. //a few useful variables
  494. steps = owner->creAnims[stack->ID]->framesInGroup(0)*owner->getAnimSpeedMultiplier()-1;
  495. if(steps == 0) //this creature seems to have no move animation so we can end it immediately
  496. {
  497. endAnim();
  498. return false;
  499. }
  500. whichStep = 0;
  501. int hexWbase = 44, hexHbase = 42;
  502. const CStack * movedStack = stack;
  503. if(!movedStack || owner->creAnims[stack->ID]->getType() == 5)
  504. {
  505. endAnim();
  506. return false;
  507. }
  508. //bool twoTiles = movedStack->doubleWide();
  509. Point begPosition = CBattleHex::getXYUnitAnim(curStackPos, movedStack->attackerOwned, movedStack, owner);
  510. Point endPosition = CBattleHex::getXYUnitAnim(destHex, movedStack->attackerOwned, movedStack, owner);
  511. int mutPos = THex::mutualPosition(curStackPos, destHex);
  512. //reverse unit if necessary
  513. if((begPosition.x > endPosition.x) && owner->creDir[stack->ID] == true)
  514. {
  515. owner->addNewAnim(new CReverseAnim(owner, stack, curStackPos, true));
  516. return false;
  517. }
  518. else if ((begPosition.x < endPosition.x) && owner->creDir[stack->ID] == false)
  519. {
  520. owner->addNewAnim(new CReverseAnim(owner, stack, curStackPos, true));
  521. return false;
  522. }
  523. if(owner->creAnims[stack->ID]->getType() != 0)
  524. {
  525. owner->creAnims[stack->ID]->setType(0);
  526. }
  527. //unit reversed
  528. if(owner->moveSh <= 0)
  529. owner->moveSh = CCS->soundh->playSound(battle_sound(movedStack->getCreature(), move), -1);
  530. //step shift calculation
  531. posX = owner->creAnims[stack->ID]->pos.x, posY = owner->creAnims[stack->ID]->pos.y; // for precise calculations ;]
  532. if(mutPos == -1 && movedStack->hasBonusOfType(Bonus::FLYING))
  533. {
  534. steps *= distance;
  535. steps /= 2; //to make animation faster
  536. stepX = (endPosition.x - (float)begPosition.x)/steps;
  537. stepY = (endPosition.y - (float)begPosition.y)/steps;
  538. }
  539. else
  540. {
  541. switch(mutPos)
  542. {
  543. case 0:
  544. stepX = (-1.0)*((float)hexWbase)/(2.0f*steps);
  545. stepY = (-1.0)*((float)hexHbase)/((float)steps);
  546. break;
  547. case 1:
  548. stepX = ((float)hexWbase)/(2.0f*steps);
  549. stepY = (-1.0)*((float)hexHbase)/((float)steps);
  550. break;
  551. case 2:
  552. stepX = ((float)hexWbase)/((float)steps);
  553. stepY = 0.0;
  554. break;
  555. case 3:
  556. stepX = ((float)hexWbase)/(2.0f*steps);
  557. stepY = ((float)hexHbase)/((float)steps);
  558. break;
  559. case 4:
  560. stepX = (-1.0)*((float)hexWbase)/(2.0f*steps);
  561. stepY = ((float)hexHbase)/((float)steps);
  562. break;
  563. case 5:
  564. stepX = (-1.0)*((float)hexWbase)/((float)steps);
  565. stepY = 0.0;
  566. break;
  567. }
  568. }
  569. //step shifts calculated
  570. return true;
  571. }
  572. void CBattleStackMoved::nextFrame()
  573. {
  574. //moving instructions
  575. posX += stepX;
  576. owner->creAnims[stack->ID]->pos.x = posX;
  577. posY += stepY;
  578. owner->creAnims[stack->ID]->pos.y = posY;
  579. ++whichStep;
  580. if(whichStep == steps)
  581. {
  582. endAnim();
  583. }
  584. }
  585. void CBattleStackMoved::endAnim()
  586. {
  587. const CStack * movedStack = stack;
  588. CBattleAnimation::endAnim();
  589. if(movedStack)
  590. {
  591. bool twoTiles = movedStack->doubleWide();
  592. if(endMoving)
  593. {
  594. owner->addNewAnim(new CBattleMoveEnd(owner, stack, destHex));
  595. }
  596. Point coords = CBattleHex::getXYUnitAnim(destHex, owner->creDir[stack->ID], movedStack, owner);
  597. owner->creAnims[stack->ID]->pos = coords;
  598. if(!endMoving && twoTiles && bool(movedStack->attackerOwned) && (owner->creDir[stack->ID] != bool(movedStack->attackerOwned) )) //big attacker creature is reversed
  599. owner->creAnims[stack->ID]->pos.x -= 44;
  600. else if(!endMoving && twoTiles && (! bool(movedStack->attackerOwned) ) && (owner->creDir[stack->ID] != bool(movedStack->attackerOwned) )) //big defender creature is reversed
  601. owner->creAnims[stack->ID]->pos.x += 44;
  602. }
  603. if(owner->moveSh >= 0)
  604. {
  605. CCS->soundh->stopSound(owner->moveSh);
  606. owner->moveSh = -1;
  607. }
  608. delete this;
  609. }
  610. CBattleStackMoved::CBattleStackMoved(CBattleInterface * _owner, const CStack * _stack, THex _destHex, bool _endMoving, int _distance)
  611. : CBattleStackAnimation(_owner, _stack), destHex(_destHex), endMoving(_endMoving), distance(_distance), stepX(0.0f), stepY(0.0f)
  612. {
  613. curStackPos = stack->position;
  614. }
  615. //move started
  616. bool CBattleMoveStart::init()
  617. {
  618. if( !isEarliest(false) )
  619. return false;
  620. if(!stack || owner->creAnims[stack->ID]->getType() == 5)
  621. {
  622. CBattleMoveStart::endAnim();
  623. return false;
  624. }
  625. CCS->soundh->playSound(battle_sound(stack->getCreature(), startMoving));
  626. return true;
  627. }
  628. void CBattleMoveStart::nextFrame()
  629. {
  630. if(owner->creAnims[stack->ID]->onLastFrameInGroup())
  631. {
  632. endAnim();
  633. }
  634. else
  635. {
  636. if((owner->animCount+1)%(4/owner->curInt->sysOpts.animSpeed)==0)
  637. owner->creAnims[stack->ID]->incrementFrame();
  638. }
  639. }
  640. void CBattleMoveStart::endAnim()
  641. {
  642. CBattleAnimation::endAnim();
  643. delete this;
  644. }
  645. CBattleMoveStart::CBattleMoveStart(CBattleInterface * _owner, const CStack * _stack)
  646. : CBattleStackAnimation(_owner, _stack)
  647. {
  648. }
  649. //move finished
  650. bool CBattleMoveEnd::init()
  651. {
  652. if( !isEarliest(true) )
  653. return false;
  654. if(!stack || owner->creAnims[stack->ID]->framesInGroup(21) == 0 || owner->creAnims[stack->ID]->getType() == 5)
  655. {
  656. endAnim();
  657. return false;
  658. }
  659. CCS->soundh->playSound(battle_sound(stack->getCreature(), endMoving));
  660. owner->creAnims[stack->ID]->setType(21);
  661. return true;
  662. }
  663. void CBattleMoveEnd::nextFrame()
  664. {
  665. if(owner->creAnims[stack->ID]->onLastFrameInGroup())
  666. {
  667. endAnim();
  668. }
  669. }
  670. void CBattleMoveEnd::endAnim()
  671. {
  672. CBattleAnimation::endAnim();
  673. if(owner->creAnims[stack->ID]->getType() != 5)
  674. owner->creAnims[stack->ID]->setType(2); //resetting to default
  675. CCS->curh->show();
  676. delete this;
  677. }
  678. CBattleMoveEnd::CBattleMoveEnd(CBattleInterface * _owner, const CStack * _stack, THex destTile)
  679. : CBattleStackAnimation(_owner, _stack), destinationTile(destTile)
  680. {
  681. }
  682. //general attack anim
  683. void CBattleAttack::nextFrame()
  684. {
  685. if(owner->creAnims[stack->ID]->getType() != group)
  686. owner->creAnims[stack->ID]->setType(group);
  687. if(owner->creAnims[stack->ID]->onFirstFrameInGroup())
  688. {
  689. if(shooting)
  690. CCS->soundh->playSound(battle_sound(attackingStack->getCreature(), shoot));
  691. else
  692. CCS->soundh->playSound(battle_sound(attackingStack->getCreature(), attack));
  693. }
  694. else if(owner->creAnims[stack->ID]->onLastFrameInGroup())
  695. {
  696. owner->creAnims[stack->ID]->setType(2);
  697. endAnim();
  698. return; //execution of endAnim deletes this !!!
  699. }
  700. }
  701. bool CBattleAttack::checkInitialConditions()
  702. {
  703. return isEarliest(false);
  704. }
  705. CBattleAttack::CBattleAttack(CBattleInterface * _owner, const CStack * attacker, THex _dest, const CStack * defender)
  706. : CBattleStackAnimation(_owner, attacker), dest(_dest), attackedStack(defender), attackingStack(attacker)
  707. {
  708. assert(attackingStack && "attackingStack is NULL in CBattleAttack::CBattleAttack !\n");
  709. if(attackingStack->getCreature()->idNumber != 145) //catapult is allowed to attack not-creature
  710. {
  711. assert(attackedStack && "attackedStack is NULL in CBattleAttack::CBattleAttack !\n");
  712. }
  713. else //catapult can attack walls only
  714. {
  715. assert(owner->curInt->cb->battleGetWallUnderHex(_dest) >= 0);
  716. }
  717. attackingStackPosBeforeReturn = attackingStack->position;
  718. }
  719. ////melee attack
  720. bool CMeleeAttack::init()
  721. {
  722. if( !CBattleAttack::checkInitialConditions() )
  723. return false;
  724. //if(owner->creAnims[stackID]->getType()!=2)
  725. //{
  726. // return false;
  727. //}
  728. if(!attackingStack || owner->creAnims[stack->ID]->getType() == 5)
  729. {
  730. endAnim();
  731. return false;
  732. }
  733. int reversedShift = 0; //shift of attacking stack's position due to reversing
  734. if(attackingStack->attackerOwned)
  735. {
  736. if(attackingStack->doubleWide() && THex::mutualPosition(attackingStackPosBeforeReturn, dest) == -1)
  737. {
  738. if(THex::mutualPosition(attackingStackPosBeforeReturn + (attackingStack->attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  739. {
  740. reversedShift = (attackingStack->attackerOwned ? -1 : 1);
  741. }
  742. }
  743. }
  744. else //if(astack->attackerOwned)
  745. {
  746. if(attackingStack->doubleWide() && THex::mutualPosition(attackingStackPosBeforeReturn, dest) == -1)
  747. {
  748. if(THex::mutualPosition(attackingStackPosBeforeReturn + (attackingStack->attackerOwned ? -1 : 1), dest) >= 0) //if reversing stack will make its position adjacent to dest
  749. {
  750. reversedShift = (attackingStack->attackerOwned ? -1 : 1);
  751. }
  752. }
  753. }
  754. //reversing stack if necessary
  755. if(isToReverse(attackingStackPosBeforeReturn, dest, owner->creDir[stack->ID], attackedStack->doubleWide(), owner->creDir[attackedStack->ID]))
  756. {
  757. owner->addNewAnim(new CReverseAnim(owner, stack, attackingStackPosBeforeReturn, true));
  758. return false;
  759. }
  760. //reversed
  761. shooting = false;
  762. posShiftDueToDist = reversedShift;
  763. static const int mutPosToGroup[] = {11, 11, 12, 13, 13, 12};
  764. int mutPos = THex::mutualPosition(attackingStackPosBeforeReturn + reversedShift, dest);
  765. if(mutPos == -1 && attackedStack->doubleWide())
  766. {
  767. mutPos = THex::mutualPosition(attackingStackPosBeforeReturn + reversedShift, attackedStack->occupiedHex());
  768. }
  769. switch(mutPos) //attack direction
  770. {
  771. case 0: case 1: case 2: case 3: case 4: case 5:
  772. group = mutPosToGroup[mutPos];
  773. break;
  774. default:
  775. tlog1<<"Critical Error! Wrong dest in stackAttacking! dest: "<<dest<<" attacking stack pos: "<<attackingStackPosBeforeReturn<<" reversed shift: "<<reversedShift<<std::endl;
  776. group = 11;
  777. break;
  778. }
  779. return true;
  780. }
  781. void CMeleeAttack::nextFrame()
  782. {
  783. /*for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  784. {
  785. CBattleMoveStart * anim = dynamic_cast<CBattleMoveStart *>(it->first);
  786. CReverseAnim * anim2 = dynamic_cast<CReverseAnim *>(it->first);
  787. if( (anim && anim->stackID == stackID) || (anim2 && anim2->stackID == stackID ) )
  788. return;
  789. }*/
  790. CBattleAttack::nextFrame();
  791. }
  792. void CMeleeAttack::endAnim()
  793. {
  794. CBattleAnimation::endAnim();
  795. delete this;
  796. }
  797. CMeleeAttack::CMeleeAttack(CBattleInterface * _owner, const CStack * attacker, THex _dest, const CStack * _attacked)
  798. : CBattleAttack(_owner, attacker, _dest, _attacked)
  799. {
  800. }
  801. //shooting anim
  802. bool CShootingAnim::init()
  803. {
  804. if( !CBattleAttack::checkInitialConditions() )
  805. return false;
  806. const CStack * shooter = attackingStack;
  807. if(!shooter || owner->creAnims[stack->ID]->getType() == 5)
  808. {
  809. endAnim();
  810. return false;
  811. }
  812. //projectile
  813. float projectileAngle; //in radians; if positive, projectiles goes up
  814. float straightAngle = 0.2f; //maximal angle in radians between straight horizontal line and shooting line for which shot is considered to be straight (absoulte value)
  815. int fromHex = shooter->position;
  816. projectileAngle = atan2(float(abs(dest - fromHex)/BFIELD_WIDTH), float(abs(dest - fromHex)%BFIELD_WIDTH));
  817. if(fromHex < dest)
  818. projectileAngle = -projectileAngle;
  819. SProjectileInfo spi;
  820. spi.creID = shooter->getCreature()->idNumber;
  821. spi.reverse = !shooter->attackerOwned;
  822. spi.step = 0;
  823. spi.frameNum = 0;
  824. if(vstd::contains(CGI->creh->idToProjectileSpin, spi.creID))
  825. spi.spin = CGI->creh->idToProjectileSpin[spi.creID];
  826. else
  827. {
  828. tlog2 << "Warning - no projectile spin for spi.creID " << spi.creID << std::endl;
  829. spi.spin = false;
  830. }
  831. Point xycoord = CBattleHex::getXYUnitAnim(shooter->position, true, shooter, owner);
  832. Point destcoord;
  833. if(attackedStack)
  834. {
  835. destcoord = CBattleHex::getXYUnitAnim(dest, false, attackedStack, owner);
  836. }
  837. else //catapult attack
  838. {
  839. destcoord.x = -160 + 22 * ( ((dest/BFIELD_WIDTH) + 1)%2 ) + 44 * (dest % BFIELD_WIDTH);
  840. destcoord.y = -139 + 42 * (dest/BFIELD_WIDTH);
  841. }
  842. destcoord.x += 250; destcoord.y += 210; //TODO: find a better place to shoot
  843. if(projectileAngle > straightAngle) //upper shot
  844. {
  845. spi.x = xycoord.x + 200 + shooter->getCreature()->upperRightMissleOffsetX;
  846. spi.y = xycoord.y + 100 - shooter->getCreature()->upperRightMissleOffsetY;
  847. }
  848. else if(projectileAngle < -straightAngle) //lower shot
  849. {
  850. spi.x = xycoord.x + 200 + shooter->getCreature()->lowerRightMissleOffsetX;
  851. spi.y = xycoord.y + 150 - shooter->getCreature()->lowerRightMissleOffsetY;
  852. }
  853. else //straight shot
  854. {
  855. spi.x = xycoord.x + 200 + shooter->getCreature()->rightMissleOffsetX;
  856. spi.y = xycoord.y + 125 - shooter->getCreature()->rightMissleOffsetY;
  857. }
  858. spi.lastStep = sqrt((float)((destcoord.x - spi.x)*(destcoord.x - spi.x) + (destcoord.y - spi.y) * (destcoord.y - spi.y))) / 40;
  859. if(spi.lastStep == 0)
  860. spi.lastStep = 1;
  861. spi.dx = (destcoord.x - spi.x) / spi.lastStep;
  862. spi.dy = (destcoord.y - spi.y) / spi.lastStep;
  863. //set starting frame
  864. if(spi.spin)
  865. {
  866. spi.frameNum = 0;
  867. }
  868. else
  869. {
  870. spi.frameNum = ((M_PI/2.0f - projectileAngle) / (2.0f *M_PI) + 1/((float)(2*(owner->idToProjectile[spi.creID]->ourImages.size()-1)))) * (owner->idToProjectile[spi.creID]->ourImages.size()-1);
  871. }
  872. //set delay
  873. spi.animStartDelay = CGI->creh->creatures[spi.creID]->attackClimaxFrame;
  874. owner->projectiles.push_back(spi);
  875. //attack aniamtion
  876. posShiftDueToDist = 0;
  877. shooting = true;
  878. if(projectileAngle > straightAngle) //upper shot
  879. group = 14;
  880. else if(projectileAngle < -straightAngle) //lower shot
  881. group = 16;
  882. else //straight shot
  883. group = 15;
  884. return true;
  885. }
  886. void CShootingAnim::nextFrame()
  887. {
  888. for(std::list<std::pair<CBattleAnimation *, bool> >::const_iterator it = owner->pendingAnims.begin(); it != owner->pendingAnims.end(); ++it)
  889. {
  890. CBattleMoveStart * anim = dynamic_cast<CBattleMoveStart *>(it->first);
  891. CReverseAnim * anim2 = dynamic_cast<CReverseAnim *>(it->first);
  892. if( (anim && anim->stack->ID == stack->ID) || (anim2 && anim2->stack->ID == stack->ID && anim2->priority ) )
  893. return;
  894. }
  895. CBattleAttack::nextFrame();
  896. }
  897. void CShootingAnim::endAnim()
  898. {
  899. CBattleAnimation::endAnim();
  900. delete this;
  901. }
  902. CShootingAnim::CShootingAnim(CBattleInterface * _owner, const CStack * attacker, THex _dest, const CStack * _attacked, bool _catapult, int _catapultDmg)
  903. : CBattleAttack(_owner, attacker, _dest, _attacked), catapultDamage(_catapultDmg), catapult(_catapult)
  904. {
  905. if(catapult) //catapult attack
  906. {
  907. owner->addNewAnim( new CSpellEffectAnim(owner, "SGEXPL.DEF",
  908. -130 + 22 * ( ((dest/BFIELD_WIDTH) + 1)%2 ) + 44 * (dest % BFIELD_WIDTH) + owner->pos.x,
  909. -50 + 42 * (dest/BFIELD_WIDTH) + owner->pos.y ));
  910. }
  911. }
  912. ////////////////////////
  913. void CBattleInterface::addNewAnim(CBattleAnimation * anim)
  914. {
  915. pendingAnims.push_back( std::make_pair(anim, false) );
  916. animsAreDisplayed.setn(true);
  917. }
  918. CBattleInterface::CBattleInterface(const CCreatureSet * army1, const CCreatureSet * army2, CGHeroInstance *hero1, CGHeroInstance *hero2, const SDL_Rect & myRect, CPlayerInterface * att, CPlayerInterface * defen)
  919. : queue(NULL), attackingHeroInstance(hero1), defendingHeroInstance(hero2), animCount(0),
  920. activeStack(NULL), stackToActivate(NULL), mouseHoveredStack(-1), previouslyHoveredHex(-1),
  921. currentlyHoveredHex(-1), spellDestSelectMode(false), spellToCast(NULL), siegeH(NULL),
  922. attackerInt(att), defenderInt(defen), curInt(att), animIDhelper(0), givenCommand(NULL),
  923. myTurn(false), resWindow(NULL), moveStarted(false), moveSh(-1), bresult(NULL)
  924. {
  925. ObjectConstruction h__l__p(this);
  926. if(!curInt) curInt = LOCPLINT; //may happen when we are defending during network MP game
  927. animsAreDisplayed.setn(false);
  928. pos = myRect;
  929. strongInterest = true;
  930. givenCommand = new CondSh<BattleAction *>(NULL);
  931. //create stack queue
  932. bool embedQueue = screen->h < 700;
  933. queue = new CStackQueue(embedQueue, this);
  934. if(!embedQueue && curInt->sysOpts.showQueue)
  935. {
  936. pos.y += queue->pos.h / 2; //center whole window
  937. queue->moveTo(Point(pos.x, pos.y - queue->pos.h));
  938. // queue->pos.x = pos.x;
  939. // queue->pos.y = pos.y - queue->pos.h;
  940. // pos.h += queue->pos.h;
  941. // center();
  942. }
  943. queue->update();
  944. //preparing siege info
  945. const CGTownInstance * town = curInt->cb->battleGetDefendedTown();
  946. if(town && town->hasFort())
  947. {
  948. siegeH = new SiegeHelper(town, this);
  949. }
  950. curInt->battleInt = this;
  951. //initializing armies
  952. this->army1 = army1;
  953. this->army2 = army2;
  954. std::vector<const CStack*> stacks = curInt->cb->battleGetStacks(false);
  955. BOOST_FOREACH(const CStack *s, stacks)
  956. {
  957. newStack(s);
  958. }
  959. //preparing menu background and terrain
  960. if(siegeH)
  961. {
  962. background = BitmapHandler::loadBitmap( siegeH->getSiegeName(0), false );
  963. ui8 siegeLevel = curInt->cb->battleGetSiegeLevel();
  964. if(siegeLevel >= 2) //citadel or castle
  965. {
  966. //print moat/mlip
  967. SDL_Surface * moat = BitmapHandler::loadBitmap( siegeH->getSiegeName(13) ),
  968. * mlip = BitmapHandler::loadBitmap( siegeH->getSiegeName(14) );
  969. Point moatPos = graphics->wallPositions[siegeH->town->town->typeID][10],
  970. mlipPos = graphics->wallPositions[siegeH->town->town->typeID][11];
  971. if(moat) //eg. tower has no moat
  972. blitAt(moat, moatPos.x,moatPos.y, background);
  973. if(mlip) //eg. tower has no mlip
  974. blitAt(mlip, mlipPos.x, mlipPos.y, background);
  975. SDL_FreeSurface(moat);
  976. SDL_FreeSurface(mlip);
  977. }
  978. }
  979. else
  980. {
  981. std::vector< std::string > & backref = graphics->battleBacks[ curInt->cb->battleGetBattlefieldType() ];
  982. background = BitmapHandler::loadBitmap(backref[ rand() % backref.size()], false );
  983. }
  984. //preparing menu background
  985. menu = BitmapHandler::loadBitmap("CBAR.BMP");
  986. graphics->blueToPlayersAdv(menu, hero1->tempOwner);
  987. //preparing graphics for displaying amounts of creatures
  988. amountNormal = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  989. CSDL_Ext::alphaTransform(amountNormal);
  990. transformPalette(amountNormal, 0.59f, 0.19f, 0.93f);
  991. amountPositive = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  992. CSDL_Ext::alphaTransform(amountPositive);
  993. transformPalette(amountPositive, 0.18f, 1.00f, 0.18f);
  994. amountNegative = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  995. CSDL_Ext::alphaTransform(amountNegative);
  996. transformPalette(amountNegative, 1.00f, 0.18f, 0.18f);
  997. amountEffNeutral = BitmapHandler::loadBitmap("CMNUMWIN.BMP");
  998. CSDL_Ext::alphaTransform(amountEffNeutral);
  999. transformPalette(amountEffNeutral, 1.00f, 1.00f, 0.18f);
  1000. ////blitting menu background and terrain
  1001. blitAt(background, pos.x, pos.y);
  1002. blitAt(menu, pos.x, 556 + pos.y);
  1003. CSDL_Ext::update();
  1004. //preparing buttons and console
  1005. bOptions = new AdventureMapButton (CGI->generaltexth->zelp[381].first, CGI->generaltexth->zelp[381].second, boost::bind(&CBattleInterface::bOptionsf,this), 3 + pos.x, 561 + pos.y, "icm003.def", SDLK_o);
  1006. bSurrender = new AdventureMapButton (CGI->generaltexth->zelp[379].first, CGI->generaltexth->zelp[379].second, boost::bind(&CBattleInterface::bSurrenderf,this), 54 + pos.x, 561 + pos.y, "icm001.def", SDLK_s);
  1007. bFlee = new AdventureMapButton (CGI->generaltexth->zelp[380].first, CGI->generaltexth->zelp[380].second, boost::bind(&CBattleInterface::bFleef,this), 105 + pos.x, 561 + pos.y, "icm002.def", SDLK_r);
  1008. bSurrender->block(!curInt->cb->battleCanFlee());
  1009. bFlee->block(!curInt->cb->battleCanFlee());
  1010. bAutofight = new AdventureMapButton (CGI->generaltexth->zelp[382].first, CGI->generaltexth->zelp[382].second, boost::bind(&CBattleInterface::bAutofightf,this), 157 + pos.x, 561 + pos.y, "icm004.def", SDLK_a);
  1011. bSpell = new AdventureMapButton (CGI->generaltexth->zelp[385].first, CGI->generaltexth->zelp[385].second, boost::bind(&CBattleInterface::bSpellf,this), 645 + pos.x, 561 + pos.y, "icm005.def", SDLK_c);
  1012. bSpell->block(true);
  1013. bWait = new AdventureMapButton (CGI->generaltexth->zelp[386].first, CGI->generaltexth->zelp[386].second, boost::bind(&CBattleInterface::bWaitf,this), 696 + pos.x, 561 + pos.y, "icm006.def", SDLK_w);
  1014. bDefence = new AdventureMapButton (CGI->generaltexth->zelp[387].first, CGI->generaltexth->zelp[387].second, boost::bind(&CBattleInterface::bDefencef,this), 747 + pos.x, 561 + pos.y, "icm007.def", SDLK_d);
  1015. bDefence->assignedKeys.insert(SDLK_SPACE);
  1016. bConsoleUp = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleUpf,this), 624 + pos.x, 561 + pos.y, "ComSlide.def", SDLK_UP);
  1017. bConsoleDown = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleInterface::bConsoleDownf,this), 624 + pos.x, 580 + pos.y, "ComSlide.def", SDLK_DOWN);
  1018. bConsoleDown->bitmapOffset = 2;
  1019. console = new CBattleConsole();
  1020. console->pos.x = 211 + pos.x;
  1021. console->pos.y = 560 + pos.y;
  1022. console->pos.w = 406;
  1023. console->pos.h = 38;
  1024. //loading hero animations
  1025. if(hero1) // attacking hero
  1026. {
  1027. int type = hero1->type->heroType;
  1028. if ( type % 2 ) type--;
  1029. if ( hero1->sex ) type++;
  1030. attackingHero = new CBattleHero(graphics->battleHeroes[type], 0, 0, false, hero1->tempOwner, hero1->tempOwner == curInt->playerID ? hero1 : NULL, this);
  1031. attackingHero->pos = genRect(attackingHero->dh->ourImages[0].bitmap->h, attackingHero->dh->ourImages[0].bitmap->w, -40 + pos.x, pos.y);
  1032. }
  1033. else
  1034. {
  1035. attackingHero = NULL;
  1036. }
  1037. if(hero2) // defending hero
  1038. {
  1039. int type = hero2->type->heroType;
  1040. if ( type % 2 ) type--;
  1041. if ( hero2->sex ) type++;
  1042. defendingHero = new CBattleHero(graphics->battleHeroes[type ], 0, 0, true, hero2->tempOwner, hero2->tempOwner == curInt->playerID ? hero2 : NULL, this);
  1043. defendingHero->pos = genRect(defendingHero->dh->ourImages[0].bitmap->h, defendingHero->dh->ourImages[0].bitmap->w, 690 + pos.x, pos.y);
  1044. }
  1045. else
  1046. {
  1047. defendingHero = NULL;
  1048. }
  1049. //preparing cells and hexes
  1050. cellBorder = BitmapHandler::loadBitmap("CCELLGRD.BMP");
  1051. CSDL_Ext::alphaTransform(cellBorder);
  1052. cellShade = BitmapHandler::loadBitmap("CCELLSHD.BMP");
  1053. CSDL_Ext::alphaTransform(cellShade);
  1054. for(int h=0; h<BFIELD_SIZE; ++h)
  1055. {
  1056. bfield[h].myNumber = h;
  1057. int x = 14 + ((h/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(h%BFIELD_WIDTH);
  1058. int y = 86 + 42 * (h/BFIELD_WIDTH);
  1059. bfield[h].pos = genRect(cellShade->h, cellShade->w, x + pos.x, y + pos.y);
  1060. bfield[h].accessible = true;
  1061. bfield[h].myInterface = this;
  1062. }
  1063. //locking occupied positions on batlefield
  1064. BOOST_FOREACH(const CStack *s, stacks) //stacks gained at top of this function
  1065. if(s->position >= 0) //turrets have position < 0
  1066. bfield[s->position].accessible = false;
  1067. //loading projectiles for units
  1068. BOOST_FOREACH(const CStack *s, stacks)
  1069. {
  1070. int creID = (s->getCreature()->idNumber == 149) ? CGI->creh->factionToTurretCreature[siegeH->town->town->typeID] : s->getCreature()->idNumber; //id of creature whose shots should be loaded
  1071. if(s->getCreature()->isShooting() && vstd::contains(CGI->creh->idToProjectile, creID))
  1072. {
  1073. CDefHandler *&projectile = idToProjectile[s->getCreature()->idNumber];
  1074. projectile = CDefHandler::giveDef(CGI->creh->idToProjectile[creID]);
  1075. if(projectile->ourImages.size() > 2) //add symmetric images
  1076. {
  1077. for(int k = projectile->ourImages.size()-2; k > 1; --k)
  1078. {
  1079. Cimage ci;
  1080. ci.bitmap = CSDL_Ext::rotate01(projectile->ourImages[k].bitmap);
  1081. ci.groupNumber = 0;
  1082. ci.imName = std::string();
  1083. projectile->ourImages.push_back(ci);
  1084. }
  1085. }
  1086. for(int s=0; s<projectile->ourImages.size(); ++s) //alpha transforming
  1087. {
  1088. CSDL_Ext::alphaTransform(projectile->ourImages[s].bitmap);
  1089. }
  1090. }
  1091. }
  1092. //preparing graphic with cell borders
  1093. cellBorders = CSDL_Ext::newSurface(background->w, background->h, cellBorder);
  1094. //copying palette
  1095. for(int g=0; g<cellBorder->format->palette->ncolors; ++g) //we assume that cellBorders->format->palette->ncolors == 256
  1096. {
  1097. cellBorders->format->palette->colors[g] = cellBorder->format->palette->colors[g];
  1098. }
  1099. //palette copied
  1100. for(int i=0; i<BFIELD_HEIGHT; ++i) //rows
  1101. {
  1102. for(int j=0; j<BFIELD_WIDTH-2; ++j) //columns
  1103. {
  1104. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  1105. int y = 86 + 42 * i;
  1106. for(int cellX = 0; cellX < cellBorder->w; ++cellX)
  1107. {
  1108. for(int cellY = 0; cellY < cellBorder->h; ++cellY)
  1109. {
  1110. if(y+cellY < cellBorders->h && x+cellX < cellBorders->w)
  1111. * ((Uint8*)cellBorders->pixels + (y+cellY) * cellBorders->pitch + (x+cellX)) |= * ((Uint8*)cellBorder->pixels + cellY * cellBorder->pitch + cellX);
  1112. }
  1113. }
  1114. }
  1115. }
  1116. backgroundWithHexes = CSDL_Ext::newSurface(background->w, background->h, screen);
  1117. //preparing obstacle defs
  1118. std::vector<CObstacleInstance> obst = curInt->cb->battleGetAllObstacles();
  1119. for(int t=0; t<obst.size(); ++t)
  1120. {
  1121. idToObstacle[obst[t].ID] = CDefHandler::giveDef(CGI->heroh->obstacles.find(obst[t].ID)->second.defName);
  1122. for(int n=0; n<idToObstacle[obst[t].ID]->ourImages.size(); ++n)
  1123. {
  1124. SDL_SetColorKey(idToObstacle[obst[t].ID]->ourImages[n].bitmap, SDL_SRCCOLORKEY, SDL_MapRGB(idToObstacle[obst[t].ID]->ourImages[n].bitmap->format,0,255,255));
  1125. }
  1126. }
  1127. for (int i = 0; i < ARRAY_COUNT(bfield); i++)
  1128. {
  1129. children.push_back(&bfield[i]);
  1130. }
  1131. }
  1132. CBattleInterface::~CBattleInterface()
  1133. {
  1134. if (active) //dirty fix for #485
  1135. {
  1136. deactivate();
  1137. }
  1138. SDL_FreeSurface(background);
  1139. SDL_FreeSurface(menu);
  1140. SDL_FreeSurface(amountNormal);
  1141. SDL_FreeSurface(amountNegative);
  1142. SDL_FreeSurface(amountPositive);
  1143. SDL_FreeSurface(amountEffNeutral);
  1144. SDL_FreeSurface(cellBorders);
  1145. SDL_FreeSurface(backgroundWithHexes);
  1146. delete bOptions;
  1147. delete bSurrender;
  1148. delete bFlee;
  1149. delete bAutofight;
  1150. delete bSpell;
  1151. delete bWait;
  1152. delete bDefence;
  1153. delete bConsoleUp;
  1154. delete bConsoleDown;
  1155. delete console;
  1156. delete givenCommand;
  1157. delete attackingHero;
  1158. delete defendingHero;
  1159. delete queue;
  1160. SDL_FreeSurface(cellBorder);
  1161. SDL_FreeSurface(cellShade);
  1162. for(std::map< int, CCreatureAnimation * >::iterator g=creAnims.begin(); g!=creAnims.end(); ++g)
  1163. delete g->second;
  1164. for(std::map< int, CDefHandler * >::iterator g=idToProjectile.begin(); g!=idToProjectile.end(); ++g)
  1165. delete g->second;
  1166. for(std::map< int, CDefHandler * >::iterator g=idToObstacle.begin(); g!=idToObstacle.end(); ++g)
  1167. delete g->second;
  1168. delete siegeH;
  1169. curInt->battleInt = NULL;
  1170. }
  1171. void CBattleInterface::setPrintCellBorders(bool set)
  1172. {
  1173. curInt->sysOpts.printCellBorders = set;
  1174. curInt->sysOpts.settingsChanged();
  1175. redrawBackgroundWithHexes(activeStack);
  1176. GH.totalRedraw();
  1177. }
  1178. void CBattleInterface::setPrintStackRange(bool set)
  1179. {
  1180. curInt->sysOpts.printStackRange = set;
  1181. curInt->sysOpts.settingsChanged();
  1182. redrawBackgroundWithHexes(activeStack);
  1183. GH.totalRedraw();
  1184. }
  1185. void CBattleInterface::setPrintMouseShadow(bool set)
  1186. {
  1187. curInt->sysOpts.printMouseShadow = set;
  1188. curInt->sysOpts.settingsChanged();
  1189. }
  1190. void CBattleInterface::activate()
  1191. {
  1192. activateKeys();
  1193. activateMouseMove();
  1194. activateRClick();
  1195. bOptions->activate();
  1196. bSurrender->activate();
  1197. bFlee->activate();
  1198. bAutofight->activate();
  1199. bSpell->activate();
  1200. bWait->activate();
  1201. bDefence->activate();
  1202. bConsoleUp->activate();
  1203. bConsoleDown->activate();
  1204. for(int b=0; b<BFIELD_SIZE; ++b)
  1205. {
  1206. bfield[b].activate();
  1207. }
  1208. if(attackingHero)
  1209. attackingHero->activate();
  1210. if(defendingHero)
  1211. defendingHero->activate();
  1212. if(curInt->sysOpts.showQueue)
  1213. queue->activate();
  1214. LOCPLINT->cingconsole->activate();
  1215. }
  1216. void CBattleInterface::deactivate()
  1217. {
  1218. deactivateKeys();
  1219. deactivateMouseMove();
  1220. deactivateRClick();
  1221. bOptions->deactivate();
  1222. bSurrender->deactivate();
  1223. bFlee->deactivate();
  1224. bAutofight->deactivate();
  1225. bSpell->deactivate();
  1226. bWait->deactivate();
  1227. bDefence->deactivate();
  1228. bConsoleUp->deactivate();
  1229. bConsoleDown->deactivate();
  1230. for(int b=0; b<BFIELD_SIZE; ++b)
  1231. {
  1232. bfield[b].deactivate();
  1233. }
  1234. if(attackingHero)
  1235. attackingHero->deactivate();
  1236. if(defendingHero)
  1237. defendingHero->deactivate();
  1238. if(curInt->sysOpts.showQueue)
  1239. queue->deactivate();
  1240. LOCPLINT->cingconsole->deactivate();
  1241. }
  1242. void CBattleInterface::show(SDL_Surface * to)
  1243. {
  1244. std::vector<const CStack*> stacks = curInt->cb->battleGetStacks(false); //used in a few places
  1245. ++animCount;
  1246. if(!to) //"evaluating" to
  1247. to = screen;
  1248. SDL_Rect buf;
  1249. SDL_GetClipRect(to, &buf);
  1250. SDL_SetClipRect(to, &pos);
  1251. //printing background and hexes
  1252. if(activeStack != NULL && creAnims[activeStack->ID]->getType() != 0) //show everything with range
  1253. {
  1254. blitAt(backgroundWithHexes, pos.x, pos.y, to);
  1255. }
  1256. else
  1257. {
  1258. //showing background
  1259. blitAt(background, pos.x, pos.y, to);
  1260. if(curInt->sysOpts.printCellBorders)
  1261. {
  1262. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, to, &pos);
  1263. }
  1264. }
  1265. //printing hovered cell
  1266. for(int b=0; b<BFIELD_SIZE; ++b)
  1267. {
  1268. if(bfield[b].strictHovered && bfield[b].hovered)
  1269. {
  1270. if(previouslyHoveredHex == -1) previouslyHoveredHex = b; //something to start with
  1271. if(currentlyHoveredHex == -1) currentlyHoveredHex = b; //something to start with
  1272. if(currentlyHoveredHex != b) //repair hover info
  1273. {
  1274. previouslyHoveredHex = currentlyHoveredHex;
  1275. currentlyHoveredHex = b;
  1276. }
  1277. //print shade
  1278. if(spellToCast) //when casting spell
  1279. {
  1280. //calculating spell schoold level
  1281. const CSpell & spToCast = *CGI->spellh->spells[spellToCast->additionalInfo];
  1282. ui8 schoolLevel = 0;
  1283. if( activeStack->attackerOwned )
  1284. {
  1285. if(attackingHeroInstance)
  1286. schoolLevel = attackingHeroInstance->getSpellSchoolLevel(&spToCast);
  1287. }
  1288. else
  1289. {
  1290. if(defendingHeroInstance)
  1291. schoolLevel = defendingHeroInstance->getSpellSchoolLevel(&spToCast);
  1292. }
  1293. //obtaining range and printing it
  1294. std::set<ui16> shaded = spToCast.rangeInHexes(b, schoolLevel);
  1295. for(std::set<ui16>::iterator it = shaded.begin(); it != shaded.end(); ++it) //for spells with range greater then one hex
  1296. {
  1297. if(curInt->sysOpts.printMouseShadow && (*it % BFIELD_WIDTH != 0) && (*it % BFIELD_WIDTH != 16))
  1298. {
  1299. int x = 14 + ((*it/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(*it%BFIELD_WIDTH) + pos.x;
  1300. int y = 86 + 42 * (*it/BFIELD_WIDTH) + pos.y;
  1301. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  1302. }
  1303. }
  1304. }
  1305. else if(curInt->sysOpts.printMouseShadow) //when not casting spell
  1306. {
  1307. int x = 14 + ((b/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(b%BFIELD_WIDTH) + pos.x;
  1308. int y = 86 + 42 * (b/BFIELD_WIDTH) + pos.y;
  1309. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, to, &genRect(cellShade->h, cellShade->w, x, y));
  1310. }
  1311. }
  1312. }
  1313. SDL_SetClipRect(to, &buf); //restoring previous clip_rect
  1314. //prevents blitting outside this window
  1315. SDL_GetClipRect(to, &buf);
  1316. SDL_SetClipRect(to, &pos);
  1317. //preparing obstacles to be shown
  1318. std::vector<CObstacleInstance> obstacles = curInt->cb->battleGetAllObstacles();
  1319. std::multimap<int, int> hexToObstacle;
  1320. for(int b=0; b<obstacles.size(); ++b)
  1321. {
  1322. int position = CGI->heroh->obstacles.find(obstacles[b].ID)->second.getMaxBlocked(obstacles[b].pos);
  1323. hexToObstacle.insert(std::make_pair(position, b));
  1324. }
  1325. ////showing units //a lot of work...
  1326. std::vector<const CStack *> stackAliveByHex[BFIELD_SIZE];
  1327. //double loop because dead stacks should be printed first
  1328. BOOST_FOREACH(const CStack *s, stacks)
  1329. {
  1330. if(creAnims.find(s->ID) == creAnims.end()) //eg. for summoned but not yet handled stacks
  1331. continue;
  1332. if(creAnims[s->ID]->getType() != 5 && s->position >= 0) //don't show turrets here
  1333. stackAliveByHex[s->position].push_back(s);
  1334. }
  1335. std::vector<const CStack *> stackDeadByHex[BFIELD_SIZE];
  1336. BOOST_FOREACH(const CStack *s, stacks)
  1337. {
  1338. if(creAnims.find(s->ID) == creAnims.end()) //eg. for summoned but not yet handled stacks
  1339. continue;
  1340. if(creAnims[s->ID]->getType() == 5)
  1341. stackDeadByHex[s->position].push_back(s);
  1342. }
  1343. //handle animations
  1344. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = pendingAnims.begin(); it != pendingAnims.end(); ++it)
  1345. {
  1346. if(!it->first) //this animation should be deleted
  1347. continue;
  1348. if(!it->second)
  1349. {
  1350. it->second = it->first->init();
  1351. }
  1352. if(it->second && it->first)
  1353. it->first->nextFrame();
  1354. }
  1355. //delete anims
  1356. int preSize = pendingAnims.size();
  1357. for(std::list<std::pair<CBattleAnimation *, bool> >::iterator it = pendingAnims.begin(); it != pendingAnims.end(); ++it)
  1358. {
  1359. if(it->first == NULL)
  1360. {
  1361. pendingAnims.erase(it);
  1362. it = pendingAnims.begin();
  1363. break;
  1364. }
  1365. }
  1366. if(preSize > 0 && pendingAnims.size() == 0)
  1367. {
  1368. //action finished, restore the interface
  1369. if(!active)
  1370. activate();
  1371. //restoring good directions of stacks
  1372. BOOST_FOREACH(const CStack *s, stacks)
  1373. {
  1374. if(creDir[s->ID] != bool(s->attackerOwned) && s->alive())
  1375. {
  1376. addNewAnim(new CReverseAnim(this, s, s->position, false));
  1377. }
  1378. }
  1379. //activation of next stack
  1380. if(pendingAnims.size() == 0 && stackToActivate != NULL)
  1381. {
  1382. activateStack();
  1383. }
  1384. //anims ended
  1385. animsAreDisplayed.setn(false);
  1386. }
  1387. for(int b=0; b<BFIELD_SIZE; ++b) //showing dead stacks
  1388. {
  1389. for(size_t v=0; v<stackDeadByHex[b].size(); ++v)
  1390. {
  1391. 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
  1392. }
  1393. }
  1394. std::vector<const CStack *> flyingStacks; //flying stacks should be displayed later, over other stacks and obstacles
  1395. for(int b=0; b<BFIELD_SIZE; ++b) //showing alive stacks
  1396. {
  1397. for(size_t v=0; v<stackAliveByHex[b].size(); ++v)
  1398. {
  1399. const CStack *s = stackAliveByHex[b][v];
  1400. if(!s->hasBonusOfType(Bonus::FLYING) || creAnims[s->ID]->getType() != 0)
  1401. showAliveStack(s, to);
  1402. else
  1403. flyingStacks.push_back(s);
  1404. }
  1405. //showing obstacles
  1406. std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> obstRange =
  1407. hexToObstacle.equal_range(b);
  1408. for(std::multimap<int, int>::const_iterator it = obstRange.first; it != obstRange.second; ++it)
  1409. {
  1410. CObstacleInstance & curOb = obstacles[it->second];
  1411. std::pair<si16, si16> shift = CGI->heroh->obstacles.find(curOb.ID)->second.posShift;
  1412. int x = ((curOb.pos/BFIELD_WIDTH)%2==0 ? 22 : 0) + 44*(curOb.pos%BFIELD_WIDTH) + pos.x + shift.first;
  1413. int y = 86 + 42 * (curOb.pos/BFIELD_WIDTH) + pos.y + shift.second;
  1414. std::vector<Cimage> &images = idToObstacle[curOb.ID]->ourImages; //reference to animation of obstacle
  1415. blitAt(images[((animCount+1)/(4/curInt->sysOpts.animSpeed))%images.size()].bitmap, x, y, to);
  1416. }
  1417. //showing wall pieces
  1418. showPieceOfWall(to, b, stacks);
  1419. }
  1420. for(int b=0; b<flyingStacks.size(); ++b) //showing flying stacks
  1421. showAliveStack(flyingStacks[b], to);
  1422. //units shown
  1423. //showing hero animations
  1424. if(attackingHero)
  1425. attackingHero->show(to);
  1426. if(defendingHero)
  1427. defendingHero->show(to);
  1428. projectileShowHelper(to);//showing projectiles
  1429. //showing spell effects
  1430. if(battleEffects.size())
  1431. {
  1432. for(std::list<SBattleEffect>::iterator it = battleEffects.begin(); it!=battleEffects.end(); ++it)
  1433. {
  1434. SDL_Surface * bitmapToBlit = it->anim->ourImages[(it->frame)%it->anim->ourImages.size()].bitmap;
  1435. SDL_BlitSurface(bitmapToBlit, NULL, to, &genRect(bitmapToBlit->h, bitmapToBlit->w, it->x, it->y));
  1436. }
  1437. }
  1438. SDL_SetClipRect(to, &buf); //restoring previous clip_rect
  1439. //showing menu background and console
  1440. blitAt(menu, pos.x, 556 + pos.y, to);
  1441. console->show(to);
  1442. //showing buttons
  1443. bOptions->show(to);
  1444. bSurrender->show(to);
  1445. bFlee->show(to);
  1446. bAutofight->show(to);
  1447. bSpell->show(to);
  1448. bWait->show(to);
  1449. bDefence->show(to);
  1450. bConsoleUp->show(to);
  1451. bConsoleDown->show(to);
  1452. //showing window with result of battle
  1453. if(resWindow)
  1454. {
  1455. resWindow->show(to);
  1456. }
  1457. //showing in-game console
  1458. LOCPLINT->cingconsole->show(to);
  1459. Rect posWithQueue = Rect(pos.x, pos.y, 800, 600);
  1460. if(curInt->sysOpts.showQueue)
  1461. {
  1462. if(!queue->embedded)
  1463. {
  1464. posWithQueue.y -= queue->pos.h;
  1465. posWithQueue.h += queue->pos.h;
  1466. }
  1467. //showing queue
  1468. if(!bresult)
  1469. queue->showAll(to);
  1470. else
  1471. queue->blitBg(to); //blit only background, stacks are deleted
  1472. }
  1473. //printing border around interface
  1474. if(screen->w != 800 || screen->h !=600)
  1475. {
  1476. CMessage::drawBorder(curInt->playerID,to,posWithQueue.w + 28, posWithQueue.h + 28, posWithQueue.x-14, posWithQueue.y-15);
  1477. }
  1478. }
  1479. void CBattleInterface::keyPressed(const SDL_KeyboardEvent & key)
  1480. {
  1481. if(key.keysym.sym == SDLK_q && key.state == SDL_PRESSED)
  1482. {
  1483. if(curInt->sysOpts.showQueue) //hide queue
  1484. hideQueue();
  1485. else
  1486. showQueue();
  1487. curInt->sysOpts.settingsChanged();
  1488. }
  1489. else if(key.keysym.sym == SDLK_ESCAPE && spellDestSelectMode)
  1490. {
  1491. endCastingSpell();
  1492. }
  1493. }
  1494. void CBattleInterface::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  1495. {
  1496. if(activeStack!= NULL && !spellDestSelectMode)
  1497. {
  1498. mouseHoveredStack = -1;
  1499. int myNumber = -1; //number of hovered tile
  1500. for(int g=0; g<BFIELD_SIZE; ++g)
  1501. {
  1502. if(bfield[g].hovered && bfield[g].strictHovered)
  1503. {
  1504. myNumber = g;
  1505. break;
  1506. }
  1507. }
  1508. if(myNumber == -1)
  1509. {
  1510. CCS->curh->changeGraphic(1, 6);
  1511. if(console->whoSetAlter == 0)
  1512. {
  1513. console->alterTxt = "";
  1514. }
  1515. }
  1516. else
  1517. {
  1518. if(std::find(shadedHexes.begin(),shadedHexes.end(),myNumber) == shadedHexes.end())
  1519. {
  1520. const CStack *shere = curInt->cb->battleGetStackByPos(myNumber);
  1521. const CStack *sactive = activeStack;
  1522. if(shere)
  1523. {
  1524. if(shere->owner == curInt->playerID) //our stack
  1525. {
  1526. if(sactive->hasBonusOfType(Bonus::HEALER))
  1527. {
  1528. //display the possibility to heal this creature
  1529. CCS->curh->changeGraphic(1,17);
  1530. }
  1531. else
  1532. {
  1533. //info about creature
  1534. CCS->curh->changeGraphic(1,5);
  1535. }
  1536. //setting console text
  1537. char buf[500];
  1538. sprintf(buf, CGI->generaltexth->allTexts[297].c_str(), shere->count == 1 ? shere->getCreature()->nameSing.c_str() : shere->getCreature()->namePl.c_str());
  1539. console->alterTxt = buf;
  1540. console->whoSetAlter = 0;
  1541. mouseHoveredStack = shere->ID;
  1542. if(creAnims[shere->ID]->getType() == 2 && creAnims[shere->ID]->framesInGroup(1) > 0)
  1543. {
  1544. creAnims[shere->ID]->playOnce(1);
  1545. }
  1546. }
  1547. else if(curInt->cb->battleCanShoot(activeStack,myNumber)) //we can shoot enemy
  1548. {
  1549. if(curInt->cb->battleHasDistancePenalty(activeStack, myNumber) ||
  1550. curInt->cb->battleHasWallPenalty(activeStack, myNumber))
  1551. {
  1552. CCS->curh->changeGraphic(1,15);
  1553. }
  1554. else
  1555. {
  1556. CCS->curh->changeGraphic(1,3);
  1557. }
  1558. //setting console text
  1559. char buf[500];
  1560. //calculating estimated dmg
  1561. std::pair<ui32, ui32> estimatedDmg = curInt->cb->battleEstimateDamage(sactive, shere);
  1562. std::ostringstream estDmg;
  1563. estDmg << estimatedDmg.first << " - " << estimatedDmg.second;
  1564. //printing
  1565. sprintf(buf, CGI->generaltexth->allTexts[296].c_str(), shere->count == 1 ? shere->getCreature()->nameSing.c_str() : shere->getCreature()->namePl.c_str(), sactive->shots, estDmg.str().c_str());
  1566. console->alterTxt = buf;
  1567. console->whoSetAlter = 0;
  1568. }
  1569. else if(isTileAttackable(myNumber)) //available enemy (melee attackable)
  1570. {
  1571. CCursorHandler *cursor = CCS->curh;
  1572. const CBattleHex &hoveredHex = bfield[myNumber];
  1573. const double subdividingAngle = 2.0*M_PI/6.0; // Divide a hex into six sectors.
  1574. const double hexMidX = hoveredHex.pos.x + hoveredHex.pos.w/2;
  1575. const double hexMidY = hoveredHex.pos.y + hoveredHex.pos.h/2;
  1576. const double cursorHexAngle = M_PI - atan2(hexMidY - cursor->ypos, cursor->xpos - hexMidX) + subdividingAngle/2;
  1577. const double sector = fmod(cursorHexAngle/subdividingAngle, 6.0);
  1578. const int zigzagCorrection = !((myNumber/BFIELD_WIDTH)%2); // Off-by-one correction needed to deal with the odd battlefield rows.
  1579. std::vector<int> sectorCursor; // From left to bottom left.
  1580. sectorCursor.push_back(8);
  1581. sectorCursor.push_back(9);
  1582. sectorCursor.push_back(10);
  1583. sectorCursor.push_back(11);
  1584. sectorCursor.push_back(12);
  1585. sectorCursor.push_back(7);
  1586. const bool doubleWide = activeStack->doubleWide();
  1587. bool aboveAttackable = true, belowAttackable = true;
  1588. // Exclude directions which cannot be attacked from.
  1589. // Check to the left.
  1590. if (myNumber%BFIELD_WIDTH <= 1 || !vstd::contains(shadedHexes, myNumber - 1))
  1591. {
  1592. sectorCursor[0] = -1;
  1593. }
  1594. // Check top left, top right as well as above for 2-hex creatures.
  1595. if (myNumber/BFIELD_WIDTH == 0)
  1596. {
  1597. sectorCursor[1] = -1;
  1598. sectorCursor[2] = -1;
  1599. aboveAttackable = false;
  1600. }
  1601. else
  1602. {
  1603. if (doubleWide)
  1604. {
  1605. bool attackRow[4] = {true, true, true, true};
  1606. if (myNumber%BFIELD_WIDTH <= 1 || !vstd::contains(shadedHexes, myNumber - BFIELD_WIDTH - 2 + zigzagCorrection))
  1607. attackRow[0] = false;
  1608. if (!vstd::contains(shadedHexes, myNumber - BFIELD_WIDTH - 1 + zigzagCorrection))
  1609. attackRow[1] = false;
  1610. if (!vstd::contains(shadedHexes, myNumber - BFIELD_WIDTH + zigzagCorrection))
  1611. attackRow[2] = false;
  1612. if (myNumber%BFIELD_WIDTH >= BFIELD_WIDTH - 2 || !vstd::contains(shadedHexes, myNumber - BFIELD_WIDTH + 1 + zigzagCorrection))
  1613. attackRow[3] = false;
  1614. if (!(attackRow[0] && attackRow[1]))
  1615. sectorCursor[1] = -1;
  1616. if (!(attackRow[1] && attackRow[2]))
  1617. aboveAttackable = false;
  1618. if (!(attackRow[2] && attackRow[3]))
  1619. sectorCursor[2] = -1;
  1620. }
  1621. else
  1622. {
  1623. if (!vstd::contains(shadedHexes, myNumber - BFIELD_WIDTH - 1 + zigzagCorrection))
  1624. sectorCursor[1] = -1;
  1625. if (!vstd::contains(shadedHexes, myNumber - BFIELD_WIDTH + zigzagCorrection))
  1626. sectorCursor[2] = -1;
  1627. }
  1628. }
  1629. // Check to the right.
  1630. if (myNumber%BFIELD_WIDTH >= BFIELD_WIDTH - 2 || !vstd::contains(shadedHexes, myNumber + 1))
  1631. {
  1632. sectorCursor[3] = -1;
  1633. }
  1634. // Check bottom right, bottom left as well as below for 2-hex creatures.
  1635. if (myNumber/BFIELD_WIDTH == BFIELD_HEIGHT - 1)
  1636. {
  1637. sectorCursor[4] = -1;
  1638. sectorCursor[5] = -1;
  1639. belowAttackable = false;
  1640. }
  1641. else
  1642. {
  1643. if (doubleWide)
  1644. {
  1645. bool attackRow[4] = {true, true, true, true};
  1646. if (myNumber%BFIELD_WIDTH <= 1 || !vstd::contains(shadedHexes, myNumber + BFIELD_WIDTH - 2 + zigzagCorrection))
  1647. attackRow[0] = false;
  1648. if (!vstd::contains(shadedHexes, myNumber + BFIELD_WIDTH - 1 + zigzagCorrection))
  1649. attackRow[1] = false;
  1650. if (!vstd::contains(shadedHexes, myNumber + BFIELD_WIDTH + zigzagCorrection))
  1651. attackRow[2] = false;
  1652. if (myNumber%BFIELD_WIDTH >= BFIELD_WIDTH - 2 || !vstd::contains(shadedHexes, myNumber + BFIELD_WIDTH + 1 + zigzagCorrection))
  1653. attackRow[3] = false;
  1654. if (!(attackRow[0] && attackRow[1]))
  1655. sectorCursor[5] = -1;
  1656. if (!(attackRow[1] && attackRow[2]))
  1657. belowAttackable = false;
  1658. if (!(attackRow[2] && attackRow[3]))
  1659. sectorCursor[4] = -1;
  1660. }
  1661. else
  1662. {
  1663. if (!vstd::contains(shadedHexes, myNumber + BFIELD_WIDTH + zigzagCorrection))
  1664. sectorCursor[4] = -1;
  1665. if (!vstd::contains(shadedHexes, myNumber + BFIELD_WIDTH - 1 + zigzagCorrection))
  1666. sectorCursor[5] = -1;
  1667. }
  1668. }
  1669. // Determine index from sector.
  1670. int cursorIndex;
  1671. if (doubleWide)
  1672. {
  1673. sectorCursor.insert(sectorCursor.begin() + 5, belowAttackable ? 13 : -1);
  1674. sectorCursor.insert(sectorCursor.begin() + 2, aboveAttackable ? 14 : -1);
  1675. if (sector < 1.5)
  1676. cursorIndex = sector;
  1677. else if (sector >= 1.5 && sector < 2.5)
  1678. cursorIndex = 2;
  1679. else if (sector >= 2.5 && sector < 4.5)
  1680. cursorIndex = (int) sector + 1;
  1681. else if (sector >= 4.5 && sector < 5.5)
  1682. cursorIndex = 6;
  1683. else
  1684. cursorIndex = (int) sector + 2;
  1685. }
  1686. else
  1687. {
  1688. cursorIndex = sector;
  1689. }
  1690. // Find the closest direction attackable, starting with the right one.
  1691. // FIXME: Is this really how the original H3 client does it?
  1692. int i = 0;
  1693. while (sectorCursor[(cursorIndex + i)%sectorCursor.size()] == -1)
  1694. i = i <= 0 ? 1 - i : -i; // 0, 1, -1, 2, -2, 3, -3 etc..
  1695. cursor->changeGraphic(1, sectorCursor[(cursorIndex + i)%sectorCursor.size()]);
  1696. //setting console info
  1697. char buf[500];
  1698. //calculating estimated dmg
  1699. std::pair<ui32, ui32> estimatedDmg = curInt->cb->battleEstimateDamage(sactive, shere);
  1700. std::ostringstream estDmg;
  1701. estDmg << estimatedDmg.first << " - " << estimatedDmg.second;
  1702. //printing
  1703. sprintf(buf, CGI->generaltexth->allTexts[36].c_str(), shere->count == 1 ? shere->getCreature()->nameSing.c_str() : shere->getCreature()->namePl.c_str(), estDmg.str().c_str());
  1704. console->alterTxt = buf;
  1705. console->whoSetAlter = 0;
  1706. }
  1707. else //unavailable enemy
  1708. {
  1709. CCS->curh->changeGraphic(1,0);
  1710. console->alterTxt = "";
  1711. console->whoSetAlter = 0;
  1712. }
  1713. }
  1714. else if( sactive && sactive->hasBonusOfType(Bonus::CATAPULT) && isCatapultAttackable(myNumber) ) //catapulting
  1715. {
  1716. CCS->curh->changeGraphic(1,16);
  1717. console->alterTxt = "";
  1718. console->whoSetAlter = 0;
  1719. }
  1720. else //empty unavailable tile
  1721. {
  1722. CCS->curh->changeGraphic(1,0);
  1723. console->alterTxt = "";
  1724. console->whoSetAlter = 0;
  1725. }
  1726. }
  1727. else //available tile
  1728. {
  1729. //setting console text and cursor
  1730. const CStack *sactive = activeStack;
  1731. if(sactive) //there can be a moment when stack is dead ut next is not yet activated
  1732. {
  1733. char buf[500];
  1734. if(sactive->hasBonusOfType(Bonus::FLYING))
  1735. {
  1736. CCS->curh->changeGraphic(1,2);
  1737. sprintf(buf, CGI->generaltexth->allTexts[295].c_str(), sactive->count == 1 ? sactive->getCreature()->nameSing.c_str() : sactive->getCreature()->namePl.c_str());
  1738. }
  1739. else
  1740. {
  1741. CCS->curh->changeGraphic(1,1);
  1742. sprintf(buf, CGI->generaltexth->allTexts[294].c_str(), sactive->count == 1 ? sactive->getCreature()->nameSing.c_str() : sactive->getCreature()->namePl.c_str());
  1743. }
  1744. console->alterTxt = buf;
  1745. console->whoSetAlter = 0;
  1746. }
  1747. }
  1748. }
  1749. }
  1750. else if(spellDestSelectMode)
  1751. {
  1752. int myNumber = -1; //number of hovered tile
  1753. for(int g=0; g<BFIELD_SIZE; ++g)
  1754. {
  1755. if(bfield[g].hovered && bfield[g].strictHovered)
  1756. {
  1757. myNumber = g;
  1758. break;
  1759. }
  1760. }
  1761. if(myNumber == -1)
  1762. {
  1763. CCS->curh->changeGraphic(1, 0);
  1764. //setting console text
  1765. console->alterTxt = CGI->generaltexth->allTexts[23];
  1766. console->whoSetAlter = 0;
  1767. }
  1768. else
  1769. {
  1770. //get dead stack if we cast resurrection or animate dead
  1771. const CStack * stackUnder = curInt->cb->battleGetStackByPos(myNumber, spellToCast->additionalInfo != 38 && spellToCast->additionalInfo != 39);
  1772. if(stackUnder && spellToCast->additionalInfo == 39 && !stackUnder->hasBonusOfType(Bonus::UNDEAD)) //animate dead can be cast only on undead creatures
  1773. stackUnder = NULL;
  1774. bool whichCase; //for cases 1, 2 and 3
  1775. switch(spellSelMode)
  1776. {
  1777. case 1:
  1778. whichCase = stackUnder && curInt->playerID == stackUnder->owner;
  1779. break;
  1780. case 2:
  1781. whichCase = stackUnder && curInt->playerID != stackUnder->owner;
  1782. break;
  1783. case 3:
  1784. whichCase = stackUnder;
  1785. break;
  1786. }
  1787. switch(spellSelMode)
  1788. {
  1789. case 0:
  1790. CCS->curh->changeGraphic(3, 0);
  1791. //setting console text
  1792. char buf[500];
  1793. sprintf(buf, CGI->generaltexth->allTexts[26].c_str(), CGI->spellh->spells[spellToCast->additionalInfo]->name.c_str());
  1794. console->alterTxt = buf;
  1795. console->whoSetAlter = 0;
  1796. break;
  1797. case 1: case 2: case 3:
  1798. if( whichCase )
  1799. {
  1800. CCS->curh->changeGraphic(3, 0);
  1801. //setting console text
  1802. char buf[500];
  1803. std::string creName = stackUnder->count > 1 ? stackUnder->getCreature()->namePl : stackUnder->getCreature()->nameSing;
  1804. sprintf(buf, CGI->generaltexth->allTexts[27].c_str(), CGI->spellh->spells[spellToCast->additionalInfo]->name.c_str(), creName.c_str());
  1805. console->alterTxt = buf;
  1806. console->whoSetAlter = 0;
  1807. break;
  1808. }
  1809. else
  1810. {
  1811. CCS->curh->changeGraphic(1, 0);
  1812. //setting console text
  1813. console->alterTxt = CGI->generaltexth->allTexts[23];
  1814. console->whoSetAlter = 0;
  1815. }
  1816. break;
  1817. case 4: //TODO: implement this case
  1818. if( blockedByObstacle(myNumber) )
  1819. {
  1820. CCS->curh->changeGraphic(3, 0);
  1821. }
  1822. else
  1823. {
  1824. CCS->curh->changeGraphic(1, 0);
  1825. }
  1826. break;
  1827. }
  1828. }
  1829. }
  1830. }
  1831. void CBattleInterface::clickRight(tribool down, bool previousState)
  1832. {
  1833. if(!down && spellDestSelectMode)
  1834. {
  1835. endCastingSpell();
  1836. }
  1837. }
  1838. void CBattleInterface::bOptionsf()
  1839. {
  1840. if(spellDestSelectMode) //we are casting a spell
  1841. return;
  1842. CCS->curh->changeGraphic(0,0);
  1843. SDL_Rect temp_rect = genRect(431, 481, 160, 84);
  1844. CBattleOptionsWindow * optionsWin = new CBattleOptionsWindow(temp_rect, this);
  1845. GH.pushInt(optionsWin);
  1846. }
  1847. void CBattleInterface::bSurrenderf()
  1848. {
  1849. if(spellDestSelectMode) //we are casting a spell
  1850. return;
  1851. }
  1852. void CBattleInterface::bFleef()
  1853. {
  1854. if(spellDestSelectMode) //we are casting a spell
  1855. return;
  1856. if( curInt->cb->battleCanFlee() )
  1857. {
  1858. CFunctionList<void()> ony = boost::bind(&CBattleInterface::reallyFlee,this);
  1859. curInt->showYesNoDialog(CGI->generaltexth->allTexts[28],std::vector<SComponent*>(), ony, 0, false);
  1860. }
  1861. else
  1862. {
  1863. std::vector<SComponent*> comps;
  1864. std::string heroName;
  1865. //calculating fleeing hero's name
  1866. if(attackingHeroInstance)
  1867. if(attackingHeroInstance->tempOwner == curInt->cb->getMyColor())
  1868. heroName = attackingHeroInstance->name;
  1869. if(defendingHeroInstance)
  1870. if(defendingHeroInstance->tempOwner == curInt->cb->getMyColor())
  1871. heroName = defendingHeroInstance->name;
  1872. //calculating text
  1873. char buffer[1000];
  1874. sprintf(buffer, CGI->generaltexth->allTexts[340].c_str(), heroName.c_str());
  1875. //printing message
  1876. curInt->showInfoDialog(std::string(buffer), comps);
  1877. }
  1878. }
  1879. void CBattleInterface::reallyFlee()
  1880. {
  1881. giveCommand(4,0,0);
  1882. CCS->curh->changeGraphic(0, 0);
  1883. }
  1884. void CBattleInterface::bAutofightf()
  1885. {
  1886. if(spellDestSelectMode) //we are casting a spell
  1887. return;
  1888. }
  1889. void CBattleInterface::bSpellf()
  1890. {
  1891. if(spellDestSelectMode) //we are casting a spell
  1892. return;
  1893. CCS->curh->changeGraphic(0,0);
  1894. const CGHeroInstance * chi = NULL;
  1895. if(attackingHeroInstance->tempOwner == curInt->playerID)
  1896. chi = attackingHeroInstance;
  1897. else
  1898. chi = defendingHeroInstance;
  1899. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), chi, curInt);
  1900. GH.pushInt(spellWindow);
  1901. }
  1902. void CBattleInterface::bWaitf()
  1903. {
  1904. if(spellDestSelectMode) //we are casting a spell
  1905. return;
  1906. if(activeStack != NULL)
  1907. giveCommand(8,0,activeStack->ID);
  1908. }
  1909. void CBattleInterface::bDefencef()
  1910. {
  1911. if(spellDestSelectMode) //we are casting a spell
  1912. return;
  1913. if(activeStack != NULL)
  1914. giveCommand(3,0,activeStack->ID);
  1915. }
  1916. void CBattleInterface::bConsoleUpf()
  1917. {
  1918. if(spellDestSelectMode) //we are casting a spell
  1919. return;
  1920. console->scrollUp();
  1921. }
  1922. void CBattleInterface::bConsoleDownf()
  1923. {
  1924. if(spellDestSelectMode) //we are casting a spell
  1925. return;
  1926. console->scrollDown();
  1927. }
  1928. void CBattleInterface::newStack(const CStack * stack)
  1929. {
  1930. Point coords = CBattleHex::getXYUnitAnim(stack->position, stack->owner == attackingHeroInstance->tempOwner, stack, this);;
  1931. if(stack->position < 0) //turret
  1932. {
  1933. const CCreature & turretCreature = *CGI->creh->creatures[ CGI->creh->factionToTurretCreature[siegeH->town->town->typeID] ];
  1934. creAnims[stack->ID] = new CCreatureAnimation(turretCreature.animDefName);
  1935. }
  1936. else
  1937. {
  1938. creAnims[stack->ID] = new CCreatureAnimation(stack->getCreature()->animDefName);
  1939. }
  1940. creAnims[stack->ID]->setType(2);
  1941. creAnims[stack->ID]->pos = Rect(coords.x, coords.y, creAnims[stack->ID]->fullWidth, creAnims[stack->ID]->fullHeight);
  1942. creDir[stack->ID] = stack->attackerOwned;
  1943. }
  1944. void CBattleInterface::stackRemoved(const CStack * stack)
  1945. {
  1946. int stackID = stack->ID;
  1947. delete creAnims[stackID];
  1948. creAnims.erase(stackID);
  1949. creDir.erase(stackID);
  1950. }
  1951. void CBattleInterface::stackActivated(const CStack * stack)
  1952. {
  1953. //givenCommand = NULL;
  1954. stackToActivate = stack;
  1955. if(pendingAnims.size() == 0)
  1956. activateStack();
  1957. }
  1958. void CBattleInterface::stackMoved(const CStack * stack, THex destHex, bool endMoving, int distance)
  1959. {
  1960. addNewAnim(new CBattleStackMoved(this, stack, destHex, endMoving, distance));
  1961. waitForAnims();
  1962. }
  1963. void CBattleInterface::stacksAreAttacked(std::vector<SStackAttackedInfo> attackedInfos)
  1964. {
  1965. for(int h = 0; h < attackedInfos.size(); ++h)
  1966. {
  1967. addNewAnim(new CDefenceAnim(attackedInfos[h], this));
  1968. }
  1969. waitForAnims();
  1970. }
  1971. void CBattleInterface::stackAttacking( const CStack * attacker, THex dest, const CStack * attacked, bool shooting )
  1972. {
  1973. if (shooting)
  1974. {
  1975. addNewAnim(new CShootingAnim(this, attacker, dest, attacked));
  1976. }
  1977. else
  1978. {
  1979. addNewAnim(new CMeleeAttack(this, attacker, dest, attacked));
  1980. }
  1981. waitForAnims();
  1982. }
  1983. void CBattleInterface::newRoundFirst( int round )
  1984. {
  1985. //handle regeneration
  1986. std::vector<const CStack*> stacks = curInt->cb->battleGetStacks(false);
  1987. BOOST_FOREACH(const CStack *s, stacks)
  1988. {
  1989. //don't show animation when no HP is regenerated
  1990. if (s->firstHPleft == s->MaxHealth())
  1991. {
  1992. continue;
  1993. }
  1994. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  1995. displayEffect(74, s->position);
  1996. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION, 0) && s->alive() )
  1997. displayEffect(4, s->position);
  1998. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION, 1) && s->alive() )
  1999. displayEffect(74, s->position);
  2000. }
  2001. waitForAnims();
  2002. }
  2003. void CBattleInterface::newRound(int number)
  2004. {
  2005. console->addText(CGI->generaltexth->allTexts[412]);
  2006. //unlock spellbook
  2007. //bSpell->block(!curInt->cb->battleCanCastSpell());
  2008. //don't unlock spellbook - this should be done when we have axctive creature
  2009. }
  2010. void CBattleInterface::giveCommand(ui8 action, ui16 tile, ui32 stack, si32 additional)
  2011. {
  2012. if(!curInt->cb->battleGetStackByID(stack) && action != 1 && action != 4 && action != 5)
  2013. {
  2014. return;
  2015. }
  2016. BattleAction * ba = new BattleAction(); //is deleted in CPlayerInterface::activeStack()
  2017. ba->side = defendingHeroInstance ? (curInt->playerID == defendingHeroInstance->tempOwner) : false;
  2018. ba->actionType = action;
  2019. ba->destinationTile = tile;
  2020. ba->stackNumber = stack;
  2021. ba->additionalInfo = additional;
  2022. //some basic validations
  2023. switch(action)
  2024. {
  2025. case 6:
  2026. assert(curInt->cb->battleGetStackByPos(additional)); //stack to attack must exist
  2027. case 2: case 7: case 9:
  2028. assert(tile < BFIELD_SIZE);
  2029. break;
  2030. }
  2031. myTurn = false;
  2032. activeStack = NULL;
  2033. givenCommand->setn(ba);
  2034. }
  2035. bool CBattleInterface::isTileAttackable(const int & number) const
  2036. {
  2037. for(size_t b=0; b<shadedHexes.size(); ++b)
  2038. {
  2039. if(THex::mutualPosition(shadedHexes[b], number) != -1 || shadedHexes[b] == number)
  2040. return true;
  2041. }
  2042. return false;
  2043. }
  2044. bool CBattleInterface::blockedByObstacle(int hex) const
  2045. {
  2046. std::vector<CObstacleInstance> obstacles = curInt->cb->battleGetAllObstacles();
  2047. std::set<int> coveredHexes;
  2048. for(int b = 0; b < obstacles.size(); ++b)
  2049. {
  2050. std::vector<int> blocked = CGI->heroh->obstacles.find(obstacles[b].ID)->second.getBlocked(obstacles[b].pos);
  2051. for(int w = 0; w < blocked.size(); ++w)
  2052. coveredHexes.insert(blocked[w]);
  2053. }
  2054. return vstd::contains(coveredHexes, hex);
  2055. }
  2056. bool CBattleInterface::isCatapultAttackable(int hex) const
  2057. {
  2058. if(!siegeH)
  2059. return false;
  2060. int wallUnder = curInt->cb->battleGetWallUnderHex(hex);
  2061. if(wallUnder == -1)
  2062. return false;
  2063. return curInt->cb->battleGetWallState(wallUnder) < 3;
  2064. }
  2065. const CGHeroInstance * CBattleInterface::getActiveHero()
  2066. {
  2067. const CStack * attacker = activeStack;
  2068. if (!attacker)
  2069. {
  2070. return NULL;
  2071. }
  2072. if (attacker->attackerOwned)
  2073. {
  2074. return attackingHeroInstance;
  2075. }
  2076. return defendingHeroInstance;
  2077. }
  2078. void CBattleInterface::hexLclicked(int whichOne)
  2079. {
  2080. const CStack * actSt = activeStack;
  2081. if( ((whichOne%BFIELD_WIDTH)!=0 && (whichOne%BFIELD_WIDTH)!=(BFIELD_WIDTH-1)) //if player is trying to attack enemey unit or move creature stack
  2082. || (actSt->hasBonusOfType(Bonus::CATAPULT) && !spellDestSelectMode )
  2083. )
  2084. {
  2085. if(!myTurn)
  2086. return; //we are not permit to do anything
  2087. if(spellDestSelectMode)
  2088. {
  2089. //checking destination
  2090. bool allowCasting = true;
  2091. bool onlyAlive = spellToCast->additionalInfo != 38 && spellToCast->additionalInfo != 39; //when casting resurrection or animate dead we should be allow to select dead stack
  2092. switch(spellSelMode)
  2093. {
  2094. case 1:
  2095. if(!curInt->cb->battleGetStackByPos(whichOne, onlyAlive) || curInt->playerID != curInt->cb->battleGetStackByPos(whichOne, onlyAlive)->owner )
  2096. allowCasting = false;
  2097. break;
  2098. case 2:
  2099. if(!curInt->cb->battleGetStackByPos(whichOne, onlyAlive) || curInt->playerID == curInt->cb->battleGetStackByPos(whichOne, onlyAlive)->owner )
  2100. allowCasting = false;
  2101. break;
  2102. case 3:
  2103. if(!curInt->cb->battleGetStackByPos(whichOne, onlyAlive))
  2104. allowCasting = false;
  2105. break;
  2106. case 4:
  2107. if(!blockedByObstacle(whichOne))
  2108. allowCasting = false;
  2109. case 5: //teleport
  2110. const CSpell *s = CGI->spellh->spells[spellToCast->additionalInfo];
  2111. ui8 skill = getActiveHero()->getSpellSchoolLevel(s); //skill level
  2112. if (!curInt->cb->battleCanTeleportTo(activeStack, whichOne, skill))
  2113. {
  2114. allowCasting = false;
  2115. }
  2116. break;
  2117. }
  2118. //destination checked
  2119. if(allowCasting)
  2120. {
  2121. spellToCast->destinationTile = whichOne;
  2122. curInt->cb->battleMakeAction(spellToCast);
  2123. endCastingSpell();
  2124. }
  2125. }
  2126. else //we don't cast any spell
  2127. {
  2128. const CStack* dest = curInt->cb->battleGetStackByPos(whichOne); //creature at destination tile; -1 if there is no one
  2129. if(!dest || !dest->alive()) //no creature at that tile
  2130. {
  2131. if(std::find(shadedHexes.begin(),shadedHexes.end(),whichOne)!=shadedHexes.end())// and it's in our range
  2132. {
  2133. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  2134. if(activeStack->doubleWide())
  2135. {
  2136. std::vector<THex> acc = curInt->cb->battleGetAvailableHexes(activeStack, false);
  2137. int shiftedDest = whichOne + (activeStack->attackerOwned ? 1 : -1);
  2138. if(vstd::contains(acc, whichOne))
  2139. giveCommand(2,whichOne,activeStack->ID);
  2140. else if(vstd::contains(acc, shiftedDest))
  2141. giveCommand(2,shiftedDest,activeStack->ID);
  2142. }
  2143. else
  2144. {
  2145. giveCommand(2,whichOne,activeStack->ID);
  2146. }
  2147. }
  2148. else if(actSt->hasBonusOfType(Bonus::CATAPULT) && isCatapultAttackable(whichOne)) //attacking (catapult)
  2149. {
  2150. giveCommand(9,whichOne,activeStack->ID);
  2151. }
  2152. }
  2153. else if(dest->owner != actSt->owner
  2154. && curInt->cb->battleCanShoot(activeStack, whichOne) ) //shooting
  2155. {
  2156. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  2157. giveCommand(7,whichOne,activeStack->ID);
  2158. }
  2159. else if(dest->owner != actSt->owner) //attacking
  2160. {
  2161. const CStack * actStack = activeStack;
  2162. int attackFromHex = -1; //hex from which we will attack chosen stack
  2163. switch(CCS->curh->number)
  2164. {
  2165. case 12: //from bottom right
  2166. {
  2167. bool doubleWide = actStack->doubleWide();
  2168. int destHex = whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH+1 ) +
  2169. (actStack->attackerOwned && doubleWide ? 1 : 0);
  2170. if(vstd::contains(shadedHexes, destHex))
  2171. attackFromHex = destHex;
  2172. else if(actStack->attackerOwned) //if we are attacker
  2173. {
  2174. if(vstd::contains(shadedHexes, destHex+1))
  2175. attackFromHex = destHex+1;
  2176. }
  2177. else //if we are defender
  2178. {
  2179. if(vstd::contains(shadedHexes, destHex-1))
  2180. attackFromHex = destHex-1;
  2181. }
  2182. break;
  2183. }
  2184. case 7: //from bottom left
  2185. {
  2186. int destHex = whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH-1 : BFIELD_WIDTH );
  2187. if(vstd::contains(shadedHexes, destHex))
  2188. attackFromHex = destHex;
  2189. else if(actStack->attackerOwned) //if we are attacker
  2190. {
  2191. if(vstd::contains(shadedHexes, destHex+1))
  2192. attackFromHex = destHex+1;
  2193. }
  2194. else //if we are defender
  2195. {
  2196. if(vstd::contains(shadedHexes, destHex-1))
  2197. attackFromHex = destHex-1;
  2198. }
  2199. break;
  2200. }
  2201. case 8: //from left
  2202. {
  2203. if(actStack->doubleWide() && !actStack->attackerOwned)
  2204. {
  2205. std::vector<THex> acc = curInt->cb->battleGetAvailableHexes(activeStack, false);
  2206. if(vstd::contains(acc, whichOne))
  2207. attackFromHex = whichOne - 1;
  2208. else
  2209. attackFromHex = whichOne - 2;
  2210. }
  2211. else
  2212. {
  2213. attackFromHex = whichOne - 1;
  2214. }
  2215. break;
  2216. }
  2217. case 9: //from top left
  2218. {
  2219. int destHex = whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH+1 : BFIELD_WIDTH );
  2220. if(vstd::contains(shadedHexes, destHex))
  2221. attackFromHex = destHex;
  2222. else if(actStack->attackerOwned) //if we are attacker
  2223. {
  2224. if(vstd::contains(shadedHexes, destHex+1))
  2225. attackFromHex = destHex+1;
  2226. }
  2227. else //if we are defender
  2228. {
  2229. if(vstd::contains(shadedHexes, destHex-1))
  2230. attackFromHex = destHex-1;
  2231. }
  2232. break;
  2233. }
  2234. case 10: //from top right
  2235. {
  2236. bool doubleWide = actStack->doubleWide();
  2237. int destHex = whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH-1 ) +
  2238. (actStack->attackerOwned && doubleWide ? 1 : 0);
  2239. if(vstd::contains(shadedHexes, destHex))
  2240. attackFromHex = destHex;
  2241. else if(actStack->attackerOwned) //if we are attacker
  2242. {
  2243. if(vstd::contains(shadedHexes, destHex+1))
  2244. attackFromHex = destHex+1;
  2245. }
  2246. else //if we are defender
  2247. {
  2248. if(vstd::contains(shadedHexes, destHex-1))
  2249. attackFromHex = destHex-1;
  2250. }
  2251. break;
  2252. }
  2253. case 11: //from right
  2254. {
  2255. if(actStack->doubleWide() && actStack->attackerOwned)
  2256. {
  2257. std::vector<THex> acc = curInt->cb->battleGetAvailableHexes(activeStack, false);
  2258. if(vstd::contains(acc, whichOne))
  2259. attackFromHex = whichOne + 1;
  2260. else
  2261. attackFromHex = whichOne + 2;
  2262. }
  2263. else
  2264. {
  2265. attackFromHex = whichOne + 1;
  2266. }
  2267. break;
  2268. }
  2269. case 13: //from bottom
  2270. {
  2271. int destHex = whichOne + ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH+1 );
  2272. if(vstd::contains(shadedHexes, destHex))
  2273. attackFromHex = destHex;
  2274. else if(attackingHeroInstance->tempOwner == curInt->cb->getMyColor()) //if we are attacker
  2275. {
  2276. if(vstd::contains(shadedHexes, destHex+1))
  2277. attackFromHex = destHex+1;
  2278. }
  2279. else //if we are defender
  2280. {
  2281. if(vstd::contains(shadedHexes, destHex-1))
  2282. attackFromHex = destHex-1;
  2283. }
  2284. break;
  2285. }
  2286. case 14: //from top
  2287. {
  2288. int destHex = whichOne - ( (whichOne/BFIELD_WIDTH)%2 ? BFIELD_WIDTH : BFIELD_WIDTH-1 );
  2289. if(vstd::contains(shadedHexes, destHex))
  2290. attackFromHex = destHex;
  2291. else if(attackingHeroInstance->tempOwner == curInt->cb->getMyColor()) //if we are attacker
  2292. {
  2293. if(vstd::contains(shadedHexes, destHex+1))
  2294. attackFromHex = destHex+1;
  2295. }
  2296. else //if we are defender
  2297. {
  2298. if(vstd::contains(shadedHexes, destHex-1))
  2299. attackFromHex = destHex-1;
  2300. }
  2301. break;
  2302. }
  2303. }
  2304. if(attackFromHex >= 0) //we can be in this line when unreachable creature is L - clicked (as of revision 1308)
  2305. {
  2306. giveCommand(6, attackFromHex, activeStack->ID, whichOne);
  2307. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  2308. }
  2309. }
  2310. else if (actSt->hasBonusOfType(Bonus::HEALER) && actSt->owner == dest->owner) //friendly creature we can heal
  2311. {
  2312. giveCommand(12, whichOne, activeStack->ID); //command healing
  2313. CCS->curh->changeGraphic(1, 6); //cursor should be changed
  2314. }
  2315. }
  2316. }
  2317. }
  2318. void CBattleInterface::stackIsCatapulting(const CatapultAttack & ca)
  2319. {
  2320. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = ca.attackedParts.begin(); it != ca.attackedParts.end(); ++it)
  2321. {
  2322. const CStack * stack = curInt->cb->battleGetStackByID(ca.attacker);
  2323. addNewAnim(new CShootingAnim(this, stack, it->first.second, NULL, true, it->second));
  2324. SDL_FreeSurface(siegeH->walls[it->first.first + 2]);
  2325. siegeH->walls[it->first.first + 2] = BitmapHandler::loadBitmap(
  2326. siegeH->getSiegeName(it->first.first + 2, curInt->cb->battleGetWallState(it->first.first)) );
  2327. }
  2328. waitForAnims();
  2329. }
  2330. void CBattleInterface::battleFinished(const BattleResult& br)
  2331. {
  2332. bresult = &br;
  2333. LOCPLINT->pim->unlock();
  2334. animsAreDisplayed.waitUntil(false);
  2335. LOCPLINT->pim->lock();
  2336. displayBattleFinished();
  2337. }
  2338. void CBattleInterface::displayBattleFinished()
  2339. {
  2340. CCS->curh->changeGraphic(0,0);
  2341. SDL_Rect temp_rect = genRect(561, 470, (screen->w - 800)/2 + 165, (screen->h - 600)/2 + 19);
  2342. CCS->musich->stopMusic();
  2343. resWindow = new CBattleResultWindow(*bresult, temp_rect, this);
  2344. GH.pushInt(resWindow);
  2345. }
  2346. void CBattleInterface::spellCast( const BattleSpellCast * sc )
  2347. {
  2348. const CSpell &spell = *CGI->spellh->spells[sc->id];
  2349. //spell opening battle is cast when no stack is active
  2350. if(sc->castedByHero && ( activeStack == NULL || sc->side == !activeStack->attackerOwned) )
  2351. bSpell->block(true);
  2352. std::vector< std::string > anims; //for magic arrow and ice bolt
  2353. if (vstd::contains(CCS->soundh->spellSounds, &spell))
  2354. CCS->soundh->playSound(CCS->soundh->spellSounds[&spell]);
  2355. switch(sc->id)
  2356. {
  2357. case 15: //magic arrow
  2358. {
  2359. //initialization of anims
  2360. 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");
  2361. }
  2362. case 16: //ice bolt
  2363. {
  2364. if(anims.size() == 0) //initialization of anims
  2365. {
  2366. 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");
  2367. }
  2368. } //end of ice bolt only part
  2369. { //common ice bolt and magic arrow part
  2370. //initial variables
  2371. std::string animToDisplay;
  2372. Point srccoord = (sc->side ? Point(770, 60) : Point(30, 60)) + pos;
  2373. Point destcoord = CBattleHex::getXYUnitAnim(sc->tile, !sc->side, curInt->cb->battleGetStackByPos(sc->tile), this); //position attacked by arrow
  2374. destcoord.x += 250; destcoord.y += 240;
  2375. //animation angle
  2376. float angle = atan2(float(destcoord.x - srccoord.x), float(destcoord.y - srccoord.y));
  2377. bool Vflip = false;
  2378. if (angle < 0)
  2379. {
  2380. Vflip = true;
  2381. angle = -angle;
  2382. }
  2383. //choosing animation by angle
  2384. if(angle > 1.50)
  2385. animToDisplay = anims[0];
  2386. else if(angle > 1.20)
  2387. animToDisplay = anims[1];
  2388. else if(angle > 0.90)
  2389. animToDisplay = anims[2];
  2390. else if(angle > 0.60)
  2391. animToDisplay = anims[3];
  2392. else
  2393. animToDisplay = anims[4];
  2394. //displaying animation
  2395. CDefEssential * animDef = CDefHandler::giveDefEss(animToDisplay);
  2396. int steps = sqrt((float)((destcoord.x - srccoord.x)*(destcoord.x - srccoord.x) + (destcoord.y - srccoord.y) * (destcoord.y - srccoord.y))) / 40;
  2397. if(steps <= 0)
  2398. steps = 1;
  2399. int dx = (destcoord.x - srccoord.x - animDef->ourImages[0].bitmap->w)/steps, dy = (destcoord.y - srccoord.y - animDef->ourImages[0].bitmap->h)/steps;
  2400. delete animDef;
  2401. addNewAnim(new CSpellEffectAnim(this, animToDisplay, srccoord.x, srccoord.y, dx, dy, Vflip));
  2402. break; //for 15 and 16 cases
  2403. }
  2404. case 17: //lightning bolt
  2405. case 77: //thunderbolt
  2406. displayEffect(1, sc->tile);
  2407. displayEffect(spell.mainEffectAnim, sc->tile);
  2408. break;
  2409. case 35: //dispel
  2410. case 37: //cure
  2411. case 38: //resurrection
  2412. case 39: //animate dead
  2413. case 78: //dispel helpful spells
  2414. for(std::set<ui32>::const_iterator it = sc->affectedCres.begin(); it != sc->affectedCres.end(); ++it)
  2415. {
  2416. displayEffect(spell.mainEffectAnim, curInt->cb->battleGetStackByID(*it, false)->position);
  2417. }
  2418. break;
  2419. case 66: case 67: case 68: case 69: //summon elemental
  2420. addNewAnim(new CDummyAnim(this, 2));
  2421. break;
  2422. } //switch(sc->id)
  2423. //support for resistance
  2424. for(int j=0; j<sc->resisted.size(); ++j)
  2425. {
  2426. int tile = curInt->cb->battleGetStackByID(sc->resisted[j])->position;
  2427. displayEffect(78, tile);
  2428. }
  2429. //displaying message in console
  2430. if(sc->affectedCres.size() == 1)
  2431. {
  2432. std::string text = CGI->generaltexth->allTexts[195];
  2433. if(sc->castedByHero)
  2434. {
  2435. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetFightingHero(sc->side)->name);
  2436. }
  2437. else
  2438. {
  2439. boost::algorithm::replace_first(text, "%s", "Creature"); //TODO: better fix
  2440. }
  2441. boost::algorithm::replace_first(text, "%s", CGI->spellh->spells[sc->id]->name);
  2442. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetStackByID(*sc->affectedCres.begin(), false)->getCreature()->namePl );
  2443. console->addText(text);
  2444. }
  2445. else
  2446. {
  2447. std::string text = CGI->generaltexth->allTexts[196];
  2448. if(sc->castedByHero)
  2449. {
  2450. boost::algorithm::replace_first(text, "%s", curInt->cb->battleGetFightingHero(sc->side)->name);
  2451. }
  2452. else
  2453. {
  2454. boost::algorithm::replace_first(text, "%s", "Creature"); //TODO: better fix
  2455. }
  2456. boost::algorithm::replace_first(text, "%s", CGI->spellh->spells[sc->id]->name);
  2457. console->addText(text);
  2458. }
  2459. if(sc->dmgToDisplay != 0)
  2460. {
  2461. std::string dmgInfo = CGI->generaltexth->allTexts[343].substr(1, CGI->generaltexth->allTexts[343].size() - 1);
  2462. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(sc->dmgToDisplay));
  2463. console->addText(dmgInfo);
  2464. }
  2465. waitForAnims();
  2466. }
  2467. void CBattleInterface::battleStacksEffectsSet(const SetStackEffect & sse)
  2468. {
  2469. for(std::vector<ui32>::const_iterator ci = sse.stacks.begin(); ci!=sse.stacks.end(); ++ci)
  2470. {
  2471. displayEffect(CGI->spellh->spells[sse.effect.id]->mainEffectAnim, curInt->cb->battleGetStackByID(*ci)->position);
  2472. }
  2473. if (activeStack != NULL) //it can be -1 when a creature casts effect
  2474. {
  2475. redrawBackgroundWithHexes(activeStack);
  2476. }
  2477. }
  2478. void CBattleInterface::castThisSpell(int spellID)
  2479. {
  2480. BattleAction * ba = new BattleAction;
  2481. ba->actionType = BattleAction::HERO_SPELL;
  2482. ba->additionalInfo = spellID; //spell number
  2483. ba->destinationTile = -1;
  2484. ba->stackNumber = (attackingHeroInstance->tempOwner == curInt->playerID) ? -1 : -2;
  2485. ba->side = defendingHeroInstance ? (curInt->playerID == defendingHeroInstance->tempOwner) : false;
  2486. spellToCast = ba;
  2487. spellDestSelectMode = true;
  2488. //choosing possible tragets
  2489. const CGHeroInstance * castingHero = (attackingHeroInstance->tempOwner == curInt->playerID) ? attackingHeroInstance : defendingHeroInstance;
  2490. const CSpell & spell = *CGI->spellh->spells[spellID];
  2491. spellSelMode = 0;
  2492. if(spell.attributes.find("CREATURE_TARGET") != std::string::npos) //spell to be cast on one specific creature
  2493. {
  2494. switch(spell.positiveness)
  2495. {
  2496. case -1 :
  2497. spellSelMode = 2;
  2498. break;
  2499. case 0:
  2500. spellSelMode = 3;
  2501. break;
  2502. case 1:
  2503. spellSelMode = 1;
  2504. break;
  2505. }
  2506. }
  2507. if(spell.attributes.find("CREATURE_TARGET_1") != std::string::npos ||
  2508. spell.attributes.find("CREATURE_TARGET_2") != std::string::npos) //spell to be cast on a specific creature but massive on expert
  2509. {
  2510. if(castingHero && castingHero->getSpellSchoolLevel(&spell) < 3)
  2511. {
  2512. switch(spell.positiveness)
  2513. {
  2514. case -1 :
  2515. spellSelMode = 2;
  2516. break;
  2517. case 0:
  2518. spellSelMode = 3;
  2519. break;
  2520. case 1:
  2521. spellSelMode = 1;
  2522. break;
  2523. }
  2524. }
  2525. else
  2526. {
  2527. spellSelMode = -1;
  2528. }
  2529. }
  2530. if(spell.attributes.find("OBSTACLE_TARGET") != std::string::npos) //spell to be cast on an obstacle
  2531. {
  2532. spellSelMode = 4;
  2533. }
  2534. if(spell.range[ castingHero->getSpellSchoolLevel(&spell) ] == "X") //spell has no range
  2535. {
  2536. spellSelMode = -1;
  2537. }
  2538. if(spell.id == 63) //teleport
  2539. {
  2540. spellSelMode = 5;
  2541. }
  2542. if(spell.range[ castingHero->getSpellSchoolLevel(&spell) ].size() > 1) //spell has many-hex range
  2543. {
  2544. spellSelMode = 0;
  2545. }
  2546. if(spellSelMode == -1) //user does not have to select location
  2547. {
  2548. spellToCast->destinationTile = -1;
  2549. curInt->cb->battleMakeAction(spellToCast);
  2550. endCastingSpell();
  2551. }
  2552. else
  2553. {
  2554. CCS->curh->changeGraphic(3, 0);
  2555. }
  2556. }
  2557. void CBattleInterface::displayEffect(ui32 effect, int destTile)
  2558. {
  2559. addNewAnim(new CSpellEffectAnim(this, effect, destTile));
  2560. }
  2561. void CBattleInterface::setAnimSpeed(int set)
  2562. {
  2563. curInt->sysOpts.animSpeed = set;
  2564. curInt->sysOpts.settingsChanged();
  2565. }
  2566. int CBattleInterface::getAnimSpeed() const
  2567. {
  2568. return curInt->sysOpts.animSpeed;
  2569. curInt->sysOpts.settingsChanged();
  2570. }
  2571. void CBattleInterface::activateStack()
  2572. {
  2573. activeStack = stackToActivate;
  2574. stackToActivate = NULL;
  2575. const CStack *s = activeStack;
  2576. myTurn = true;
  2577. if(attackerInt && defenderInt) //hotseat -> need to pick which interface "takes over" as active
  2578. curInt = attackerInt->playerID == s->owner ? attackerInt : defenderInt;
  2579. queue->update();
  2580. redrawBackgroundWithHexes(activeStack);
  2581. bWait->block(vstd::contains(s->state, WAITING)); //block waiting button if stack has been already waiting
  2582. //block cast spell button if hero doesn't have a spellbook
  2583. bSpell->block(!curInt->cb->battleCanCastSpell());
  2584. bSurrender->block((curInt == attackerInt ? defendingHeroInstance : attackingHeroInstance) == NULL);
  2585. bFlee->block(!curInt->cb->battleCanFlee());
  2586. GH.fakeMouseMove();
  2587. if(!pendingAnims.size() && !active)
  2588. activate();
  2589. }
  2590. float CBattleInterface::getAnimSpeedMultiplier() const
  2591. {
  2592. switch(curInt->sysOpts.animSpeed)
  2593. {
  2594. case 1:
  2595. return 3.5f;
  2596. case 2:
  2597. return 2.2f;
  2598. case 4:
  2599. return 1.0f;
  2600. default:
  2601. return 0.0f;
  2602. }
  2603. }
  2604. void CBattleInterface::endCastingSpell()
  2605. {
  2606. assert(spellDestSelectMode);
  2607. delete spellToCast;
  2608. spellToCast = NULL;
  2609. spellDestSelectMode = false;
  2610. CCS->curh->changeGraphic(1, 6);
  2611. }
  2612. void CBattleInterface::showAliveStack(const CStack *stack, SDL_Surface * to)
  2613. {
  2614. int ID = stack->ID;
  2615. if(creAnims.find(ID) == creAnims.end()) //eg. for summoned but not yet handled stacks
  2616. return;
  2617. int animType = creAnims[ID]->getType();
  2618. int affectingSpeed = curInt->sysOpts.animSpeed;
  2619. if(animType == 1 || animType == 2) //standing stacks should not stand faster :)
  2620. affectingSpeed = 2;
  2621. bool incrementFrame = (animCount%(4/affectingSpeed)==0) && animType!=5 && animType!=20 && animType!=2;
  2622. if(animType == 2)
  2623. {
  2624. if(standingFrame.find(ID)!=standingFrame.end())
  2625. {
  2626. incrementFrame = (animCount%(8/affectingSpeed)==0);
  2627. if(incrementFrame)
  2628. {
  2629. ++standingFrame[ID];
  2630. if(standingFrame[ID] == creAnims[ID]->framesInGroup(2))
  2631. {
  2632. standingFrame.erase(standingFrame.find(ID));
  2633. }
  2634. }
  2635. }
  2636. else
  2637. {
  2638. if((rand()%50) == 0)
  2639. {
  2640. standingFrame.insert(std::make_pair(ID, 0));
  2641. }
  2642. }
  2643. }
  2644. creAnims[ID]->nextFrame(to, creAnims[ID]->pos.x, creAnims[ID]->pos.y, creDir[ID], animCount, incrementFrame, activeStack && ID==activeStack->ID, ID==mouseHoveredStack); //increment always when moving, never if stack died
  2645. //printing amount
  2646. if(stack->count > 0 //don't print if stack is not alive
  2647. && (!curInt->curAction
  2648. || (curInt->curAction->stackNumber != ID //don't print if stack is currently taking an action
  2649. && (curInt->curAction->actionType != BattleAction::WALK_AND_ATTACK || stack->position != curInt->curAction->additionalInfo) //nor if it's an object of attack
  2650. && (curInt->curAction->destinationTile != stack->position) //nor if it's on destination tile for current action
  2651. )
  2652. )
  2653. && !stack->hasBonusOfType(Bonus::SIEGE_WEAPON) //and not a war machine...
  2654. )
  2655. {
  2656. int xAdd = stack->attackerOwned ? 220 : 202;
  2657. //blitting amoutn background box
  2658. SDL_Surface *amountBG = NULL;
  2659. BonusList spellEffects = stack->getSpellBonuses();
  2660. if(!spellEffects.size())
  2661. {
  2662. amountBG = amountNormal;
  2663. }
  2664. else
  2665. {
  2666. int pos=0; //determining total positiveness of effects
  2667. std::vector<si32> spellIds = stack->activeSpells();
  2668. for(std::vector<si32>::const_iterator it = spellIds.begin(); it != spellIds.end(); it++)
  2669. {
  2670. pos += CGI->spellh->spells[ *it ]->positiveness;
  2671. }
  2672. if(pos > 0)
  2673. {
  2674. amountBG = amountPositive;
  2675. }
  2676. else if(pos < 0)
  2677. {
  2678. amountBG = amountNegative;
  2679. }
  2680. else
  2681. {
  2682. amountBG = amountEffNeutral;
  2683. }
  2684. }
  2685. SDL_BlitSurface(amountBG, NULL, to, &genRect(amountNormal->h, amountNormal->w, creAnims[ID]->pos.x + xAdd, creAnims[ID]->pos.y + 260));
  2686. //blitting amount
  2687. CSDL_Ext::printAtMiddle(
  2688. makeNumberShort(stack->count),
  2689. creAnims[ID]->pos.x + xAdd + 15,
  2690. creAnims[ID]->pos.y + 260 + 5,
  2691. FONT_TINY,
  2692. zwykly,
  2693. to
  2694. );
  2695. }
  2696. }
  2697. void CBattleInterface::showPieceOfWall(SDL_Surface * to, int hex, const std::vector<const CStack*> & stacks)
  2698. {
  2699. if(!siegeH)
  2700. return;
  2701. static const std::map<int, int> hexToPart = boost::assign::map_list_of(12, 8)(16, 1)(29, 7)(50, 2)(62, 12)(78, 6)(112, 10)(147, 5)(165, 11)(182, 3)(186, 0);
  2702. //additionally print bottom wall
  2703. if(hex == 182)
  2704. {
  2705. siegeH->printPartOfWall(to, 4);
  2706. }
  2707. std::map<int, int>::const_iterator it = hexToPart.find(hex);
  2708. if(it != hexToPart.end())
  2709. {
  2710. siegeH->printPartOfWall(to, it->second);
  2711. //print creature in turret
  2712. int posToSeek = -1;
  2713. switch(it->second)
  2714. {
  2715. case 3: //bottom turret
  2716. posToSeek = -3;
  2717. break;
  2718. case 8: //upper turret
  2719. posToSeek = -4;
  2720. break;
  2721. case 2: //keep
  2722. posToSeek = -2;
  2723. break;
  2724. }
  2725. if(posToSeek != -1)
  2726. {
  2727. const CStack *turret = NULL;
  2728. BOOST_FOREACH(const CStack *s, stacks)
  2729. {
  2730. if(s->position == posToSeek)
  2731. {
  2732. turret = s;
  2733. break;
  2734. }
  2735. }
  2736. if(turret)
  2737. {
  2738. showAliveStack(turret, to);
  2739. //blitting creature cover
  2740. switch(posToSeek)
  2741. {
  2742. case -3: //bottom turret
  2743. siegeH->printPartOfWall(to, 16);
  2744. break;
  2745. case -4: //upper turret
  2746. siegeH->printPartOfWall(to, 17);
  2747. break;
  2748. case -2: //keep
  2749. siegeH->printPartOfWall(to, 15);
  2750. break;
  2751. }
  2752. }
  2753. }
  2754. }
  2755. }
  2756. void CBattleInterface::redrawBackgroundWithHexes(const CStack * activeStack)
  2757. {
  2758. shadedHexes = curInt->cb->battleGetAvailableHexes(activeStack, true);
  2759. //preparating background graphic with hexes and shaded hexes
  2760. blitAt(background, 0, 0, backgroundWithHexes);
  2761. if(curInt->sysOpts.printCellBorders)
  2762. CSDL_Ext::blit8bppAlphaTo24bpp(cellBorders, NULL, backgroundWithHexes, NULL);
  2763. if(curInt->sysOpts.printStackRange)
  2764. {
  2765. for(size_t m=0; m<shadedHexes.size(); ++m) //rows
  2766. {
  2767. int i = shadedHexes[m]/BFIELD_WIDTH; //row
  2768. int j = shadedHexes[m]%BFIELD_WIDTH-1; //column
  2769. int x = 58 + (i%2==0 ? 22 : 0) + 44*j;
  2770. int y = 86 + 42 * i;
  2771. CSDL_Ext::blit8bppAlphaTo24bpp(cellShade, NULL, backgroundWithHexes, &genRect(cellShade->h, cellShade->w, x, y));
  2772. }
  2773. }
  2774. }
  2775. void CBattleInterface::printConsoleAttacked( const CStack * defender, int dmg, int killed, const CStack * attacker )
  2776. {
  2777. char tabh[200];
  2778. int end = sprintf(tabh, CGI->generaltexth->allTexts[attacker->count > 1 ? 377 : 376].c_str(),
  2779. (attacker->count > 1 ? attacker->getCreature()->namePl.c_str() : attacker->getCreature()->nameSing.c_str()),
  2780. dmg);
  2781. if(killed > 0)
  2782. {
  2783. if(killed > 1)
  2784. {
  2785. sprintf(tabh + end, CGI->generaltexth->allTexts[379].c_str(), killed, defender->getCreature()->namePl.c_str());
  2786. }
  2787. else //killed == 1
  2788. {
  2789. sprintf(tabh + end, CGI->generaltexth->allTexts[378].c_str(), defender->getCreature()->nameSing.c_str());
  2790. }
  2791. }
  2792. console->addText(std::string(tabh));
  2793. }
  2794. void CBattleInterface::projectileShowHelper(SDL_Surface * to)
  2795. {
  2796. if(to == NULL)
  2797. to = screen;
  2798. std::list< std::list<SProjectileInfo>::iterator > toBeDeleted;
  2799. for(std::list<SProjectileInfo>::iterator it=projectiles.begin(); it!=projectiles.end(); ++it)
  2800. {
  2801. if(it->animStartDelay>0)
  2802. {
  2803. --(it->animStartDelay);
  2804. continue;
  2805. }
  2806. SDL_Rect dst;
  2807. dst.h = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->h;
  2808. dst.w = idToProjectile[it->creID]->ourImages[it->frameNum].bitmap->w;
  2809. dst.x = it->x;
  2810. dst.y = it->y;
  2811. if(it->reverse)
  2812. {
  2813. SDL_Surface * rev = CSDL_Ext::rotate01(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap);
  2814. CSDL_Ext::blit8bppAlphaTo24bpp(rev, NULL, to, &dst);
  2815. SDL_FreeSurface(rev);
  2816. }
  2817. else
  2818. {
  2819. CSDL_Ext::blit8bppAlphaTo24bpp(idToProjectile[it->creID]->ourImages[it->frameNum].bitmap, NULL, to, &dst);
  2820. }
  2821. //actualizing projectile
  2822. ++it->step;
  2823. if(it->step == it->lastStep)
  2824. {
  2825. toBeDeleted.insert(toBeDeleted.end(), it);
  2826. }
  2827. else
  2828. {
  2829. it->x += it->dx;
  2830. it->y += it->dy;
  2831. if(it->spin)
  2832. {
  2833. ++(it->frameNum);
  2834. it->frameNum %= idToProjectile[it->creID]->ourImages.size();
  2835. }
  2836. }
  2837. }
  2838. for(std::list< std::list<SProjectileInfo>::iterator >::iterator it = toBeDeleted.begin(); it!= toBeDeleted.end(); ++it)
  2839. {
  2840. projectiles.erase(*it);
  2841. }
  2842. }
  2843. void CBattleInterface::endAction(const BattleAction* action)
  2844. {
  2845. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  2846. // {
  2847. // activate();
  2848. // }
  2849. if(action->actionType == BattleAction::HERO_SPELL)
  2850. {
  2851. if(action->side)
  2852. defendingHero->setPhase(0);
  2853. else
  2854. attackingHero->setPhase(0);
  2855. }
  2856. if(action->actionType == BattleAction::WALK && creAnims[action->stackNumber]->getType() != 2) //walk or walk & attack
  2857. {
  2858. const CStack * stack = curInt->cb->battleGetStackByID(action->stackNumber);
  2859. pendingAnims.push_back(std::make_pair(new CBattleMoveEnd(this, stack, action->destinationTile), false));
  2860. }
  2861. if(action->actionType == BattleAction::CATAPULT) //catapult
  2862. {
  2863. }
  2864. queue->update();
  2865. }
  2866. void CBattleInterface::hideQueue()
  2867. {
  2868. curInt->sysOpts.showQueue = false;
  2869. queue->deactivate();
  2870. if(!queue->embedded)
  2871. {
  2872. moveBy(Point(0, -queue->pos.h / 2));
  2873. GH.totalRedraw();
  2874. }
  2875. }
  2876. void CBattleInterface::showQueue()
  2877. {
  2878. curInt->sysOpts.showQueue = true;
  2879. queue->activate();
  2880. if(!queue->embedded)
  2881. {
  2882. moveBy(Point(0, +queue->pos.h / 2));
  2883. GH.totalRedraw();
  2884. }
  2885. }
  2886. void CBattleInterface::startAction(const BattleAction* action)
  2887. {
  2888. const CStack *stack = curInt->cb->battleGetStackByID(action->stackNumber);
  2889. if(stack)
  2890. {
  2891. queue->update();
  2892. }
  2893. else
  2894. {
  2895. assert(action->actionType == BattleAction::HERO_SPELL); //only cast spell is valid action without acting stack number
  2896. }
  2897. if(action->actionType == BattleAction::WALK
  2898. || (action->actionType == BattleAction::WALK_AND_ATTACK && action->destinationTile != stack->position))
  2899. {
  2900. moveStarted = true;
  2901. if(creAnims[action->stackNumber]->framesInGroup(20))
  2902. {
  2903. const CStack * stack = curInt->cb->battleGetStackByID(action->stackNumber);
  2904. pendingAnims.push_back(std::make_pair(new CBattleMoveStart(this, stack), false));
  2905. }
  2906. }
  2907. if(active)
  2908. deactivate();
  2909. char txt[400];
  2910. if(action->actionType == BattleAction::HERO_SPELL) //when hero casts spell
  2911. {
  2912. if(action->side)
  2913. defendingHero->setPhase(4);
  2914. else
  2915. attackingHero->setPhase(4);
  2916. return;
  2917. }
  2918. if(!stack)
  2919. {
  2920. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  2921. return;
  2922. }
  2923. int txtid = 0;
  2924. switch(action->actionType)
  2925. {
  2926. case 3: //defend
  2927. txtid = 120;
  2928. break;
  2929. case 8: //wait
  2930. txtid = 136;
  2931. break;
  2932. case 11: //bad morale
  2933. txtid = -34; //negative -> no separate singular/plural form
  2934. displayEffect(30,stack->position);
  2935. break;
  2936. }
  2937. if(txtid > 0 && stack->count != 1)
  2938. txtid++; //move to plural text
  2939. else if(txtid < 0)
  2940. txtid = -txtid;
  2941. if(txtid)
  2942. {
  2943. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->count != 1) ? stack->getCreature()->namePl.c_str() : stack->getCreature()->nameSing.c_str(), 0);
  2944. console->addText(txt);
  2945. }
  2946. //displaying heal animation
  2947. if (action->actionType == BattleAction::STACK_HEAL)
  2948. {
  2949. displayEffect(50, action->destinationTile);
  2950. }
  2951. }
  2952. void CBattleInterface::waitForAnims()
  2953. {
  2954. LOCPLINT->pim->unlock();
  2955. animsAreDisplayed.waitWhileTrue();
  2956. LOCPLINT->pim->lock();
  2957. }
  2958. void CBattleHero::show(SDL_Surface *to)
  2959. {
  2960. //animation of flag
  2961. if(flip)
  2962. {
  2963. CSDL_Ext::blit8bppAlphaTo24bpp(
  2964. flag->ourImages[flagAnim].bitmap,
  2965. NULL,
  2966. screen,
  2967. &genRect(
  2968. flag->ourImages[flagAnim].bitmap->h,
  2969. flag->ourImages[flagAnim].bitmap->w,
  2970. 62 + pos.x,
  2971. 39 + pos.y
  2972. )
  2973. );
  2974. }
  2975. else
  2976. {
  2977. CSDL_Ext::blit8bppAlphaTo24bpp(
  2978. flag->ourImages[flagAnim].bitmap,
  2979. NULL,
  2980. screen,
  2981. &genRect(
  2982. flag->ourImages[flagAnim].bitmap->h,
  2983. flag->ourImages[flagAnim].bitmap->w,
  2984. 71 + pos.x,
  2985. 39 + pos.y
  2986. )
  2987. );
  2988. }
  2989. ++flagAnimCount;
  2990. if(flagAnimCount%4==0)
  2991. {
  2992. ++flagAnim;
  2993. flagAnim %= flag->ourImages.size();
  2994. }
  2995. //animation of hero
  2996. int tick=-1;
  2997. for(int i=0; i<dh->ourImages.size(); ++i)
  2998. {
  2999. if(dh->ourImages[i].groupNumber==phase)
  3000. ++tick;
  3001. if(tick==image)
  3002. {
  3003. SDL_Rect posb = pos;
  3004. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[i].bitmap, NULL, to, &posb);
  3005. if(phase != 4 || nextPhase != -1 || image < 4)
  3006. {
  3007. if(flagAnimCount%2==0)
  3008. {
  3009. ++image;
  3010. }
  3011. if(dh->ourImages[(i+1)%dh->ourImages.size()].groupNumber!=phase) //back to appropriate frame
  3012. {
  3013. image = 0;
  3014. }
  3015. }
  3016. if(phase == 4 && nextPhase != -1 && image == 7)
  3017. {
  3018. phase = nextPhase;
  3019. nextPhase = -1;
  3020. image = 0;
  3021. }
  3022. break;
  3023. }
  3024. }
  3025. }
  3026. void CBattleHero::activate()
  3027. {
  3028. activateLClick();
  3029. }
  3030. void CBattleHero::deactivate()
  3031. {
  3032. deactivateLClick();
  3033. }
  3034. void CBattleHero::setPhase(int newPhase)
  3035. {
  3036. if(phase != 4)
  3037. {
  3038. phase = newPhase;
  3039. image = 0;
  3040. }
  3041. else
  3042. {
  3043. nextPhase = newPhase;
  3044. }
  3045. }
  3046. void CBattleHero::clickLeft(tribool down, bool previousState)
  3047. {
  3048. if(myOwner->spellDestSelectMode) //we are casting a spell
  3049. return;
  3050. if(!down && myHero && myOwner->curInt->cb->battleCanCastSpell()) //check conditions
  3051. {
  3052. for(int it=0; it<BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  3053. {
  3054. if(myOwner->bfield[it].hovered && myOwner->bfield[it].strictHovered)
  3055. return;
  3056. }
  3057. CCS->curh->changeGraphic(0,0);
  3058. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), myHero, myOwner->curInt);
  3059. GH.pushInt(spellWindow);
  3060. }
  3061. }
  3062. CBattleHero::CBattleHero(const std::string & defName, int phaseG, int imageG, bool flipG, unsigned char player, const CGHeroInstance * hero, const CBattleInterface * owner): flip(flipG), myHero(hero), myOwner(owner), phase(phaseG), nextPhase(-1), image(imageG), flagAnim(0), flagAnimCount(0)
  3063. {
  3064. dh = CDefHandler::giveDef( defName );
  3065. for(int i=0; i<dh->ourImages.size(); ++i) //transforming images
  3066. {
  3067. if(flip)
  3068. {
  3069. SDL_Surface * hlp = CSDL_Ext::rotate01(dh->ourImages[i].bitmap);
  3070. SDL_FreeSurface(dh->ourImages[i].bitmap);
  3071. dh->ourImages[i].bitmap = hlp;
  3072. }
  3073. CSDL_Ext::alphaTransform(dh->ourImages[i].bitmap);
  3074. }
  3075. if(flip)
  3076. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  3077. else
  3078. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  3079. //coloring flag and adding transparency
  3080. for(int i=0; i<flag->ourImages.size(); ++i)
  3081. {
  3082. CSDL_Ext::alphaTransform(flag->ourImages[i].bitmap);
  3083. graphics->blueToPlayersAdv(flag->ourImages[i].bitmap, player);
  3084. }
  3085. }
  3086. CBattleHero::~CBattleHero()
  3087. {
  3088. delete dh;
  3089. delete flag;
  3090. }
  3091. Point CBattleHex::getXYUnitAnim(const int & hexNum, const bool & attacker, const CStack * stack, const CBattleInterface * cbi)
  3092. {
  3093. Point ret(-500, -500); //returned value
  3094. if(stack->position < 0) //creatures in turrets
  3095. {
  3096. const CCreature & turretCreature = *CGI->creh->creatures[ CGI->creh->factionToTurretCreature[cbi->siegeH->town->town->typeID] ];
  3097. int xShift = turretCreature.isDoubleWide() ? 44 : 0;
  3098. switch(stack->position)
  3099. {
  3100. case -2: //keep
  3101. ret = Point(505 + xShift, -66);
  3102. break;
  3103. case -3: //lower turret
  3104. ret = Point(368 + xShift, 304);
  3105. break;
  3106. case -4: //upper turret
  3107. ret = Point(339 + xShift, -192);
  3108. break;
  3109. }
  3110. }
  3111. else
  3112. {
  3113. ret.y = -139 + 42 * (hexNum/BFIELD_WIDTH); //counting y
  3114. //counting x
  3115. if(attacker)
  3116. {
  3117. ret.x = -160 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  3118. }
  3119. else
  3120. {
  3121. ret.x = -219 + 22 * ( ((hexNum/BFIELD_WIDTH) + 1)%2 ) + 44 * (hexNum % BFIELD_WIDTH);
  3122. }
  3123. //shifting position for double - hex creatures
  3124. if(stack && stack->doubleWide())
  3125. {
  3126. if(attacker)
  3127. {
  3128. ret.x -= 42;
  3129. }
  3130. else
  3131. {
  3132. ret.x += 42;
  3133. }
  3134. }
  3135. }
  3136. //returning
  3137. return ret +CPlayerInterface::battleInt->pos;
  3138. }
  3139. void CBattleHex::activate()
  3140. {
  3141. activateHover();
  3142. activateMouseMove();
  3143. activateLClick();
  3144. activateRClick();
  3145. }
  3146. void CBattleHex::deactivate()
  3147. {
  3148. deactivateHover();
  3149. deactivateMouseMove();
  3150. deactivateLClick();
  3151. deactivateRClick();
  3152. }
  3153. void CBattleHex::hover(bool on)
  3154. {
  3155. hovered = on;
  3156. //Hoverable::hover(on);
  3157. if(!on && setAlterText)
  3158. {
  3159. myInterface->console->alterTxt = std::string();
  3160. setAlterText = false;
  3161. }
  3162. }
  3163. CBattleHex::CBattleHex() : setAlterText(false), myNumber(-1), accessible(true), hovered(false), strictHovered(false), myInterface(NULL)
  3164. {
  3165. }
  3166. void CBattleHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  3167. {
  3168. if(myInterface->cellShade)
  3169. {
  3170. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  3171. {
  3172. strictHovered = false;
  3173. }
  3174. else //hovered pixel is inside hex
  3175. {
  3176. strictHovered = true;
  3177. }
  3178. }
  3179. if(hovered && strictHovered) //print attacked creature to console
  3180. {
  3181. if(myInterface->console->alterTxt.size() == 0 && myInterface->curInt->cb->battleGetStackByPos(myNumber) != NULL &&
  3182. myInterface->curInt->cb->battleGetStackByPos(myNumber)->owner != myInterface->curInt->playerID &&
  3183. myInterface->curInt->cb->battleGetStackByPos(myNumber)->alive())
  3184. {
  3185. char tabh[160];
  3186. const CStack * attackedStack = myInterface->curInt->cb->battleGetStackByPos(myNumber);
  3187. const std::string & attackedName = attackedStack->count == 1 ? attackedStack->getCreature()->nameSing : attackedStack->getCreature()->namePl;
  3188. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  3189. myInterface->console->alterTxt = std::string(tabh);
  3190. setAlterText = true;
  3191. }
  3192. }
  3193. else if(setAlterText)
  3194. {
  3195. myInterface->console->alterTxt = std::string();
  3196. setAlterText = false;
  3197. }
  3198. }
  3199. void CBattleHex::clickLeft(tribool down, bool previousState)
  3200. {
  3201. if(!down && hovered && strictHovered) //we've been really clicked!
  3202. {
  3203. myInterface->hexLclicked(myNumber);
  3204. }
  3205. }
  3206. void CBattleHex::clickRight(tribool down, bool previousState)
  3207. {
  3208. const CStack * myst = myInterface->curInt->cb->battleGetStackByPos(myNumber); //stack info
  3209. if(hovered && strictHovered && myst!=NULL)
  3210. {
  3211. if(!myst->alive()) return;
  3212. if(down)
  3213. {
  3214. GH.pushInt(new CCreInfoWindow(*myst));
  3215. }
  3216. }
  3217. }
  3218. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt(""), whoSetAlter(0)
  3219. {
  3220. }
  3221. CBattleConsole::~CBattleConsole()
  3222. {
  3223. texts.clear();
  3224. }
  3225. void CBattleConsole::show(SDL_Surface * to)
  3226. {
  3227. if(ingcAlter.size())
  3228. {
  3229. CSDL_Ext::printAtMiddleWB(ingcAlter, pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  3230. }
  3231. else if(alterTxt.size())
  3232. {
  3233. CSDL_Ext::printAtMiddleWB(alterTxt, pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  3234. }
  3235. else if(texts.size())
  3236. {
  3237. if(texts.size()==1)
  3238. {
  3239. CSDL_Ext::printAtMiddleWB(texts[0], pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  3240. }
  3241. else
  3242. {
  3243. CSDL_Ext::printAtMiddleWB(texts[lastShown-1], pos.x + pos.w/2, pos.y + 11, FONT_SMALL, 80, zwykly, to);
  3244. CSDL_Ext::printAtMiddleWB(texts[lastShown], pos.x + pos.w/2, pos.y + 27, FONT_SMALL, 80, zwykly, to);
  3245. }
  3246. }
  3247. }
  3248. bool CBattleConsole::addText(const std::string & text)
  3249. {
  3250. if(text.size()>70)
  3251. return false; //text too long!
  3252. int firstInToken = 0;
  3253. for(int i=0; i<text.size(); ++i) //tokenize
  3254. {
  3255. if(text[i] == 10)
  3256. {
  3257. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  3258. firstInToken = i+1;
  3259. }
  3260. }
  3261. texts.push_back( text.substr(firstInToken, text.size()) );
  3262. lastShown = texts.size()-1;
  3263. return true;
  3264. }
  3265. void CBattleConsole::eraseText(unsigned int pos)
  3266. {
  3267. if(pos < texts.size())
  3268. {
  3269. texts.erase(texts.begin() + pos);
  3270. if(lastShown == texts.size())
  3271. --lastShown;
  3272. }
  3273. }
  3274. void CBattleConsole::changeTextAt(const std::string & text, unsigned int pos)
  3275. {
  3276. if(pos >= texts.size()) //no such pos
  3277. return;
  3278. texts[pos] = text;
  3279. }
  3280. void CBattleConsole::scrollUp(unsigned int by)
  3281. {
  3282. if(lastShown > by)
  3283. lastShown -= by;
  3284. }
  3285. void CBattleConsole::scrollDown(unsigned int by)
  3286. {
  3287. if(lastShown + by < texts.size())
  3288. lastShown += by;
  3289. }
  3290. CBattleResultWindow::CBattleResultWindow(const BattleResult &br, const SDL_Rect & pos, CBattleInterface * _owner)
  3291. : owner(_owner)
  3292. {
  3293. this->pos = pos;
  3294. background = BitmapHandler::loadBitmap("CPRESULT.BMP", true);
  3295. graphics->blueToPlayersAdv(background, owner->curInt->playerID);
  3296. SDL_Surface * pom = SDL_ConvertSurface(background, screen->format, screen->flags);
  3297. SDL_FreeSurface(background);
  3298. background = pom;
  3299. exit = new AdventureMapButton (std::string(), std::string(), boost::bind(&CBattleResultWindow::bExitf,this), 384 + pos.x, 505 + pos.y, "iok6432.def", SDLK_RETURN);
  3300. if(br.winner==0) //attacker won
  3301. {
  3302. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 59, 124, FONT_SMALL, zwykly, background);
  3303. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 408, 124, FONT_SMALL, zwykly, background);
  3304. }
  3305. else //if(br.winner==1)
  3306. {
  3307. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[411], 59, 124, FONT_SMALL, zwykly, background);
  3308. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[410], 412, 124, FONT_SMALL, zwykly, background);
  3309. }
  3310. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[407], 232, 302, FONT_BIG, tytulowy, background);
  3311. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[408], 232, 332, FONT_BIG, zwykly, background);
  3312. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[409], 237, 428, FONT_BIG, zwykly, background);
  3313. std::string attackerName, defenderName;
  3314. if(owner->attackingHeroInstance) //a hero attacked
  3315. {
  3316. SDL_BlitSurface(graphics->portraitLarge[owner->attackingHeroInstance->portrait], NULL, background, &genRect(64, 58, 21, 38));
  3317. //setting attackerName
  3318. attackerName = owner->attackingHeroInstance->name;
  3319. }
  3320. else //a monster attacked
  3321. {
  3322. int bestMonsterID = -1;
  3323. int bestPower = 0;
  3324. for(TSlots::const_iterator it = owner->army1->Slots().begin(); it!=owner->army1->Slots().end(); ++it)
  3325. {
  3326. if( it->second->type->AIValue > bestPower)
  3327. {
  3328. bestPower = it->second->type->AIValue;
  3329. bestMonsterID = it->second->type->idNumber;
  3330. }
  3331. }
  3332. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 21, 38));
  3333. //setting attackerName
  3334. attackerName = CGI->creh->creatures[bestMonsterID]->namePl;
  3335. }
  3336. if(owner->defendingHeroInstance) //a hero defended
  3337. {
  3338. SDL_BlitSurface(graphics->portraitLarge[owner->defendingHeroInstance->portrait], NULL, background, &genRect(64, 58, 392, 38));
  3339. //setting defenderName
  3340. defenderName = owner->defendingHeroInstance->name;
  3341. }
  3342. else //a monster defended
  3343. {
  3344. int bestMonsterID = -1;
  3345. int bestPower = 0;
  3346. for(TSlots::const_iterator it = owner->army2->Slots().begin(); it!=owner->army2->Slots().end(); ++it)
  3347. {
  3348. if( it->second->type->AIValue > bestPower)
  3349. {
  3350. bestPower = it->second->type->AIValue;
  3351. bestMonsterID = it->second->type->idNumber;
  3352. }
  3353. }
  3354. SDL_BlitSurface(graphics->bigImgs[bestMonsterID], NULL, background, &genRect(64, 58, 392, 38));
  3355. //setting defenderName
  3356. defenderName = CGI->creh->creatures[bestMonsterID]->namePl;
  3357. }
  3358. //printing attacker and defender's names
  3359. CSDL_Ext::printAt(attackerName, 89, 37, FONT_SMALL, zwykly, background);
  3360. CSDL_Ext::printTo(defenderName, 381, 53, FONT_SMALL, zwykly, background);
  3361. //printing casualities
  3362. for(int step = 0; step < 2; ++step)
  3363. {
  3364. if(br.casualties[step].size()==0)
  3365. {
  3366. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[523], 235, 360 + 97*step, FONT_SMALL, zwykly, background);
  3367. }
  3368. else
  3369. {
  3370. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  3371. int yPos = 344 + step*97;
  3372. for(std::map<ui32,si32>::const_iterator it=br.casualties[step].begin(); it!=br.casualties[step].end(); ++it)
  3373. {
  3374. blitAt(graphics->smallImgs[it->first], xPos, yPos, background);
  3375. std::ostringstream amount;
  3376. amount<<it->second;
  3377. CSDL_Ext::printAtMiddle(amount.str(), xPos+16, yPos + 42, FONT_SMALL, zwykly, background);
  3378. xPos += 42;
  3379. }
  3380. }
  3381. }
  3382. //printing result description
  3383. bool weAreAttacker = (owner->curInt->playerID == owner->attackingHeroInstance->tempOwner);
  3384. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  3385. {
  3386. int text;
  3387. switch(br.result)
  3388. {
  3389. case 0: text = 304; break;
  3390. case 1: text = 303; break;
  3391. case 2: text = 302; break;
  3392. }
  3393. CCS->musich->playMusic(musicBase::winBattle);
  3394. #ifdef _WIN32
  3395. CCS->videoh->open(VIDEO_WIN);
  3396. #else
  3397. CCS->videoh->open(VIDEO_WIN, true);
  3398. #endif
  3399. std::string str = CGI->generaltexth->allTexts[text];
  3400. const CGHeroInstance * ourHero = weAreAttacker? owner->attackingHeroInstance : owner->defendingHeroInstance;
  3401. if (ourHero)
  3402. {
  3403. str += CGI->generaltexth->allTexts[305];
  3404. boost::algorithm::replace_first(str,"%s",ourHero->name);
  3405. boost::algorithm::replace_first(str,"%d",boost::lexical_cast<std::string>(br.exp[weAreAttacker?0:1]));
  3406. }
  3407. CSDL_Ext::printAtMiddleWB(str, 235, 235, FONT_SMALL, 55, zwykly, background);
  3408. }
  3409. else // we lose
  3410. {
  3411. switch(br.result)
  3412. {
  3413. case 0: //normal victory
  3414. {
  3415. CCS->musich->playMusic(musicBase::loseCombat);
  3416. CCS->videoh->open(VIDEO_LOSE_BATTLE_START);
  3417. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[311], 235, 235, FONT_SMALL, zwykly, background);
  3418. break;
  3419. }
  3420. case 1: //flee
  3421. {
  3422. CCS->musich->playMusic(musicBase::retreatBattle);
  3423. CCS->videoh->open(VIDEO_RETREAT_START);
  3424. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[310], 235, 235, FONT_SMALL, zwykly, background);
  3425. break;
  3426. }
  3427. case 2: //surrender
  3428. {
  3429. CCS->musich->playMusic(musicBase::surrenderBattle);
  3430. CCS->videoh->open(VIDEO_SURRENDER);
  3431. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[309], 235, 220, FONT_SMALL, zwykly, background);
  3432. break;
  3433. }
  3434. }
  3435. }
  3436. }
  3437. CBattleResultWindow::~CBattleResultWindow()
  3438. {
  3439. SDL_FreeSurface(background);
  3440. }
  3441. void CBattleResultWindow::activate()
  3442. {
  3443. owner->curInt->showingDialog->set(true);
  3444. exit->activate();
  3445. }
  3446. void CBattleResultWindow::deactivate()
  3447. {
  3448. exit->deactivate();
  3449. }
  3450. void CBattleResultWindow::show(SDL_Surface *to)
  3451. {
  3452. //evaluating to
  3453. if(!to)
  3454. to = screen;
  3455. CCS->videoh->update(107, 70, background, false, true);
  3456. SDL_BlitSurface(background, NULL, to, &pos);
  3457. exit->show(to);
  3458. }
  3459. void CBattleResultWindow::bExitf()
  3460. {
  3461. if(LOCPLINT->cb->getStartInfo()->mode == StartInfo::DUEL)
  3462. {
  3463. std::exit(0);
  3464. }
  3465. CPlayerInterface * intTmp = owner->curInt;
  3466. GH.popInts(2); //first - we; second - battle interface
  3467. intTmp->showingDialog->setn(false);
  3468. CCS->videoh->close();
  3469. }
  3470. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner): myInt(owner)
  3471. {
  3472. pos = position;
  3473. SDL_Surface *hhlp = BitmapHandler::loadBitmap("comopbck.bmp", true);
  3474. graphics->blueToPlayersAdv(hhlp, owner->curInt->playerID);
  3475. background = SDL_ConvertSurface(hhlp, screen->format, 0);
  3476. SDL_SetColorKey(background, SDL_SRCCOLORKEY, SDL_MapRGB(background->format,0,255,255));
  3477. SDL_FreeSurface(hhlp);
  3478. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[427].first)(3,CGI->generaltexth->zelp[427].first), CGI->generaltexth->zelp[427].second, false, "sysopchk.def", NULL, 185, 140, false);
  3479. viewGrid->select(owner->curInt->sysOpts.printCellBorders);
  3480. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[428].first)(3,CGI->generaltexth->zelp[428].first), CGI->generaltexth->zelp[428].second, false, "sysopchk.def", NULL, 185, 173, false);
  3481. movementShadow->select(owner->curInt->sysOpts.printStackRange);
  3482. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[429].first)(3,CGI->generaltexth->zelp[429].first), CGI->generaltexth->zelp[429].second, false, "sysopchk.def", NULL, 185, 207, false);
  3483. mouseShadow->select(owner->curInt->sysOpts.printMouseShadow);
  3484. animSpeeds = new CHighlightableButtonsGroup(0);
  3485. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def",188, 309, 1);
  3486. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def",252, 309, 2);
  3487. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",315, 309, 4);
  3488. animSpeeds->select(owner->getAnimSpeed(), 1);
  3489. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  3490. setToDefault = new AdventureMapButton (CGI->generaltexth->zelp[392].first, CGI->generaltexth->zelp[392].second, boost::bind(&CBattleOptionsWindow::bDefaultf,this), 405, 443, "codefaul.def");
  3491. setToDefault->imgs[0]->fixButtonPos();
  3492. exit = new AdventureMapButton (CGI->generaltexth->zelp[393].first, CGI->generaltexth->zelp[393].second, boost::bind(&CBattleOptionsWindow::bExitf,this), 516, 443, "soretrn.def",SDLK_RETURN);
  3493. exit->imgs[0]->fixButtonPos();
  3494. //printing texts to background
  3495. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[392], 242, 32, FONT_BIG, tytulowy, background); //window title
  3496. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[393], 122, 214, FONT_MEDIUM, tytulowy, background); //animation speed
  3497. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 293, FONT_MEDIUM, tytulowy, background); //music volume
  3498. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 359, FONT_MEDIUM, tytulowy, background); //effects' volume
  3499. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[396], 353, 66, FONT_MEDIUM, tytulowy, background); //auto - combat options
  3500. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[397], 353, 265, FONT_MEDIUM, tytulowy, background); //creature info
  3501. //auto - combat options
  3502. CSDL_Ext::printAt(CGI->generaltexth->allTexts[398], 283, 86, FONT_MEDIUM, zwykly, background); //creatures
  3503. CSDL_Ext::printAt(CGI->generaltexth->allTexts[399], 283, 116, FONT_MEDIUM, zwykly, background); //spells
  3504. CSDL_Ext::printAt(CGI->generaltexth->allTexts[400], 283, 146, FONT_MEDIUM, zwykly, background); //catapult
  3505. CSDL_Ext::printAt(CGI->generaltexth->allTexts[151], 283, 176, FONT_MEDIUM, zwykly, background); //ballista
  3506. CSDL_Ext::printAt(CGI->generaltexth->allTexts[401], 283, 206, FONT_MEDIUM, zwykly, background); //first aid tent
  3507. //creature info
  3508. CSDL_Ext::printAt(CGI->generaltexth->allTexts[402], 283, 285, FONT_MEDIUM, zwykly, background); //all stats
  3509. CSDL_Ext::printAt(CGI->generaltexth->allTexts[403], 283, 315, FONT_MEDIUM, zwykly, background); //spells only
  3510. //general options
  3511. CSDL_Ext::printAt(CGI->generaltexth->allTexts[404], 61, 57, FONT_MEDIUM, zwykly, background); //hex grid
  3512. CSDL_Ext::printAt(CGI->generaltexth->allTexts[405], 61, 90, FONT_MEDIUM, zwykly, background); //movement shadow
  3513. CSDL_Ext::printAt(CGI->generaltexth->allTexts[406], 61, 124, FONT_MEDIUM, zwykly, background); //cursor shadow
  3514. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 61, 156, FONT_MEDIUM, zwykly, background); //spellbook animation
  3515. //texts printed
  3516. }
  3517. CBattleOptionsWindow::~CBattleOptionsWindow()
  3518. {
  3519. SDL_FreeSurface(background);
  3520. delete setToDefault;
  3521. delete exit;
  3522. delete viewGrid;
  3523. delete movementShadow;
  3524. delete animSpeeds;
  3525. delete mouseShadow;
  3526. }
  3527. void CBattleOptionsWindow::activate()
  3528. {
  3529. setToDefault->activate();
  3530. exit->activate();
  3531. viewGrid->activate();
  3532. movementShadow->activate();
  3533. animSpeeds->activate();
  3534. mouseShadow->activate();
  3535. }
  3536. void CBattleOptionsWindow::deactivate()
  3537. {
  3538. setToDefault->deactivate();
  3539. exit->deactivate();
  3540. viewGrid->deactivate();
  3541. movementShadow->deactivate();
  3542. animSpeeds->deactivate();
  3543. mouseShadow->deactivate();
  3544. }
  3545. void CBattleOptionsWindow::show(SDL_Surface *to)
  3546. {
  3547. if(!to) //"evaluating" to
  3548. to = screen;
  3549. SDL_BlitSurface(background, NULL, to, &pos);
  3550. setToDefault->show(to);
  3551. exit->show(to);
  3552. viewGrid->show(to);
  3553. movementShadow->show(to);
  3554. animSpeeds->show(to);
  3555. mouseShadow->show(to);
  3556. }
  3557. void CBattleOptionsWindow::bDefaultf()
  3558. {
  3559. }
  3560. void CBattleOptionsWindow::bExitf()
  3561. {
  3562. GH.popIntTotally(this);
  3563. }
  3564. std::string CBattleInterface::SiegeHelper::townTypeInfixes[F_NUMBER] = {"CS", "RM", "TW", "IN", "NC", "DN", "ST", "FR", "EL"};
  3565. CBattleInterface::SiegeHelper::SiegeHelper(const CGTownInstance *siegeTown, const CBattleInterface * _owner)
  3566. : owner(_owner), town(siegeTown)
  3567. {
  3568. for(int g=0; g<ARRAY_COUNT(walls); ++g)
  3569. {
  3570. walls[g] = BitmapHandler::loadBitmap( getSiegeName(g) );
  3571. }
  3572. }
  3573. CBattleInterface::SiegeHelper::~SiegeHelper()
  3574. {
  3575. for(int g=0; g<ARRAY_COUNT(walls); ++g)
  3576. {
  3577. SDL_FreeSurface(walls[g]);
  3578. }
  3579. }
  3580. std::string CBattleInterface::SiegeHelper::getSiegeName(ui16 what, ui16 additInfo) const
  3581. {
  3582. if(what == 2 || what == 3 || what == 8)
  3583. {
  3584. if(additInfo == 3) additInfo = 2;
  3585. }
  3586. char buf[100];
  3587. SDL_itoa(additInfo, buf, 10);
  3588. std::string addit(buf);
  3589. switch(what)
  3590. {
  3591. case 0: //background
  3592. return "SG" + townTypeInfixes[town->town->typeID] + "BACK.BMP";
  3593. case 1: //background wall
  3594. {
  3595. switch(town->town->typeID)
  3596. {
  3597. case 5: case 4: case 1: case 6:
  3598. return "SG" + townTypeInfixes[town->town->typeID] + "TPW1.BMP";
  3599. case 0: case 2: case 3: case 7: case 8:
  3600. return "SG" + townTypeInfixes[town->town->typeID] + "TPWL.BMP";
  3601. default:
  3602. return "";
  3603. }
  3604. }
  3605. case 2: //keep
  3606. return "SG" + townTypeInfixes[town->town->typeID] + "MAN" + addit + ".BMP";
  3607. case 3: //bottom tower
  3608. return "SG" + townTypeInfixes[town->town->typeID] + "TW1" + addit + ".BMP";
  3609. case 4: //bottom wall
  3610. return "SG" + townTypeInfixes[town->town->typeID] + "WA1" + addit + ".BMP";
  3611. case 5: //below gate
  3612. return "SG" + townTypeInfixes[town->town->typeID] + "WA3" + addit + ".BMP";
  3613. case 6: //over gate
  3614. return "SG" + townTypeInfixes[town->town->typeID] + "WA4" + addit + ".BMP";
  3615. case 7: //upper wall
  3616. return "SG" + townTypeInfixes[town->town->typeID] + "WA6" + addit + ".BMP";
  3617. case 8: //upper tower
  3618. return "SG" + townTypeInfixes[town->town->typeID] + "TW2" + addit + ".BMP";
  3619. case 9: //gate
  3620. return "SG" + townTypeInfixes[town->town->typeID] + "DRW" + addit + ".BMP";
  3621. case 10: //gate arch
  3622. return "SG" + townTypeInfixes[town->town->typeID] + "ARCH.BMP";
  3623. case 11: //bottom static wall
  3624. return "SG" + townTypeInfixes[town->town->typeID] + "WA2.BMP";
  3625. case 12: //upper static wall
  3626. return "SG" + townTypeInfixes[town->town->typeID] + "WA5.BMP";
  3627. case 13: //moat
  3628. return "SG" + townTypeInfixes[town->town->typeID] + "MOAT.BMP";
  3629. case 14: //mlip
  3630. return "SG" + townTypeInfixes[town->town->typeID] + "MLIP.BMP";
  3631. case 15: //keep creature cover
  3632. return "SG" + townTypeInfixes[town->town->typeID] + "MANC.BMP";
  3633. case 16: //bottom turret creature cover
  3634. return "SG" + townTypeInfixes[town->town->typeID] + "TW1C.BMP";
  3635. case 17: //upper turret creature cover
  3636. return "SG" + townTypeInfixes[town->town->typeID] + "TW2C.BMP";
  3637. default:
  3638. return "";
  3639. }
  3640. }
  3641. void CBattleInterface::SiegeHelper::printPartOfWall(SDL_Surface * to, int what)
  3642. {
  3643. Point pos = Point(-1, -1);
  3644. switch(what)
  3645. {
  3646. case 1: //background wall
  3647. pos = Point(owner->pos.w + owner->pos.x - walls[1]->w, 55 + owner->pos.y);
  3648. break;
  3649. case 2: //keep
  3650. pos = Point(owner->pos.w + owner->pos.x - walls[2]->w, 154 + owner->pos.y);
  3651. break;
  3652. case 3: //bottom tower
  3653. case 4: //bottom wall
  3654. case 5: //below gate
  3655. case 6: //over gate
  3656. case 7: //upper wall
  3657. case 8: //upper tower
  3658. case 9: //gate
  3659. case 10: //gate arch
  3660. case 11: //bottom static wall
  3661. case 12: //upper static wall
  3662. pos.x = graphics->wallPositions[town->town->typeID][what - 3].x + owner->pos.x;
  3663. pos.y = graphics->wallPositions[town->town->typeID][what - 3].y + owner->pos.y;
  3664. break;
  3665. case 15: //keep creature cover
  3666. pos = Point(owner->pos.w + owner->pos.x - walls[2]->w, 154 + owner->pos.y);
  3667. break;
  3668. case 16: //bottom turret creature cover
  3669. pos.x = graphics->wallPositions[town->town->typeID][0].x + owner->pos.x;
  3670. pos.y = graphics->wallPositions[town->town->typeID][0].y + owner->pos.y;
  3671. break;
  3672. case 17: //upper turret creature cover
  3673. pos.x = graphics->wallPositions[town->town->typeID][5].x + owner->pos.x;
  3674. pos.y = graphics->wallPositions[town->town->typeID][5].y + owner->pos.y;
  3675. break;
  3676. };
  3677. if(pos.x != -1)
  3678. {
  3679. blitAt(walls[what], pos.x, pos.y, to);
  3680. }
  3681. }
  3682. void CStackQueue::update()
  3683. {
  3684. stacksSorted.clear();
  3685. owner->curInt->cb->getStackQueue(stacksSorted, QUEUE_SIZE);
  3686. for (int i = 0; i < QUEUE_SIZE ; i++)
  3687. {
  3688. stackBoxes[i]->setStack(stacksSorted[i]);
  3689. }
  3690. }
  3691. CStackQueue::CStackQueue(bool Embedded, CBattleInterface * _owner)
  3692. :embedded(Embedded), owner(_owner)
  3693. {
  3694. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3695. if(embedded)
  3696. {
  3697. box = NULL;
  3698. bg = NULL;
  3699. pos.w = QUEUE_SIZE * 37;
  3700. pos.h = 32; //height of small creature img
  3701. pos.x = screen->w/2 - pos.w/2;
  3702. pos.y = (screen->h - 600)/2 + 10;
  3703. }
  3704. else
  3705. {
  3706. box = BitmapHandler::loadBitmap("CHRROP.pcx");
  3707. bg = BitmapHandler::loadBitmap("DIBOXPI.pcx");
  3708. pos.w = 600;
  3709. pos.h = bg->h;
  3710. }
  3711. stackBoxes.resize(QUEUE_SIZE);
  3712. for (int i = 0; i < QUEUE_SIZE; i++)
  3713. {
  3714. stackBoxes[i] = new StackBox(box);
  3715. stackBoxes[i]->pos.x += 6 + (embedded ? 37 : 79)*i;
  3716. }
  3717. }
  3718. CStackQueue::~CStackQueue()
  3719. {
  3720. SDL_FreeSurface(box);
  3721. }
  3722. void CStackQueue::showAll( SDL_Surface *to )
  3723. {
  3724. blitBg(to);
  3725. CIntObject::showAll(to);
  3726. }
  3727. void CStackQueue::blitBg( SDL_Surface * to )
  3728. {
  3729. if(bg)
  3730. {
  3731. for (int w = 0; w < pos.w; w += bg->w)
  3732. {
  3733. blitAtLoc(bg, w, 0, to);
  3734. }
  3735. }
  3736. }
  3737. void CStackQueue::StackBox::showAll( SDL_Surface *to )
  3738. {
  3739. assert(my);
  3740. if(bg)
  3741. {
  3742. graphics->blueToPlayersAdv(bg, my->owner);
  3743. //SDL_UpdateRect(bg, 0, 0, 0, 0);
  3744. CSDL_Ext::blit8bppAlphaTo24bpp(bg, NULL, to, &genRect(bg->h, bg->w, pos.x, pos.y));
  3745. //blitAt(bg, pos, to);
  3746. blitAt(graphics->bigImgs[my->getCreature()->idNumber], pos.x +9, pos.y + 1, to);
  3747. printAtMiddleLoc(makeNumberShort(my->count), pos.w/2, pos.h - 12, FONT_MEDIUM, zwykly, to);
  3748. }
  3749. else
  3750. {
  3751. blitAt(graphics->smallImgs[-2], pos, to);
  3752. blitAt(graphics->smallImgs[my->getCreature()->idNumber], pos, to);
  3753. const SDL_Color &ownerColor = (my->owner == 255 ? *graphics->neutralColor : graphics->playerColors[my->owner]);
  3754. CSDL_Ext::drawBorder(to, pos, int3(ownerColor.r, ownerColor.g, ownerColor.b));
  3755. printAtMiddleLoc(makeNumberShort(my->count), pos.w/2, pos.h - 8, FONT_TINY, zwykly, to);
  3756. }
  3757. }
  3758. void CStackQueue::StackBox::setStack( const CStack *nStack )
  3759. {
  3760. my = nStack;
  3761. }
  3762. CStackQueue::StackBox::StackBox(SDL_Surface *BG)
  3763. :my(NULL), bg(BG)
  3764. {
  3765. if(bg)
  3766. {
  3767. pos.w = bg->w;
  3768. pos.h = bg->h;
  3769. }
  3770. else
  3771. {
  3772. pos.w = pos.h = 32;
  3773. }
  3774. pos.y += 2;
  3775. }
  3776. CStackQueue::StackBox::~StackBox()
  3777. {
  3778. }
  3779. void CStackQueue::StackBox::hover( bool on )
  3780. {
  3781. }