CBattleInterface.cpp 123 KB

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