CBattleInterface.cpp 133 KB

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