CBattleInterface.cpp 122 KB

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