CBattleInterface.cpp 121 KB

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