CBattleInterface.cpp 113 KB

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