CBattleInterface.cpp 112 KB

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