CBattleInterface.cpp 114 KB

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