CBattleInterface.cpp 121 KB

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