CBattleInterface.cpp 123 KB

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