CBattleInterface.cpp 117 KB

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