CBattleInterface.cpp 124 KB

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