CBattleInterface.cpp 125 KB

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