CBattleInterface.cpp 124 KB

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