CBattleInterface.cpp 113 KB

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