CBattleInterface.cpp 125 KB

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