CBattleInterface.cpp 120 KB

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