CBattleInterface.cpp 124 KB

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