CBattleInterface.cpp 112 KB

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