CBattleInterface.cpp 121 KB

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