CBattleInterface.cpp 117 KB

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