CBattleInterface.cpp 124 KB

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