CBattleInterface.cpp 125 KB

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