CBattleInterface.cpp 124 KB

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