CBattleInterface.cpp 124 KB

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