CBattleInterface.cpp 111 KB

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