CBattleInterface.cpp 117 KB

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