CBattleInterface.cpp 125 KB

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