CBattleInterface.cpp 125 KB

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