CBattleInterface.cpp 125 KB

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