CPlayerInterface.cpp 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805
  1. #include "stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CBattleInterface.h"
  4. #include "CCallback.h"
  5. #include "CCastleInterface.h"
  6. #include "CCursorHandler.h"
  7. #include "CGameInfo.h"
  8. #include "CHeroWindow.h"
  9. #include "CMessage.h"
  10. #include "CPlayerInterface.h"
  11. //#include "SDL_Extensions.h"
  12. #include "SDL_Extensions.h"
  13. //#include "SDL_framerate.h"
  14. #include "SDL_framerate.h"
  15. #include "client/CConfigHandler.h"
  16. #include "client/CCreatureAnimation.h"
  17. #include "client/Graphics.h"
  18. #include "hch/CArtHandler.h"
  19. #include "hch/CGeneralTextHandler.h"
  20. #include "hch/CHeroHandler.h"
  21. #include "hch/CLodHandler.h"
  22. #include "hch/CObjectHandler.h"
  23. #include "lib/Connection.h"
  24. #include "hch/CSpellHandler.h"
  25. #include "hch/CTownHandler.h"
  26. #include "lib/CondSh.h"
  27. #include "lib/NetPacks.h"
  28. #include "map.h"
  29. #include "mapHandler.h"
  30. #include "timeHandler.h"
  31. #include <boost/lexical_cast.hpp>
  32. #include <boost/algorithm/string.hpp>
  33. #include <boost/algorithm/string/replace.hpp>
  34. #include <boost/assign/std/vector.hpp>
  35. #include <boost/assign/list_of.hpp>
  36. #include <boost/date_time/posix_time/posix_time.hpp>
  37. #include <boost/thread.hpp>
  38. #include <cmath>
  39. #include <queue>
  40. #include <sstream>
  41. #ifdef min
  42. #undef min
  43. #endif
  44. #ifdef max
  45. #undef max
  46. #endif
  47. using namespace boost::assign;
  48. using namespace CSDL_Ext;
  49. extern TTF_Font * GEOR16;
  50. CPlayerInterface * LOCPLINT;
  51. extern std::queue<SDL_Event*> events;
  52. extern boost::mutex eventsM;
  53. void processCommand(const std::string &message, CClient *&client);
  54. struct OCM_HLP_CGIN
  55. {
  56. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  57. {
  58. return (*a.first)<(*b.first);
  59. }
  60. } ocmptwo_cgin ;
  61. void KeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  62. {
  63. if(vstd::contains(assignedKeys,key.keysym.sym))
  64. {
  65. if(key.state == SDL_PRESSED)
  66. clickLeft(true);
  67. else
  68. clickLeft(false);
  69. }
  70. }
  71. void CGarrisonSlot::hover (bool on)
  72. {
  73. Hoverable::hover(on);
  74. if(on)
  75. {
  76. std::string temp;
  77. if(creature)
  78. {
  79. if(owner->highlighted)
  80. {
  81. if(owner->highlighted == this)
  82. {
  83. temp = CGI->generaltexth->tcommands[4];
  84. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  85. }
  86. else if (owner->highlighted->creature == creature)
  87. {
  88. temp = CGI->generaltexth->tcommands[2];
  89. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  90. }
  91. else if (owner->highlighted->creature)
  92. {
  93. temp = CGI->generaltexth->tcommands[7];
  94. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  95. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  96. }
  97. else
  98. {
  99. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  100. tlog2 << "Highlighted set to NULL"<<std::endl;
  101. owner->highlighted = NULL;
  102. }
  103. }
  104. else
  105. {
  106. if(upg)
  107. {
  108. temp = CGI->generaltexth->tcommands[32];
  109. }
  110. else
  111. {
  112. temp = CGI->generaltexth->tcommands[12];
  113. }
  114. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  115. };
  116. }
  117. else
  118. {
  119. if(owner->highlighted)
  120. {
  121. const CArmedInstance *highl = owner->highlighted->getObj();
  122. if( highl->needsLastStack() //we are moving stack from hero's
  123. && highl->army.slots.size() == 1 //it's only stack
  124. && owner->highlighted->upg != upg //we're moving it to the other garrison
  125. )
  126. {
  127. temp = CGI->generaltexth->tcommands[5]; //cannot move last stack!
  128. }
  129. else
  130. {
  131. temp = CGI->generaltexth->tcommands[6];
  132. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  133. }
  134. }
  135. else
  136. {
  137. temp = CGI->generaltexth->tcommands[11];
  138. }
  139. }
  140. LOCPLINT->statusbar->print(temp);
  141. }
  142. else
  143. {
  144. LOCPLINT->statusbar->clear();
  145. }
  146. }
  147. const CArmedInstance * CGarrisonSlot::getObj()
  148. {
  149. return (!upg)?(owner->oup):(owner->odown);
  150. }
  151. StackState* getStackState(const CGObjectInstance *obj, int pos, bool town)
  152. {
  153. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(obj);
  154. if(!h) return NULL;
  155. StackState *pom = new StackState();
  156. pom->currentHealth = 0;
  157. pom->attackBonus = h->getPrimSkillLevel(0);
  158. pom->defenseBonus = h->getPrimSkillLevel(1);
  159. pom->luck = h->getCurrentLuck();
  160. pom->morale = h->getCurrentMorale(pos,town);
  161. pom->speedBonus = h->valOfBonuses(HeroBonus::STACKS_SPEED);
  162. return pom;
  163. }
  164. void CGarrisonSlot::clickRight (tribool down)
  165. {
  166. StackState *pom = getStackState(getObj(),ID, LOCPLINT->topInt() == LOCPLINT->castleInt);
  167. if(down && creature)
  168. {
  169. LOCPLINT->pushInt(new CCreInfoWindow(creature->idNumber,0,count,pom,0,0,NULL));
  170. }
  171. delete pom;
  172. }
  173. void CGarrisonSlot::clickLeft(tribool down)
  174. {
  175. if(owner->ignoreEvent)
  176. {
  177. owner->ignoreEvent = false;
  178. return;
  179. }
  180. if(down)
  181. {
  182. bool refr = false;
  183. if(owner->highlighted)
  184. {
  185. if(owner->highlighted == this) //view info
  186. {
  187. StackState *pom2 = getStackState(getObj(), ID, LOCPLINT->topInt() == LOCPLINT->castleInt);
  188. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(), ID);
  189. CCreInfoWindow *creWindow = NULL;
  190. if(pom.oldID>=0) //upgrade is possible
  191. {
  192. creWindow = new CCreInfoWindow(
  193. creature->idNumber,1,count,pom2,
  194. boost::bind(&CCallback::upgradeCreature,LOCPLINT->cb,getObj(),ID,pom.newID[0]), //bind upgrade function
  195. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),&pom);
  196. }
  197. else
  198. {
  199. creWindow = new CCreInfoWindow(
  200. creature->idNumber,1,count,pom2,0,
  201. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID), NULL);
  202. }
  203. LOCPLINT->pushInt(creWindow);
  204. owner->highlighted = NULL;
  205. show(screen2);
  206. refr = true;
  207. delete pom2;
  208. }
  209. else if((owner->splitting || LOCPLINT->shiftPressed())
  210. && (!creature
  211. || (creature == owner->highlighted->creature))
  212. ) //we want to split
  213. {
  214. owner->p2 = ID; //store the second stack pos
  215. owner->pb = upg;//store the second stack owner (up or down army)
  216. owner->splitting = false;
  217. int totalAmount = owner->highlighted->count;
  218. if(creature)
  219. totalAmount += count;
  220. int last = -1;
  221. if(upg != owner->highlighted->upg) //not splitting within same army
  222. {
  223. if(owner->highlighted->getObj()->army.slots.size() == 1
  224. && owner->highlighted->getObj()->needsLastStack() )
  225. {
  226. last = 0;
  227. }
  228. if(getObj()->army.slots.size() == 1
  229. && getObj()->needsLastStack() )
  230. {
  231. last += 2;
  232. }
  233. }
  234. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  235. LOCPLINT->pushInt(spw);
  236. refr = true;
  237. }
  238. else if(creature != owner->highlighted->creature) //swap
  239. {
  240. LOCPLINT->cb->swapCreatures(
  241. (!upg)?(owner->oup):(owner->odown),
  242. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  243. ID,owner->highlighted->ID);
  244. }
  245. else //merge
  246. {
  247. LOCPLINT->cb->mergeStacks(
  248. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  249. (!upg)?(owner->oup):(owner->odown),
  250. owner->highlighted->ID,ID);
  251. }
  252. }
  253. else //highlight
  254. {
  255. if(creature)
  256. owner->highlighted = this;
  257. show(screen2);
  258. refr = true;
  259. }
  260. if(refr) {hover(false); hover(true); } //to refresh statusbar
  261. }
  262. }
  263. void CGarrisonSlot::activate()
  264. {
  265. if(!active) active=true;
  266. else return;
  267. ClickableL::activate();
  268. ClickableR::activate();
  269. Hoverable::activate();
  270. }
  271. void CGarrisonSlot::deactivate()
  272. {
  273. if(active) active=false;
  274. else return;
  275. ClickableL::deactivate();
  276. ClickableR::deactivate();
  277. Hoverable::deactivate();
  278. }
  279. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  280. {
  281. active = false;
  282. upg = Upg;
  283. count = Count;
  284. ID = IID;
  285. creature = Creature;
  286. pos.x = x;
  287. pos.y = y;
  288. pos.w = 58;
  289. pos.h = 64;
  290. owner = Owner;
  291. }
  292. CGarrisonSlot::~CGarrisonSlot()
  293. {
  294. if(active)
  295. deactivate();
  296. }
  297. void CGarrisonSlot::show(SDL_Surface * to)
  298. {
  299. if(creature)
  300. {
  301. char buf[15];
  302. SDL_itoa(count,buf,10);
  303. blitAt(graphics->bigImgs[creature->idNumber],pos,to);
  304. printToWR(buf,pos.x+56,pos.y+62,GEOR16,zwykly,to);
  305. if((owner->highlighted==this)
  306. || (owner->splitting && owner->highlighted->creature == creature))
  307. {
  308. blitAt(graphics->bigImgs[-1],pos,to);
  309. }
  310. }
  311. else //empty slot
  312. {
  313. SDL_Rect jakis1 = genRect(pos.h,pos.w,owner->offx+ID*(pos.w+owner->interx),owner->offy+upg*(pos.h+owner->intery)),
  314. jakis2 = pos;
  315. SDL_BlitSurface(owner->sur,&jakis1,to,&jakis2);
  316. if(owner->splitting)
  317. blitAt(graphics->bigImgs[-1],pos,to);
  318. }
  319. }
  320. CGarrisonInt::~CGarrisonInt()
  321. {
  322. if(sup)
  323. {
  324. for(size_t i=0;i<sup->size();i++)
  325. {
  326. delete (*sup)[i];
  327. }
  328. delete sup;
  329. }
  330. if(sdown)
  331. {
  332. for(size_t i=0;i<sdown->size();i++)
  333. {
  334. delete (*sdown)[i]; //XXX what about smartpointers? boost or auto_ptr from std
  335. }
  336. delete sdown;
  337. }
  338. }
  339. void CGarrisonInt::show(SDL_Surface * to)
  340. {
  341. if(sup)
  342. {
  343. for(size_t i = 0; i<sup->size(); i++)
  344. {
  345. if((*sup)[i])
  346. {
  347. (*sup)[i]->show(to);
  348. }
  349. }
  350. }
  351. if(sdown)
  352. {
  353. for(size_t i = 0; i<sdown->size(); i++)
  354. {
  355. if((*sdown)[i])
  356. {
  357. (*sdown)[i]->show(to);
  358. }
  359. }
  360. }
  361. }
  362. void CGarrisonInt::deactiveteSlots()
  363. {
  364. if(sup)
  365. {
  366. for(int i = 0; i<sup->size(); i++)
  367. {
  368. if((*sup)[i])
  369. {
  370. (*sup)[i]->deactivate();
  371. }
  372. }
  373. }
  374. if(sdown)
  375. {
  376. for(int i = 0; i<sdown->size(); i++)
  377. {
  378. if((*sdown)[i])
  379. {
  380. (*sdown)[i]->deactivate();
  381. }
  382. }
  383. }
  384. }
  385. void CGarrisonInt::activeteSlots()
  386. {
  387. if(sup)
  388. {
  389. for(int i = 0; i<sup->size(); i++)
  390. {
  391. if((*sup)[i])
  392. {
  393. (*sup)[i]->activate();
  394. }
  395. }
  396. }
  397. if(sdown)
  398. {
  399. for(int i = 0; i<sdown->size(); i++)
  400. {
  401. if((*sdown)[i])
  402. {
  403. (*sdown)[i]->activate();
  404. }
  405. }
  406. }
  407. }
  408. void CGarrisonInt::createSlots()
  409. {
  410. if(set1)
  411. {
  412. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  413. for
  414. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  415. i!=set1->slots.end(); i++)
  416. {
  417. (*sup)[i->first] =
  418. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y,i->first, 0,
  419. &CGI->creh->creatures[i->second.first],i->second.second);
  420. }
  421. for(int i=0; i<sup->size(); i++)
  422. if((*sup)[i] == NULL)
  423. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y,i,0,NULL, 0);
  424. }
  425. if(set2)
  426. {
  427. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  428. for
  429. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  430. i!=set2->slots.end(); i++)
  431. {
  432. (*sdown)[i->first] =
  433. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y + 64 + intery,i->first,1,
  434. &CGI->creh->creatures[i->second.first],i->second.second);
  435. }
  436. for(int i=0; i<sup->size(); i++)
  437. if((*sdown)[i] == NULL)
  438. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y + 64 + intery,i,1, NULL, 0);
  439. }
  440. }
  441. void CGarrisonInt::deleteSlots()
  442. {
  443. if(sup)
  444. {
  445. for(int i = 0; i<sup->size(); i++)
  446. {
  447. if((*sup)[i])
  448. {
  449. delete (*sup)[i];
  450. }
  451. }
  452. delete sup;
  453. sup = NULL;
  454. }
  455. if(sdown)
  456. {
  457. for(int i = 0; i<sdown->size(); i++)
  458. {
  459. if((*sdown)[i])
  460. {
  461. delete (*sdown)[i];
  462. }
  463. }
  464. delete sdown;
  465. sdown = NULL;
  466. }
  467. }
  468. void CGarrisonInt::recreateSlots()
  469. {
  470. splitting = false;
  471. highlighted = NULL;
  472. if(active)
  473. {
  474. deactiveteSlots();
  475. }
  476. deleteSlots();
  477. createSlots();
  478. if(active)
  479. {
  480. //ignoreEvent = true;
  481. activeteSlots();
  482. //show(screen2);
  483. }
  484. }
  485. void CGarrisonInt::splitClick()
  486. {
  487. if(!highlighted)
  488. return;
  489. splitting = !splitting;
  490. show(screen2);
  491. }
  492. void CGarrisonInt::splitStacks(int am2)
  493. {
  494. LOCPLINT->cb->splitStack(
  495. (highlighted->upg)?(odown):(oup),
  496. (pb)?(odown):(oup),
  497. highlighted->ID,
  498. p2,
  499. am2);
  500. }
  501. CGarrisonInt::CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *&pomsur, int OX, int OY, const CArmedInstance *s1,
  502. const CArmedInstance *s2)
  503. :interx(inx),intery(iny),sur(pomsur),highlighted(NULL),sup(NULL),sdown(NULL),oup(s1),odown(s2),
  504. offx(OX),offy(OY)
  505. {
  506. active = false;
  507. splitting = false;
  508. set1 = LOCPLINT->cb->getGarrison(s1);
  509. set2 = LOCPLINT->cb->getGarrison(s2);
  510. ignoreEvent = false;
  511. update = true;
  512. pos.x=(x);
  513. pos.y=(y);
  514. pos.w=(58);
  515. pos.h=(64);
  516. createSlots();
  517. }
  518. void CGarrisonInt::activate()
  519. {
  520. active = true;
  521. if(sup)
  522. {
  523. for(int i = 0; i<sup->size(); i++)
  524. if((*sup)[i])
  525. (*sup)[i]->activate();
  526. }
  527. if(sdown)
  528. {
  529. for(int i = 0; i<sdown->size(); i++)
  530. if((*sdown)[i])
  531. (*sdown)[i]->activate();
  532. }
  533. }
  534. void CGarrisonInt::deactivate()
  535. {
  536. active = false;
  537. deactiveteSlots();
  538. }
  539. CInfoWindow::CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons)
  540. {
  541. ID = -1;
  542. for(int i=0;i<Buttons.size();i++)
  543. {
  544. buttons.push_back(new AdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first));
  545. buttons[i]->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  546. }
  547. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  548. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  549. for(int i=0;i<comps.size();i++)
  550. {
  551. components.push_back(comps[i]);
  552. }
  553. CMessage::drawIWindow(this,text,player,charperline);
  554. }
  555. CInfoWindow::CInfoWindow()
  556. {
  557. ID = -1;
  558. }
  559. void CInfoWindow::close()
  560. {
  561. LOCPLINT->popIntTotally(this);
  562. LOCPLINT->showingDialog->setn(false);
  563. }
  564. void CInfoWindow::show(SDL_Surface * to)
  565. {
  566. CSimpleWindow::show(to);
  567. for(int i=0;i<buttons.size();i++)
  568. buttons[i]->show(to);
  569. }
  570. CInfoWindow::~CInfoWindow()
  571. {
  572. if(delComps)
  573. {
  574. for (int i=0;i<components.size();i++)
  575. delete components[i];
  576. }
  577. for(int i=0;i<buttons.size();i++)
  578. delete buttons[i];
  579. }
  580. void CInfoWindow::activate()
  581. {
  582. for (int i=0;i<components.size();i++)
  583. components[i]->activate();
  584. for(int i=0;i<buttons.size();i++)
  585. buttons[i]->activate();
  586. }
  587. void CInfoWindow::deactivate()
  588. {
  589. for (int i=0;i<components.size();i++)
  590. components[i]->deactivate();
  591. for(int i=0;i<buttons.size();i++)
  592. buttons[i]->deactivate();
  593. }
  594. void CRClickPopup::clickRight (tribool down)
  595. {
  596. if(down)
  597. return;
  598. close();
  599. }
  600. void CRClickPopup::activate()
  601. {
  602. ClickableR::activate();
  603. }
  604. void CRClickPopup::deactivate()
  605. {
  606. ClickableR::deactivate();
  607. }
  608. void CRClickPopup::close()
  609. {
  610. LOCPLINT->popIntTotally(this);
  611. }
  612. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  613. :bitmap(Bitmap),free(Free)
  614. {
  615. pos.x = x;
  616. pos.y = y;
  617. pos.h = bitmap->h;
  618. pos.w = bitmap->w;
  619. }
  620. void CInfoPopup::close()
  621. {
  622. if(free)
  623. SDL_FreeSurface(bitmap);
  624. LOCPLINT->popIntTotally(this);
  625. }
  626. void CInfoPopup::show(SDL_Surface * to)
  627. {
  628. blitAt(bitmap,pos.x,pos.y,to);
  629. }
  630. void SComponent::init(Etype Type, int Subtype, int Val)
  631. {
  632. std::ostringstream oss;
  633. switch (Type)
  634. {
  635. case artifact:
  636. description = CGI->arth->artifacts[Subtype].Description();
  637. subtitle = CGI->arth->artifacts[Subtype].Name();
  638. break;
  639. case primskill:
  640. description = CGI->generaltexth->arraytxt[2+Subtype];
  641. oss << ((Val>0)?("+"):("-")) << Val << " " << CGI->generaltexth->primarySkillNames[Subtype];
  642. subtitle = oss.str();
  643. break;
  644. case secskill44: case secskill:
  645. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  646. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  647. break;
  648. case resource:
  649. description = CGI->generaltexth->allTexts[242];
  650. oss << Val;
  651. subtitle = oss.str();
  652. break;
  653. case spell:
  654. description = CGI->spellh->spells[Subtype].descriptions[Val];
  655. subtitle = CGI->spellh->spells[Subtype].name;
  656. break;
  657. case experience:
  658. description = CGI->generaltexth->allTexts[241];
  659. oss << Val ;
  660. if(Subtype && Val==1)
  661. {
  662. subtitle = CGI->generaltexth->allTexts[442];
  663. }
  664. else
  665. {
  666. subtitle = oss.str();
  667. }
  668. break;
  669. }
  670. type = Type;
  671. subtype = Subtype;
  672. val = Val;
  673. SDL_Surface * temp = this->getImg();
  674. if(!temp)
  675. {
  676. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  677. return;
  678. }
  679. pos.w = temp->w;
  680. pos.h = temp->h;
  681. }
  682. SComponent::SComponent(Etype Type, int Subtype, int Val)
  683. {
  684. init(Type,Subtype,Val);
  685. }
  686. SComponent::SComponent(const Component &c)
  687. {
  688. if(c.id==5)
  689. init(experience,c.subtype,c.val);
  690. else if(c.id == Component::SPELL)
  691. init(spell,c.subtype,c.val);
  692. else
  693. init((Etype)c.id,c.subtype,c.val);
  694. if(c.id==2 && c.when==-1)
  695. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  696. }
  697. void SComponent::show(SDL_Surface * to)
  698. {
  699. blitAt(getImg(),pos.x,pos.y,to);
  700. }
  701. SDL_Surface * SComponent::getImg()
  702. {
  703. switch (type)
  704. {
  705. case artifact:
  706. return graphics->artDefs->ourImages[subtype].bitmap;
  707. break;
  708. case primskill:
  709. return graphics->pskillsb->ourImages[subtype].bitmap;
  710. break;
  711. case secskill44:
  712. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  713. break;
  714. case secskill:
  715. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  716. break;
  717. case resource:
  718. return graphics->resources->ourImages[subtype].bitmap;
  719. break;
  720. case experience:
  721. return graphics->pskillsb->ourImages[4].bitmap;
  722. break;
  723. case morale:
  724. return graphics->morale82->ourImages[val+3].bitmap;
  725. break;
  726. case luck:
  727. return graphics->luck82->ourImages[val+3].bitmap;
  728. break;
  729. case spell:
  730. return graphics->spellscr->ourImages[subtype].bitmap;
  731. break;
  732. }
  733. return NULL;
  734. }
  735. void SComponent::clickRight (tribool down)
  736. {
  737. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  738. }
  739. void SComponent::activate()
  740. {
  741. ClickableR::activate();
  742. }
  743. void SComponent::deactivate()
  744. {
  745. ClickableR::deactivate();
  746. }
  747. void CSelectableComponent::clickLeft(tribool down)
  748. {
  749. if (down)
  750. {
  751. if(onSelect)
  752. onSelect();
  753. }
  754. }
  755. void CSelectableComponent::init(SDL_Surface * Border)
  756. {
  757. SDL_Surface * symb = SComponent::getImg();
  758. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  759. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  760. blitAt(symb,1,1,myBitmap);
  761. if (Border) //use custom border
  762. {
  763. border = Border;
  764. customB = true;
  765. }
  766. else //we need to draw border
  767. {
  768. customB = false;
  769. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  770. SDL_FillRect(border,NULL,0x00FFFF);
  771. for (int i=0;i<border->w;i++)
  772. {
  773. SDL_PutPixelWithoutRefresh(border,i,0,239,215,123);
  774. SDL_PutPixelWithoutRefresh(border,i,(border->h)-1,239,215,123);
  775. }
  776. for (int i=0;i<border->h;i++)
  777. {
  778. SDL_PutPixelWithoutRefresh(border,0,i,239,215,123);
  779. SDL_PutPixelWithoutRefresh(border,(border->w)-1,i,239,215,123);
  780. }
  781. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  782. }
  783. selected = false;
  784. }
  785. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  786. :SComponent(c),onSelect(OnSelect)
  787. {
  788. init(Border);
  789. }
  790. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  791. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  792. {
  793. init(Border);
  794. }
  795. CSelectableComponent::~CSelectableComponent()
  796. {
  797. SDL_FreeSurface(myBitmap);
  798. if (!customB)
  799. SDL_FreeSurface(border);
  800. }
  801. void CSelectableComponent::activate()
  802. {
  803. KeyInterested::activate();
  804. SComponent::activate();
  805. ClickableL::activate();
  806. }
  807. void CSelectableComponent::deactivate()
  808. {
  809. KeyInterested::deactivate();
  810. SComponent::deactivate();
  811. ClickableL::deactivate();
  812. }
  813. SDL_Surface * CSelectableComponent::getImg()
  814. {
  815. return myBitmap;
  816. }
  817. void CSelectableComponent::select(bool on)
  818. {
  819. if(on != selected)
  820. {
  821. SDL_FillRect(myBitmap,NULL,0x000000);
  822. blitAt(SComponent::getImg(),1,1,myBitmap);
  823. if (on)
  824. {
  825. blitAt(border,0,0,myBitmap);
  826. }
  827. selected = on;
  828. return;
  829. }
  830. else
  831. {
  832. return;
  833. }
  834. }
  835. void CSelectableComponent::show(SDL_Surface * to)
  836. {
  837. blitAt(myBitmap,pos.x,pos.y,to);
  838. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+14,GEOR13,12,zwykly,to);
  839. }
  840. void CSimpleWindow::show(SDL_Surface * to)
  841. {
  842. blitAt(bitmap,pos.x,pos.y,to);
  843. }
  844. CSimpleWindow::~CSimpleWindow()
  845. {
  846. if (bitmap)
  847. {
  848. SDL_FreeSurface(bitmap);
  849. bitmap=NULL;
  850. }
  851. }
  852. void CSelWindow::selectionChange(unsigned to)
  853. {
  854. for (unsigned i=0;i<components.size();i++)
  855. {
  856. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  857. if (!pom)
  858. continue;
  859. pom->select(i==to);
  860. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  861. }
  862. }
  863. CSelWindow::CSelWindow(const std::string &text, int player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, int askID)
  864. {
  865. ID = askID;
  866. for(int i=0;i<Buttons.size();i++)
  867. {
  868. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  869. if(!i && askID >= 0)
  870. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  871. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  872. }
  873. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  874. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  875. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  876. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  877. for(int i=0;i<comps.size();i++)
  878. {
  879. components.push_back(comps[i]);
  880. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  881. if(i<9)
  882. comps[i]->assignedKeys.insert(SDLK_1+i);
  883. }
  884. CMessage::drawIWindow(this,text,player,charperline);
  885. }
  886. void CSelWindow::madeChoice()
  887. {
  888. if(ID < 0)
  889. return;
  890. int ret = -1;
  891. for (int i=0;i<components.size();i++)
  892. {
  893. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  894. {
  895. ret = i;
  896. }
  897. }
  898. LOCPLINT->cb->selectionMade(ret+1,ID);
  899. }
  900. CButtonBase::CButtonBase()
  901. {
  902. bitmapOffset = 0;
  903. curimg=0;
  904. type=-1;
  905. abs=false;
  906. active=false;
  907. notFreeButton = false;
  908. ourObj=NULL;
  909. state=0;
  910. }
  911. CButtonBase::~CButtonBase()
  912. {
  913. if(notFreeButton)
  914. return;
  915. for(int i =0; i<imgs.size();i++)
  916. for(int j=0;j<imgs[i].size();j++)
  917. SDL_FreeSurface(imgs[i][j]);
  918. }
  919. void CButtonBase::show(SDL_Surface * to)
  920. {
  921. int img = std::min(state+bitmapOffset,int(imgs[curimg].size()-1));
  922. if (abs)
  923. {
  924. blitAt(imgs[curimg][img],pos.x,pos.y,to);
  925. }
  926. else
  927. {
  928. blitAt(imgs[curimg][img],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
  929. }
  930. }
  931. ClickableL::ClickableL()
  932. {
  933. pressedL=false;
  934. }
  935. ClickableL::~ClickableL()
  936. {
  937. }
  938. void ClickableL::clickLeft(tribool down)
  939. {
  940. if (down)
  941. pressedL=true;
  942. else
  943. pressedL=false;
  944. }
  945. void ClickableL::activate()
  946. {
  947. LOCPLINT->lclickable.push_front(this);
  948. }
  949. void ClickableL::deactivate()
  950. {
  951. LOCPLINT->lclickable.erase
  952. (std::find(LOCPLINT->lclickable.begin(),LOCPLINT->lclickable.end(),this));
  953. }
  954. ClickableR::ClickableR()
  955. {
  956. pressedR=false;
  957. }
  958. ClickableR::~ClickableR()
  959. {}
  960. void ClickableR::clickRight(tribool down)
  961. {
  962. if (down)
  963. pressedR=true;
  964. else
  965. pressedR=false;
  966. }
  967. void ClickableR::activate()
  968. {
  969. LOCPLINT->rclickable.push_front(this);
  970. }
  971. void ClickableR::deactivate()
  972. {
  973. LOCPLINT->rclickable.erase(std::find(LOCPLINT->rclickable.begin(),LOCPLINT->rclickable.end(),this));
  974. }
  975. //ClickableR
  976. Hoverable::~Hoverable()
  977. {}
  978. void Hoverable::activate()
  979. {
  980. LOCPLINT->hoverable.push_front(this);
  981. }
  982. void Hoverable::deactivate()
  983. {
  984. LOCPLINT->hoverable.erase(std::find(LOCPLINT->hoverable.begin(),LOCPLINT->hoverable.end(),this));
  985. }
  986. void Hoverable::hover(bool on)
  987. {
  988. hovered=on;
  989. }
  990. //Hoverable
  991. KeyInterested::~KeyInterested()
  992. {}
  993. void KeyInterested::activate()
  994. {
  995. LOCPLINT->keyinterested.push_front(this);
  996. }
  997. void KeyInterested::deactivate()
  998. {
  999. LOCPLINT->
  1000. keyinterested.erase(std::find(LOCPLINT->keyinterested.begin(),LOCPLINT->keyinterested.end(),this));
  1001. }
  1002. //KeyInterested
  1003. void MotionInterested::activate()
  1004. {
  1005. LOCPLINT->motioninterested.push_front(this);
  1006. }
  1007. void MotionInterested::deactivate()
  1008. {
  1009. LOCPLINT->
  1010. motioninterested.erase(std::find(LOCPLINT->motioninterested.begin(),LOCPLINT->motioninterested.end(),this));
  1011. }
  1012. void TimeInterested::activate()
  1013. {
  1014. LOCPLINT->timeinterested.push_back(this);
  1015. }
  1016. void TimeInterested::deactivate()
  1017. {
  1018. LOCPLINT->
  1019. timeinterested.erase(std::find(LOCPLINT->timeinterested.begin(),LOCPLINT->timeinterested.end(),this));
  1020. }
  1021. CPlayerInterface::CPlayerInterface(int Player, int serial)
  1022. {
  1023. LOCPLINT = this;
  1024. curAction = NULL;
  1025. playerID=Player;
  1026. serialID=serial;
  1027. human=true;
  1028. castleInt = NULL;
  1029. adventureInt = NULL;
  1030. battleInt = NULL;
  1031. pim = new boost::recursive_mutex;
  1032. showingDialog = new CondSh<bool>(false);
  1033. heroMoveSpeed = 2;
  1034. mapScrollingSpeed = 2;
  1035. //initializing framerate keeper
  1036. mainFPSmng = new FPSmanager;
  1037. SDL_initFramerate(mainFPSmng);
  1038. SDL_setFramerate(mainFPSmng, 48);
  1039. //framerate keeper initialized
  1040. cingconsole = new CInGameConsole;
  1041. }
  1042. CPlayerInterface::~CPlayerInterface()
  1043. {
  1044. delete pim;
  1045. delete showingDialog;
  1046. delete mainFPSmng;
  1047. delete adventureInt;
  1048. delete cingconsole;
  1049. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!= graphics->heroWins.end(); i++)
  1050. SDL_FreeSurface(i->second);
  1051. for(std::map<int,SDL_Surface*>::iterator i=graphics->townWins.begin(); i!= graphics->townWins.end(); i++)
  1052. SDL_FreeSurface(i->second);
  1053. }
  1054. void CPlayerInterface::init(ICallback * CB)
  1055. {
  1056. cb = dynamic_cast<CCallback*>(CB);
  1057. adventureInt = new CAdvMapInt(playerID);
  1058. std::vector<const CGTownInstance*> tt = cb->getTownsInfo(false);
  1059. for(int i=0;i<tt.size();i++)
  1060. {
  1061. SDL_Surface * pom = infoWin(tt[i]);
  1062. graphics->townWins.insert(std::pair<int,SDL_Surface*>(tt[i]->id,pom));
  1063. }
  1064. }
  1065. void CPlayerInterface::yourTurn()
  1066. {
  1067. try
  1068. {
  1069. LOCPLINT = this;
  1070. makingTurn = true;
  1071. static int autosaveCount = 0;
  1072. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  1073. autosaveCount %= 5;
  1074. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!=graphics->heroWins.end();i++) //redraw hero infoboxes
  1075. SDL_FreeSurface(i->second);
  1076. graphics->heroWins.clear();
  1077. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  1078. for(int i=0;i<hh.size();i++)
  1079. {
  1080. SDL_Surface * pom = infoWin(hh[i]);
  1081. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  1082. }
  1083. adventureInt->infoBar.newDay(cb->getDate(1));
  1084. //select first hero if available.
  1085. //TODO: check if hero is slept
  1086. if(adventureInt->heroList.items.size())
  1087. adventureInt->select(adventureInt->heroList.items[0].first);
  1088. else
  1089. adventureInt->select(adventureInt->townList.items[0]);
  1090. adventureInt->showAll(screen);
  1091. pushInt(adventureInt);
  1092. timeHandler th;
  1093. th.getDif();
  1094. while(makingTurn) // main loop
  1095. {
  1096. updateWater();
  1097. pim->lock();
  1098. //if there are any waiting dialogs, show them
  1099. if(dialogs.size() && !showingDialog->get())
  1100. {
  1101. showingDialog->set(true);
  1102. pushInt(dialogs.front());
  1103. dialogs.pop_front();
  1104. }
  1105. int tv = th.getDif();
  1106. std::list<TimeInterested*> hlp = timeinterested;
  1107. for (std::list<TimeInterested*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1108. {
  1109. if(!vstd::contains(timeinterested,*i)) continue;
  1110. if ((*i)->toNextTick>=0)
  1111. (*i)->toNextTick-=tv;
  1112. if ((*i)->toNextTick<0)
  1113. (*i)->tick();
  1114. }
  1115. while(true)
  1116. {
  1117. SDL_Event *ev = NULL;
  1118. {
  1119. boost::unique_lock<boost::mutex> lock(eventsM);
  1120. if(!events.size())
  1121. {
  1122. break;
  1123. }
  1124. else
  1125. {
  1126. ev = events.front();
  1127. events.pop();
  1128. }
  1129. }
  1130. handleEvent(ev);
  1131. delete ev;
  1132. }
  1133. if(objsToBlit.size() > 1)
  1134. blitAt(screen2,0,0,screen); //blit background
  1135. objsToBlit.back()->show(screen); //blit active interface/window
  1136. CGI->curh->draw1();
  1137. CSDL_Ext::update(screen);
  1138. CGI->curh->draw2();
  1139. pim->unlock();
  1140. SDL_framerateDelay(mainFPSmng);
  1141. }
  1142. popInt(adventureInt);
  1143. cb->endTurn();
  1144. } HANDLE_EXCEPTION
  1145. }
  1146. inline void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  1147. {
  1148. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  1149. for(int h=0; h<hlp.objects.size(); ++h)
  1150. if(hlp.objects[h].first->id==hid)
  1151. {
  1152. hlp.objects[h].second = r;
  1153. return;
  1154. }
  1155. }
  1156. inline void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  1157. {
  1158. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  1159. for(int h=0; h<hlp.objects.size(); ++h)
  1160. if(hlp.objects[h].first->id==hid)
  1161. {
  1162. hlp.objects.erase(hlp.objects.begin()+h);
  1163. return;
  1164. }
  1165. }
  1166. int getDir(int3 src, int3 dst)
  1167. {
  1168. int ret = -1;
  1169. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  1170. {
  1171. ret = 1;
  1172. }
  1173. else if(dst.x == src.x && dst.y+1 == src.y) //t
  1174. {
  1175. ret = 2;
  1176. }
  1177. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  1178. {
  1179. ret = 3;
  1180. }
  1181. else if(dst.x-1 == src.x && dst.y == src.y) //r
  1182. {
  1183. ret = 4;
  1184. }
  1185. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  1186. {
  1187. ret = 5;
  1188. }
  1189. else if(dst.x == src.x && dst.y-1 == src.y) //b
  1190. {
  1191. ret = 6;
  1192. }
  1193. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  1194. {
  1195. ret = 7;
  1196. }
  1197. else if(dst.x+1 == src.x && dst.y == src.y) //l
  1198. {
  1199. ret = 8;
  1200. }
  1201. return ret;
  1202. }
  1203. void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
  1204. {
  1205. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1206. adventureInt->centerOn(details.ho->pos); //actualizing screen pos
  1207. adventureInt->minimap.draw(screen2);
  1208. if(details.style>0 || details.src == details.dst)
  1209. return;
  1210. //initializing objects and performing first step of move
  1211. const CGHeroInstance * ho = details.ho; //object representing this hero
  1212. int3 hp = details.src;
  1213. if (!details.successful) //hero failed to move
  1214. {
  1215. if(ho->movement > 50)
  1216. ho->moveDir = getDir(details.src,details.dst);
  1217. ho->isStanding = true;
  1218. //adventureInt->heroList.draw();
  1219. if (adventureInt->terrain.currentPath && ho->movement>145) //TODO: better condition on movement - check cost of endtile
  1220. {
  1221. delete adventureInt->terrain.currentPath;
  1222. adventureInt->terrain.currentPath = NULL;
  1223. adventureInt->heroList.items[adventureInt->heroList.getPosOfHero(ho)].second = NULL;
  1224. }
  1225. return;
  1226. }
  1227. if (adventureInt->terrain.currentPath) //&& hero is moving
  1228. {
  1229. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1230. }
  1231. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1232. {
  1233. ho->moveDir = 1;
  1234. ho->isStanding = false;
  1235. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1236. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1237. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1238. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1239. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1240. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1241. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1242. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1243. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1244. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1245. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1246. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1247. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1248. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1249. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1250. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1251. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1252. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1253. }
  1254. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1255. {
  1256. ho->moveDir = 2;
  1257. ho->isStanding = false;
  1258. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1259. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1260. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1261. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1262. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1263. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1264. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1265. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1266. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1267. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1268. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1269. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1270. }
  1271. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1272. {
  1273. ho->moveDir = 3;
  1274. ho->isStanding = false;
  1275. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1276. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1277. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1278. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1279. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1280. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1281. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1282. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1283. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1284. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1285. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1286. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1287. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1288. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1289. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1290. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1291. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1292. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1293. }
  1294. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1295. {
  1296. ho->moveDir = 4;
  1297. ho->isStanding = false;
  1298. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1299. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1300. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1301. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1302. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1303. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1304. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1305. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1306. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1307. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1308. }
  1309. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1310. {
  1311. ho->moveDir = 5;
  1312. ho->isStanding = false;
  1313. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1314. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1315. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1316. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1317. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1318. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1319. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1320. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1321. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1322. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1323. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1324. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1325. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1326. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1327. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1328. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1329. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1330. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1331. }
  1332. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1333. {
  1334. ho->moveDir = 6;
  1335. ho->isStanding = false;
  1336. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1337. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1338. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1339. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1340. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1341. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1342. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1343. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1344. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1345. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1346. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1347. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1348. }
  1349. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1350. {
  1351. ho->moveDir = 7;
  1352. ho->isStanding = false;
  1353. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1354. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1355. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1356. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1357. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1358. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1359. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1360. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1361. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1362. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1363. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1364. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1365. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1366. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1367. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1368. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1369. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1370. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1371. }
  1372. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1373. {
  1374. ho->moveDir = 8;
  1375. ho->isStanding = false;
  1376. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1377. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1378. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1379. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1380. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1381. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1382. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1383. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1384. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1385. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1386. }
  1387. //first initializing done
  1388. SDL_framerateDelay(mainFPSmng); // after first move
  1389. //main moving
  1390. for(int i=1; i<32; i+=2*heroMoveSpeed)
  1391. {
  1392. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1393. {
  1394. //setting advmap shift
  1395. adventureInt->terrain.moveX = i-32;
  1396. adventureInt->terrain.moveY = i-32;
  1397. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1398. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1399. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1400. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1401. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1402. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1403. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1404. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1405. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1406. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1407. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1408. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1409. }
  1410. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1411. {
  1412. //setting advmap shift
  1413. adventureInt->terrain.moveY = i-32;
  1414. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1415. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1416. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1417. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1418. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1419. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1420. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1421. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1422. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1423. }
  1424. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1425. {
  1426. //setting advmap shift
  1427. adventureInt->terrain.moveX = -i+32;
  1428. adventureInt->terrain.moveY = i-32;
  1429. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1430. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1431. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1432. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1433. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1434. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1435. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1436. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1437. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1438. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1439. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1440. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1441. }
  1442. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1443. {
  1444. //setting advmap shift
  1445. adventureInt->terrain.moveX = -i+32;
  1446. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1447. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1448. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1449. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1450. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1451. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1452. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1453. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1454. }
  1455. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1456. {
  1457. //setting advmap shift
  1458. adventureInt->terrain.moveX = -i+32;
  1459. adventureInt->terrain.moveY = -i+32;
  1460. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1461. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1462. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1463. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1464. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1465. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1466. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1467. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1468. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1469. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1470. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1471. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1472. }
  1473. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1474. {
  1475. //setting advmap shift
  1476. adventureInt->terrain.moveY = -i+32;
  1477. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1478. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1479. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1480. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1481. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1482. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1483. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1484. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1485. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1486. }
  1487. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1488. {
  1489. //setting advmap shift
  1490. adventureInt->terrain.moveX = i-32;
  1491. adventureInt->terrain.moveY = -i+32;
  1492. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1493. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1494. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1495. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1496. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1497. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1498. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1499. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1500. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1501. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1502. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1503. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1504. }
  1505. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1506. {
  1507. //setting advmap shift
  1508. adventureInt->terrain.moveX = i-32;
  1509. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1510. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1511. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1512. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1513. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1514. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1515. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1516. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1517. }
  1518. adventureInt->updateScreen = true;
  1519. adventureInt->show(screen);
  1520. //LOCPLINT->adventureInt->show(); //updating screen
  1521. CSDL_Ext::update(screen);
  1522. SDL_Delay(5);
  1523. SDL_framerateDelay(mainFPSmng); //for animation purposes
  1524. } //for(int i=1; i<32; i+=4)
  1525. //main moving done
  1526. //finishing move
  1527. //restoring adventureInt->terrain.move*
  1528. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1529. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1530. {
  1531. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1532. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1533. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1534. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1535. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1536. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1537. }
  1538. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1539. {
  1540. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1541. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1542. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1543. }
  1544. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1545. {
  1546. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1547. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1548. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1549. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1550. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1551. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1552. }
  1553. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1554. {
  1555. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1556. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1557. }
  1558. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1559. {
  1560. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1561. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1562. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1563. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1564. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1565. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1566. }
  1567. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1568. {
  1569. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1570. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1571. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1572. }
  1573. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1574. {
  1575. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1576. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1577. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1578. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1579. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1580. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1581. }
  1582. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1583. {
  1584. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1585. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1586. }
  1587. //restoring good rects
  1588. subRect(details.dst.x-2, details.dst.y-1, details.dst.z, genRect(32, 32, 0, 0), ho->id);
  1589. subRect(details.dst.x-1, details.dst.y-1, details.dst.z, genRect(32, 32, 32, 0), ho->id);
  1590. subRect(details.dst.x, details.dst.y-1, details.dst.z, genRect(32, 32, 64, 0), ho->id);
  1591. subRect(details.dst.x-2, details.dst.y, details.dst.z, genRect(32, 32, 0, 32), ho->id);
  1592. subRect(details.dst.x-1, details.dst.y, details.dst.z, genRect(32, 32, 32, 32), ho->id);
  1593. subRect(details.dst.x, details.dst.y, details.dst.z, genRect(32, 32, 64, 32), ho->id);
  1594. //restoring good order of objects
  1595. std::stable_sort(CGI->mh->ttiles[details.dst.x-2][details.dst.y-1][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-2][details.dst.y-1][details.dst.z].objects.end(), ocmptwo_cgin);
  1596. std::stable_sort(CGI->mh->ttiles[details.dst.x-1][details.dst.y-1][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-1][details.dst.y-1][details.dst.z].objects.end(), ocmptwo_cgin);
  1597. std::stable_sort(CGI->mh->ttiles[details.dst.x][details.dst.y-1][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x][details.dst.y-1][details.dst.z].objects.end(), ocmptwo_cgin);
  1598. std::stable_sort(CGI->mh->ttiles[details.dst.x-2][details.dst.y][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-2][details.dst.y][details.dst.z].objects.end(), ocmptwo_cgin);
  1599. std::stable_sort(CGI->mh->ttiles[details.dst.x-1][details.dst.y][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-1][details.dst.y][details.dst.z].objects.end(), ocmptwo_cgin);
  1600. std::stable_sort(CGI->mh->ttiles[details.dst.x][details.dst.y][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x][details.dst.y][details.dst.z].objects.end(), ocmptwo_cgin);
  1601. ho->isStanding = true;
  1602. //move finished
  1603. adventureInt->minimap.draw(screen2);
  1604. adventureInt->heroList.updateMove(ho);
  1605. //check if user cancelled movement
  1606. {
  1607. boost::unique_lock<boost::mutex> un(eventsM);
  1608. while(events.size())
  1609. {
  1610. SDL_Event *ev = events.front();
  1611. events.pop();
  1612. switch(ev->type)
  1613. {
  1614. case SDL_MOUSEBUTTONDOWN:
  1615. stillMoveHero = false;
  1616. break;
  1617. case SDL_KEYDOWN:
  1618. if(ev->key.keysym.sym < SDLK_F1)
  1619. stillMoveHero = false;
  1620. break;
  1621. }
  1622. delete ev;
  1623. }
  1624. }
  1625. }
  1626. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  1627. {
  1628. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1629. graphics->heroWins.erase(hero->ID);
  1630. adventureInt->heroList.updateHList(hero);
  1631. }
  1632. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  1633. {
  1634. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1635. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  1636. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  1637. adventureInt->heroList.updateHList();
  1638. }
  1639. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  1640. {
  1641. castleInt = new CCastleInterface(town);
  1642. LOCPLINT->pushInt(castleInt);
  1643. }
  1644. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  1645. {
  1646. if (specific)
  1647. {
  1648. switch (specific->ID)
  1649. {
  1650. case HEROI_TYPE:
  1651. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  1652. break;
  1653. case TOWNI_TYPE:
  1654. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  1655. break;
  1656. default:
  1657. return NULL;
  1658. break;
  1659. }
  1660. }
  1661. else
  1662. {
  1663. switch (adventureInt->selection->ID)
  1664. {
  1665. case HEROI_TYPE:
  1666. {
  1667. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  1668. return graphics->drawHeroInfoWin(curh);
  1669. }
  1670. case TOWNI_TYPE:
  1671. {
  1672. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  1673. }
  1674. default:
  1675. tlog1 << "Strange... selection is neither hero nor town\n";
  1676. return NULL;
  1677. }
  1678. }
  1679. }
  1680. void CPlayerInterface::handleMouseMotion(SDL_Event *sEvent)
  1681. {
  1682. //sending active, hovered hoverable objects hover() call
  1683. std::vector<Hoverable*> hlp;
  1684. for(std::list<Hoverable*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  1685. {
  1686. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1687. {
  1688. if (!(*i)->hovered)
  1689. hlp.push_back((*i));
  1690. }
  1691. else if ((*i)->hovered)
  1692. {
  1693. (*i)->hover(false);
  1694. }
  1695. }
  1696. for(int i=0; i<hlp.size();i++)
  1697. hlp[i]->hover(true);
  1698. //sending active, MotionInterested objects mouseMoved() call
  1699. std::list<MotionInterested*> miCopy = motioninterested;
  1700. for(std::list<MotionInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  1701. {
  1702. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1703. {
  1704. (*i)->mouseMoved(sEvent->motion);
  1705. }
  1706. }
  1707. //adventure map scrolling with mouse
  1708. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL])
  1709. {
  1710. if(sEvent->motion.x<15)
  1711. {
  1712. adventureInt->scrollingDir |= CAdvMapInt::LEFT;
  1713. }
  1714. else
  1715. {
  1716. adventureInt->scrollingDir &= ~CAdvMapInt::LEFT;
  1717. }
  1718. if(sEvent->motion.x>screen->w-15)
  1719. {
  1720. adventureInt->scrollingDir |= CAdvMapInt::RIGHT;
  1721. }
  1722. else
  1723. {
  1724. adventureInt->scrollingDir &= ~CAdvMapInt::RIGHT;
  1725. }
  1726. if(sEvent->motion.y<15)
  1727. {
  1728. adventureInt->scrollingDir |= CAdvMapInt::UP;
  1729. }
  1730. else
  1731. {
  1732. adventureInt->scrollingDir &= ~CAdvMapInt::UP;
  1733. }
  1734. if(sEvent->motion.y>screen->h-15)
  1735. {
  1736. adventureInt->scrollingDir |= CAdvMapInt::DOWN;
  1737. }
  1738. else
  1739. {
  1740. adventureInt->scrollingDir &= ~CAdvMapInt::DOWN;
  1741. }
  1742. }
  1743. }
  1744. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  1745. {
  1746. current = sEvent;
  1747. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  1748. {
  1749. SDL_KeyboardEvent key = sEvent->key;
  1750. //translate numpad keys
  1751. if (key.keysym.sym >= SDLK_KP0 && key.keysym.sym <= SDLK_KP9)
  1752. {
  1753. key.keysym.sym = (SDLKey) (key.keysym.sym - SDLK_KP0 + SDLK_0);
  1754. }
  1755. else if(key.keysym.sym == SDLK_KP_ENTER)
  1756. {
  1757. key.keysym.sym = (SDLKey)SDLK_RETURN;
  1758. }
  1759. bool keysCaptured = false;
  1760. for(std::list<KeyInterested*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  1761. {
  1762. if((*i)->captureAllKeys)
  1763. {
  1764. keysCaptured = true;
  1765. break;
  1766. }
  1767. }
  1768. std::list<KeyInterested*> miCopy = keyinterested;
  1769. for(std::list<KeyInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  1770. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  1771. (**i).keyPressed(key);
  1772. }
  1773. else if(sEvent->type==SDL_MOUSEMOTION)
  1774. {
  1775. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  1776. handleMouseMotion(sEvent);
  1777. }
  1778. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1779. {
  1780. std::list<ClickableL*> hlp = lclickable;
  1781. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1782. {
  1783. if(!vstd::contains(lclickable,*i)) continue;
  1784. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1785. {
  1786. (*i)->clickLeft(true);
  1787. }
  1788. }
  1789. }
  1790. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1791. {
  1792. std::list<ClickableL*> hlp = lclickable;
  1793. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1794. {
  1795. if(!vstd::contains(lclickable,*i)) continue;
  1796. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1797. {
  1798. (*i)->clickLeft(false);
  1799. }
  1800. else
  1801. (*i)->clickLeft(boost::logic::indeterminate);
  1802. }
  1803. }
  1804. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1805. {
  1806. std::list<ClickableR*> hlp = rclickable;
  1807. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1808. {
  1809. if(!vstd::contains(rclickable,*i)) continue;
  1810. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1811. {
  1812. (*i)->clickRight(true);
  1813. }
  1814. }
  1815. }
  1816. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1817. {
  1818. std::list<ClickableR*> hlp = rclickable;
  1819. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1820. {
  1821. if(!vstd::contains(rclickable,*i)) continue;
  1822. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1823. {
  1824. (*i)->clickRight(false);
  1825. }
  1826. else
  1827. (*i)->clickRight(boost::logic::indeterminate);
  1828. }
  1829. }
  1830. current = NULL;
  1831. } //event end
  1832. int3 CPlayerInterface::repairScreenPos(int3 pos)
  1833. {
  1834. if(pos.x<=-Woff)
  1835. pos.x = -Woff+1;
  1836. if(pos.y<=-Hoff)
  1837. pos.y = -Hoff+1;
  1838. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff)
  1839. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff;
  1840. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff)
  1841. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff;
  1842. return pos;
  1843. }
  1844. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)
  1845. {
  1846. if(which >= PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  1847. return;
  1848. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1849. redrawHeroWin(hero);
  1850. }
  1851. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  1852. {
  1853. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1854. redrawHeroWin(hero);
  1855. }
  1856. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  1857. {
  1858. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1859. //adventureInt->heroList.draw();
  1860. }
  1861. void CPlayerInterface::receivedResource(int type, int val)
  1862. {
  1863. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1864. //LOCPLINT->totalRedraw();
  1865. }
  1866. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  1867. {
  1868. {
  1869. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1870. while(showingDialog->data)
  1871. showingDialog->cond.wait(un);
  1872. }
  1873. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1874. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  1875. LOCPLINT->pushInt(lw);
  1876. }
  1877. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  1878. {
  1879. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1880. //redraw infowindow
  1881. SDL_FreeSurface(graphics->townWins[town->id]);
  1882. graphics->townWins[town->id] = infoWin(town);
  1883. if(town->garrisonHero)
  1884. {
  1885. CGI->mh->hideObject(town->garrisonHero);
  1886. }
  1887. if(town->visitingHero)
  1888. {
  1889. CGI->mh->printObject(town->visitingHero);
  1890. }
  1891. adventureInt->heroList.updateHList();
  1892. CCastleInterface *c = castleInt;
  1893. if(c)
  1894. {
  1895. c->garr->highlighted = NULL;
  1896. c->hslotup.hero = town->garrisonHero;
  1897. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  1898. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  1899. c->garr->recreateSlots();
  1900. LOCPLINT->totalRedraw();
  1901. //c->showAll();
  1902. }
  1903. }
  1904. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  1905. {
  1906. if(hero->tempOwner != town->tempOwner)
  1907. return;
  1908. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1909. openTownWindow(town);
  1910. }
  1911. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  1912. {
  1913. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1914. if(obj->ID == HEROI_TYPE) //hero
  1915. {
  1916. const CGHeroInstance * hh;
  1917. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  1918. {
  1919. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  1920. graphics->heroWins[hh->subID] = infoWin(hh);
  1921. }
  1922. }
  1923. else if (obj->ID == TOWNI_TYPE) //town
  1924. {
  1925. const CGTownInstance * tt;
  1926. if(tt = static_cast<const CGTownInstance*>(obj))
  1927. {
  1928. SDL_FreeSurface(graphics->townWins[tt->id]);
  1929. graphics->townWins[tt->id] = infoWin(tt);
  1930. }
  1931. if(tt->visitingHero)
  1932. {
  1933. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  1934. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  1935. }
  1936. }
  1937. bool wasGarrison = false;
  1938. for(std::list<IShowActivable*>::iterator i = listInt.begin(); i != listInt.end(); i++)
  1939. {
  1940. if((*i)->type & IShowActivable::WITH_GARRISON)
  1941. {
  1942. CWindowWithGarrison *wwg = static_cast<CWindowWithGarrison*>(*i);
  1943. wwg->garr->recreateSlots();
  1944. wasGarrison = true;
  1945. }
  1946. }
  1947. if(wasGarrison)
  1948. LOCPLINT->totalRedraw();
  1949. }
  1950. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  1951. {
  1952. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1953. switch (buildingID)
  1954. {
  1955. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  1956. {
  1957. SDL_FreeSurface(graphics->townWins[town->id]);
  1958. graphics->townWins[town->id] = infoWin(town);
  1959. break;
  1960. }
  1961. }
  1962. if(!castleInt)
  1963. return;
  1964. if(castleInt->town!=town)
  1965. return;
  1966. switch(what)
  1967. {
  1968. case 1:
  1969. castleInt->addBuilding(buildingID);
  1970. break;
  1971. case 2:
  1972. castleInt->removeBuilding(buildingID);
  1973. break;
  1974. }
  1975. }
  1976. void CPlayerInterface::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side) //called by engine when battle starts; side=0 - left, side=1 - right
  1977. {
  1978. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1979. battleInt = new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
  1980. pushInt(battleInt);
  1981. }
  1982. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  1983. {
  1984. }
  1985. void CPlayerInterface::battleNewRound(int round) //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  1986. {
  1987. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1988. battleInt->newRound(round);
  1989. }
  1990. void CPlayerInterface::actionStarted(const BattleAction* action)
  1991. {
  1992. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1993. curAction = new BattleAction(*action);
  1994. if( (action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber))) )
  1995. {
  1996. battleInt->moveStarted = true;
  1997. if(battleInt->creAnims[action->stackNumber]->framesInGroup(20))
  1998. {
  1999. battleInt->creAnims[action->stackNumber]->setType(20);
  2000. }
  2001. }
  2002. battleInt->deactivate();
  2003. CStack *stack = cb->battleGetStackByID(action->stackNumber);
  2004. char txt[400];
  2005. if(action->actionType == 1)
  2006. {
  2007. if(action->side)
  2008. battleInt->defendingHero->setPhase(4);
  2009. else
  2010. battleInt->attackingHero->setPhase(4);
  2011. return;
  2012. }
  2013. if(!stack)
  2014. {
  2015. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  2016. return;
  2017. }
  2018. int txtid = 0;
  2019. switch(action->actionType)
  2020. {
  2021. case 3: //defend
  2022. txtid = 120;
  2023. break;
  2024. case 8: //wait
  2025. txtid = 136;
  2026. break;
  2027. case 11: //bad morale
  2028. txtid = -34; //negative -> no separate singular/plural form
  2029. battleInt->displayEffect(30,stack->position);
  2030. break;
  2031. }
  2032. if(txtid > 0 && stack->amount != 1)
  2033. txtid++; //move to plural text
  2034. else if(txtid < 0)
  2035. txtid = -txtid;
  2036. if(txtid)
  2037. {
  2038. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str(), 0);
  2039. LOCPLINT->battleInt->console->addText(txt);
  2040. }
  2041. }
  2042. void CPlayerInterface::actionFinished(const BattleAction* action)
  2043. {
  2044. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2045. delete curAction;
  2046. curAction = NULL;
  2047. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  2048. {
  2049. battleInt->activate();
  2050. }
  2051. if(action->actionType == 1)
  2052. {
  2053. if(action->side)
  2054. battleInt->defendingHero->setPhase(0);
  2055. else
  2056. battleInt->attackingHero->setPhase(0);
  2057. }
  2058. }
  2059. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  2060. {
  2061. CBattleInterface *b = battleInt;
  2062. {
  2063. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2064. CStack *stack = cb->battleGetStackByID(stackID);
  2065. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  2066. {
  2067. std::string hlp = CGI->generaltexth->allTexts[33];
  2068. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  2069. battleInt->displayEffect(20,stack->position);
  2070. battleInt->console->addText(hlp);
  2071. }
  2072. b->stackActivated(stackID);
  2073. }
  2074. //wait till BattleInterface sets its command
  2075. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  2076. while(!b->givenCommand->data)
  2077. b->givenCommand->cond.wait(lock);
  2078. //tidy up
  2079. BattleAction ret = *(b->givenCommand->data);
  2080. delete b->givenCommand->data;
  2081. b->givenCommand->data = NULL;
  2082. //return command
  2083. return ret;
  2084. }
  2085. void CPlayerInterface::battleEnd(BattleResult *br)
  2086. {
  2087. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2088. battleInt->battleFinished(*br);
  2089. }
  2090. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  2091. {
  2092. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2093. battleInt->stackMoved(ID, dest, end, distance);
  2094. }
  2095. void CPlayerInterface::battleSpellCasted(SpellCasted *sc)
  2096. {
  2097. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2098. battleInt->spellCasted(sc);
  2099. }
  2100. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  2101. {
  2102. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2103. battleInt->battleStacksEffectsSet(sse);
  2104. }
  2105. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  2106. {
  2107. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  2108. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2109. tlog5 << "done!\n";
  2110. std::vector<CBattleInterface::SStackAttackedInfo> arg;
  2111. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  2112. {
  2113. if(i->isEffect())
  2114. {
  2115. battleInt->displayEffect(i->effect, cb->battleGetStackByID(i->stackAttacked)->position);
  2116. }
  2117. CBattleInterface::SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, LOCPLINT->curAction->stackNumber, LOCPLINT->curAction->actionType==7, i->killed()};
  2118. arg.push_back(to_put);
  2119. }
  2120. battleInt->stacksAreAttacked(arg);
  2121. }
  2122. void CPlayerInterface::battleAttack(BattleAttack *ba)
  2123. {
  2124. tlog5 << "CPlayerInterface::battleAttack - locking...";
  2125. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2126. tlog5 << "done!\n";
  2127. if(ba->lucky()) //lucky hit
  2128. {
  2129. CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  2130. std::string hlp = CGI->generaltexth->allTexts[45];
  2131. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  2132. battleInt->console->addText(hlp);
  2133. battleInt->displayEffect(18,stack->position);
  2134. }
  2135. //TODO: bad luck?
  2136. if(ba->shot())
  2137. {
  2138. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  2139. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  2140. }
  2141. else
  2142. {
  2143. CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  2144. int shift = 0;
  2145. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  2146. {
  2147. if(attacker->attackerOwned)
  2148. shift = 1;
  2149. else
  2150. shift = -1;
  2151. }
  2152. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  2153. }
  2154. }
  2155. void CPlayerInterface::showComp(SComponent comp)
  2156. {
  2157. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2158. adventureInt->infoBar.showComp(&comp,4000);
  2159. }
  2160. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components)
  2161. {
  2162. std::vector<SComponent*> intComps;
  2163. for(int i=0;i<components.size();i++)
  2164. intComps.push_back(new SComponent(*components[i]));
  2165. showInfoDialog(text,intComps);
  2166. }
  2167. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components)
  2168. {
  2169. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2170. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2171. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2172. CInfoWindow * temp = new CInfoWindow(text,playerID,36,components,pom);
  2173. if(makingTurn && listInt.size())
  2174. {
  2175. showingDialog->set(true);
  2176. pushInt(temp);
  2177. }
  2178. else
  2179. {
  2180. dialogs.push_back(temp);
  2181. }
  2182. }
  2183. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps)
  2184. {
  2185. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2186. LOCPLINT->showingDialog->setn(true);
  2187. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2188. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2189. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  2190. CInfoWindow * temp = new CInfoWindow(text,playerID,36,components,pom);
  2191. temp->delComps = DelComps;
  2192. for(int i=0;i<onYes.funcs.size();i++)
  2193. temp->buttons[0]->callback += onYes.funcs[i];
  2194. for(int i=0;i<onNo.funcs.size();i++)
  2195. temp->buttons[1]->callback += onNo.funcs[i];
  2196. LOCPLINT->pushInt(temp);
  2197. }
  2198. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, bool selection, bool cancel )
  2199. {
  2200. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2201. if(!selection && cancel) //simple yes/no dialog
  2202. {
  2203. std::vector<SComponent*> intComps;
  2204. for(int i=0;i<components.size();i++)
  2205. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  2206. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true);
  2207. }
  2208. else if(selection)
  2209. {
  2210. std::vector<CSelectableComponent*> intComps;
  2211. for(int i=0;i<components.size();i++)
  2212. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  2213. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2214. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2215. if(cancel)
  2216. {
  2217. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  2218. }
  2219. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  2220. pushInt(temp);
  2221. intComps[0]->clickLeft(true);
  2222. }
  2223. }
  2224. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  2225. {
  2226. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2227. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  2228. adventureInt->minimap.showTile(*i);
  2229. }
  2230. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  2231. {
  2232. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2233. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  2234. adventureInt->minimap.hideTile(*i);
  2235. }
  2236. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  2237. {
  2238. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2239. adventureInt->heroWindow->setHero(hero);
  2240. adventureInt->heroWindow->quitButton->callback = boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  2241. pushInt(adventureInt->heroWindow);
  2242. }
  2243. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  2244. {
  2245. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2246. if(adventureInt->heroWindow->curHero)
  2247. {
  2248. adventureInt->heroWindow->deactivate();
  2249. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  2250. adventureInt->heroWindow->activate();
  2251. }
  2252. }
  2253. void CPlayerInterface::updateWater()
  2254. {
  2255. }
  2256. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  2257. {
  2258. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2259. if(castleInt)
  2260. {
  2261. //CFortScreen *fs = dynamic_cast<CFortScreen*>();
  2262. //if(fs)
  2263. // fs->draw(castleInt,false);
  2264. }
  2265. }
  2266. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  2267. {
  2268. if(bonus.type == HeroBonus::NONE) return;
  2269. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2270. redrawHeroWin(hero);
  2271. }
  2272. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  2273. {
  2274. h & playerID & serialID;
  2275. h & heroMoveSpeed & mapScrollingSpeed;
  2276. h & CBattleInterface::settings;
  2277. }
  2278. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  2279. {
  2280. serializeTempl(h,version);
  2281. }
  2282. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  2283. {
  2284. serializeTempl(h,version);
  2285. }
  2286. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  2287. {
  2288. if(!vstd::contains(graphics->heroWins,hero->subID))
  2289. {
  2290. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  2291. return;
  2292. }
  2293. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  2294. graphics->heroWins[hero->subID] = infoWin(hero);
  2295. if (adventureInt->selection == hero)
  2296. adventureInt->infoBar.draw(screen2);
  2297. }
  2298. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CPath * path )
  2299. {
  2300. if (!h || !path)
  2301. return false; //can't find hero
  2302. bool result = false;
  2303. path->convert(0);
  2304. stillMoveHero = true;
  2305. for(int i=path->nodes.size()-1; i>0; i--)
  2306. {
  2307. {
  2308. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2309. if(!stillMoveHero)
  2310. return result;
  2311. }
  2312. int3 endpos(path->nodes[i-1].coord.x, path->nodes[i-1].coord.y, h->pos.z);
  2313. result = cb->moveHero(h,endpos);
  2314. }
  2315. stillMoveHero = false;
  2316. return result;
  2317. }
  2318. bool CPlayerInterface::shiftPressed() const
  2319. {
  2320. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  2321. }
  2322. void CPlayerInterface::showGarrisonDialog( const CArmedInstance *up, const CGHeroInstance *down, boost::function<void()> &onEnd )
  2323. {
  2324. CGarrisonWindow *cgw = new CGarrisonWindow(up,down);
  2325. cgw->quit->callback += onEnd;
  2326. pushInt(cgw);
  2327. }
  2328. void CPlayerInterface::popInt( IShowActivable *top )
  2329. {
  2330. assert(listInt.front() == top);
  2331. top->deactivate();
  2332. listInt.pop_front();
  2333. objsToBlit -= top;
  2334. if(listInt.size())
  2335. listInt.front()->activate();
  2336. totalRedraw();
  2337. }
  2338. void CPlayerInterface::popIntTotally( IShowActivable *top )
  2339. {
  2340. assert(listInt.front() == top);
  2341. popInt(top);
  2342. delete top;
  2343. }
  2344. void CPlayerInterface::pushInt( IShowActivable *newInt )
  2345. {
  2346. if(listInt.size())
  2347. listInt.front()->deactivate();
  2348. listInt.push_front(newInt);
  2349. newInt->activate();
  2350. objsToBlit += newInt;
  2351. LOCPLINT->totalRedraw();
  2352. }
  2353. void CPlayerInterface::popInts( int howMany )
  2354. {
  2355. if(!howMany) return; //senseless but who knows...
  2356. assert(listInt.size() > howMany);
  2357. listInt.front()->deactivate();
  2358. for(int i=0; i < howMany; i++)
  2359. {
  2360. objsToBlit -= listInt.front();
  2361. delete listInt.front();
  2362. listInt.pop_front();
  2363. }
  2364. listInt.front()->activate();
  2365. totalRedraw();
  2366. }
  2367. IShowActivable * CPlayerInterface::topInt()
  2368. {
  2369. if(!listInt.size())
  2370. return NULL;
  2371. else
  2372. return listInt.front();
  2373. }
  2374. void CPlayerInterface::totalRedraw()
  2375. {
  2376. for(int i=0;i<objsToBlit.size();i++)
  2377. objsToBlit[i]->showAll(screen2);
  2378. blitAt(screen2,0,0,screen);
  2379. if(objsToBlit.size())
  2380. objsToBlit.back()->showAll(screen);
  2381. }
  2382. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  2383. {
  2384. bg=BitmapHandler::loadBitmap(name);
  2385. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2386. pos.x=x;
  2387. pos.y=y;
  2388. if(maxw >= 0)
  2389. pos.w = std::min(bg->w,maxw);
  2390. else
  2391. pos.w=bg->w;
  2392. pos.h=bg->h;
  2393. middlex=(pos.w/2)+x;
  2394. middley=(bg->h/2)+y;
  2395. }
  2396. CStatusBar::~CStatusBar()
  2397. {
  2398. SDL_FreeSurface(bg);
  2399. }
  2400. void CStatusBar::clear()
  2401. {
  2402. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  2403. {
  2404. current="";
  2405. show(screen);
  2406. }
  2407. }
  2408. void CStatusBar::print(const std::string & text)
  2409. {
  2410. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  2411. {
  2412. current=text;
  2413. show(screen);
  2414. }
  2415. }
  2416. void CStatusBar::show(SDL_Surface * to)
  2417. {
  2418. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2419. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),to,&pom);
  2420. printAtMiddle(current,middlex,middley,GEOR13,zwykly,to);
  2421. }
  2422. std::string CStatusBar::getCurrent()
  2423. {
  2424. return current;
  2425. }
  2426. void CList::activate()
  2427. {
  2428. ClickableL::activate();
  2429. ClickableR::activate();
  2430. Hoverable::activate();
  2431. KeyInterested::activate();
  2432. MotionInterested::activate();
  2433. };
  2434. void CList::deactivate()
  2435. {
  2436. ClickableL::deactivate();
  2437. ClickableR::deactivate();
  2438. Hoverable::deactivate();
  2439. KeyInterested::deactivate();
  2440. MotionInterested::deactivate();
  2441. };
  2442. void CList::clickLeft(tribool down)
  2443. {
  2444. };
  2445. CList::CList(int Size)
  2446. :SIZE(Size)
  2447. {
  2448. }
  2449. CHeroList::CHeroList(int Size)
  2450. :CList(Size)
  2451. {
  2452. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  2453. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  2454. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  2455. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  2456. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  2457. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  2458. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  2459. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  2460. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  2461. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  2462. //32px per hero
  2463. posmobx = pos.x+1;
  2464. posmoby = pos.y+arrup->height+1;
  2465. posporx = pos.x+mobile->width+2;
  2466. pospory = pos.y+arrup->height;
  2467. posmanx = pos.x+1+50+mobile->width;
  2468. posmany = pos.y+arrup->height+1;
  2469. from = 0;
  2470. pressed = indeterminate;
  2471. }
  2472. void CHeroList::init()
  2473. {
  2474. int w = pos.w+1, h = pos.h+4;
  2475. bg = CSDL_Ext::newSurface(w,h,screen);
  2476. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  2477. }
  2478. void CHeroList::genList()
  2479. {
  2480. int howMany = LOCPLINT->cb->howManyHeroes();
  2481. for (int i=0;i<howMany;i++)
  2482. {
  2483. const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  2484. if(!h->inTownGarrison)
  2485. items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  2486. }
  2487. }
  2488. void CHeroList::select(int which)
  2489. {
  2490. if (which<0)
  2491. {
  2492. selected = which;
  2493. LOCPLINT->adventureInt->selection = NULL;
  2494. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2495. draw(screen);
  2496. LOCPLINT->adventureInt->infoBar.draw(screen);
  2497. }
  2498. if (which>=items.size())
  2499. return;
  2500. selected = which;
  2501. //recalculationg path in case of something has changed on map
  2502. if(items[which].second)
  2503. {
  2504. CPath * newPath = LOCPLINT->cb->getPath(items[which].second->startPos(), items[which].second->endPos(), items[which].first);
  2505. delete items[which].second;
  2506. LOCPLINT->adventureInt->terrain.currentPath = items[which].second = newPath;
  2507. }
  2508. else
  2509. {
  2510. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2511. }
  2512. LOCPLINT->adventureInt->select(items[which].first);
  2513. //recalculated and assigned
  2514. }
  2515. void CHeroList::clickLeft(tribool down)
  2516. {
  2517. if (down)
  2518. {
  2519. /***************************ARROWS*****************************************/
  2520. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2521. {
  2522. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2523. pressed = true;
  2524. return;
  2525. }
  2526. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2527. {
  2528. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2529. pressed = false;
  2530. return;
  2531. }
  2532. /***************************HEROES*****************************************/
  2533. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2534. hx-=pos.x;
  2535. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2536. int ny = hy/32;
  2537. if (ny>=SIZE || ny<0)
  2538. return;
  2539. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2540. LOCPLINT->openHeroWindow(items[selected].first);//print hero screen
  2541. select(ny+from);
  2542. }
  2543. else
  2544. {
  2545. if (indeterminate(pressed))
  2546. return;
  2547. if (pressed) //up
  2548. {
  2549. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2550. pressed = indeterminate;
  2551. if (!down)
  2552. {
  2553. from--;
  2554. if (from<0)
  2555. from=0;
  2556. draw(screen2);
  2557. }
  2558. }
  2559. else if (!pressed) //down
  2560. {
  2561. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2562. pressed = indeterminate;
  2563. if (!down)
  2564. {
  2565. from++;
  2566. //if (from<items.size()-5)
  2567. // from=items.size()-5;
  2568. draw(screen2);
  2569. }
  2570. }
  2571. else
  2572. throw 0;
  2573. }
  2574. }
  2575. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  2576. {
  2577. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2578. {
  2579. if (from>0)
  2580. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  2581. else
  2582. LOCPLINT->adventureInt->statusbar.clear();
  2583. return;
  2584. }
  2585. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2586. {
  2587. if ((items.size()-from) > SIZE)
  2588. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  2589. else
  2590. LOCPLINT->adventureInt->statusbar.clear();
  2591. return;
  2592. }
  2593. //if not buttons then heroes
  2594. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2595. hx-=pos.x;
  2596. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2597. int ny = hy/32;
  2598. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2599. {
  2600. LOCPLINT->adventureInt->statusbar.clear();
  2601. return;
  2602. }
  2603. std::vector<std::string> temp;
  2604. temp.push_back(items[from+ny].first->name);
  2605. temp.push_back(items[from+ny].first->type->heroClass->name);
  2606. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  2607. //select(ny+from);
  2608. }
  2609. void CHeroList::clickRight(tribool down)
  2610. {
  2611. if (down)
  2612. {
  2613. /***************************ARROWS*****************************************/
  2614. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2615. {
  2616. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  2617. }
  2618. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2619. {
  2620. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  2621. }
  2622. //if not buttons then heroes
  2623. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2624. hx-=pos.x;
  2625. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2626. int ny = hy/32;
  2627. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2628. {
  2629. return;
  2630. }
  2631. //show popup
  2632. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[items[from+ny].first->subID],
  2633. LOCPLINT->current->motion.x-graphics->heroWins[items[from+ny].first->subID]->w,
  2634. LOCPLINT->current->motion.y-graphics->heroWins[items[from+ny].first->subID]->h,
  2635. false);
  2636. LOCPLINT->pushInt(ip);
  2637. }
  2638. else
  2639. {
  2640. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  2641. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  2642. }
  2643. }
  2644. void CHeroList::hover (bool on)
  2645. {
  2646. }
  2647. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  2648. {
  2649. }
  2650. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  2651. {
  2652. if(toRemove) //remove specific hero
  2653. {
  2654. for (std::vector<std::pair<const CGHeroInstance*, CPath *> >::iterator i=items.begin(); i != items.end(); i++)
  2655. {
  2656. if(i->first == toRemove)
  2657. {
  2658. delete i->second;
  2659. items.erase(i);
  2660. break;
  2661. }
  2662. }
  2663. }
  2664. else
  2665. {
  2666. items.clear();
  2667. genList();
  2668. }
  2669. if(selected>=items.size())
  2670. select(items.size()-1);
  2671. if(items.size()==0)
  2672. LOCPLINT->adventureInt->townList.select(0);
  2673. else
  2674. select(selected);
  2675. }
  2676. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  2677. {
  2678. int ser = -1;
  2679. for(int i=0; i<items.size() && ser<0; i++)
  2680. if(items[i].first->subID == which->subID)
  2681. ser = i;
  2682. ser -= from;
  2683. if(ser<0 || ser > SIZE) return;
  2684. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  2685. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  2686. }
  2687. void CHeroList::draw(SDL_Surface * to)
  2688. {
  2689. for (int iT=0+from;iT<SIZE+from;iT++)
  2690. {
  2691. int i = iT-from;
  2692. if (iT>=items.size())
  2693. {
  2694. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  2695. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  2696. blitAt(empty,posporx,pospory+i*32,to);
  2697. continue;
  2698. }
  2699. const CGHeroInstance *cur = items[iT].first;
  2700. int pom = cur->movement / 100;
  2701. if (pom>25) pom=25;
  2702. if (pom<0) pom=0;
  2703. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  2704. pom = cur->mana / 5;
  2705. if (pom>25) pom=25;
  2706. if (pom<0) pom=0;
  2707. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  2708. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  2709. blitAt(temp,posporx,pospory+i*32,to);
  2710. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2711. {
  2712. blitAt(selection,posporx,pospory+i*32,to);
  2713. }
  2714. //TODO: support for custom portraits
  2715. }
  2716. if (from>0)
  2717. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  2718. else
  2719. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  2720. if (items.size()-from > SIZE)
  2721. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  2722. else
  2723. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  2724. }
  2725. int CHeroList::getPosOfHero(const CArmedInstance* h)
  2726. {
  2727. return vstd::findPos(
  2728. items,h,
  2729. boost::bind(vstd::equal<std::pair<const CGHeroInstance*, CPath *>,
  2730. const CArmedInstance *,const CGHeroInstance*>,_1,
  2731. &std::pair<const CGHeroInstance*, CPath *>::first,_2));
  2732. }
  2733. CTownList::~CTownList()
  2734. {
  2735. delete arrup;
  2736. delete arrdo;
  2737. }
  2738. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  2739. :CList(Size)
  2740. {
  2741. arrup = CDefHandler::giveDef(arrupg);
  2742. arrdo = CDefHandler::giveDef(arrdog);
  2743. pos.x = x;
  2744. pos.y = y;
  2745. pos.w = std::max(arrdo->width, arrup->width);
  2746. pos.h = arrdo->height + arrup->height + Size*32;
  2747. arrupp.x=x;
  2748. arrupp.y=y;
  2749. arrupp.w=arrup->width;
  2750. arrupp.h=arrup->height;
  2751. arrdop.x=x;
  2752. arrdop.y=y+arrup->height+32*SIZE;
  2753. arrdop.w=arrdo->width;
  2754. arrdop.h=arrdo->height;
  2755. posporx = arrdop.x;
  2756. pospory = arrupp.y + arrupp.h;
  2757. pressed = indeterminate;
  2758. from = 0;
  2759. }
  2760. void CTownList::genList()
  2761. {
  2762. int howMany = LOCPLINT->cb->howManyTowns();
  2763. for (int i=0;i<howMany;i++)
  2764. {
  2765. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  2766. }
  2767. }
  2768. void CTownList::select(int which)
  2769. {
  2770. if (which>=items.size())
  2771. return;
  2772. selected = which;
  2773. if(!fun.empty())
  2774. fun();
  2775. }
  2776. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  2777. {
  2778. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2779. {
  2780. if (from>0)
  2781. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[306].first);
  2782. else
  2783. LOCPLINT->statusbar->clear();
  2784. return;
  2785. }
  2786. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2787. {
  2788. if ((items.size()-from) > SIZE)
  2789. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[307].first);
  2790. else
  2791. LOCPLINT->statusbar->clear();
  2792. return;
  2793. }
  2794. //if not buttons then towns
  2795. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2796. hx-=pos.x;
  2797. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2798. int ny = hy/32;
  2799. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2800. {
  2801. LOCPLINT->statusbar->clear();
  2802. return;
  2803. };
  2804. LOCPLINT->statusbar->print(items[from+ny]->name);
  2805. }
  2806. void CTownList::clickLeft(tribool down)
  2807. {
  2808. if (down)
  2809. {
  2810. /***************************ARROWS*****************************************/
  2811. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2812. {
  2813. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2814. pressed = true;
  2815. return;
  2816. }
  2817. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2818. {
  2819. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2820. pressed = false;
  2821. return;
  2822. }
  2823. /***************************TOWNS*****************************************/
  2824. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2825. hx-=pos.x;
  2826. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2827. int ny = hy/32;
  2828. if (ny>SIZE || ny<0)
  2829. return;
  2830. if(LOCPLINT->topInt() == LOCPLINT->adventureInt
  2831. && (ny+from)==selected
  2832. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  2833. )
  2834. LOCPLINT->openTownWindow(items[selected]);//print town screen
  2835. else
  2836. select(ny+from);
  2837. }
  2838. else
  2839. {
  2840. if (indeterminate(pressed))
  2841. return;
  2842. if (pressed) //up
  2843. {
  2844. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2845. pressed = indeterminate;
  2846. if (!down)
  2847. {
  2848. from--;
  2849. if (from<0)
  2850. from=0;
  2851. draw(screen2);
  2852. }
  2853. }
  2854. else if (!pressed) //down
  2855. {
  2856. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2857. pressed = indeterminate;
  2858. if (!down)
  2859. {
  2860. from++;
  2861. //if (from<items.size()-5)
  2862. // from=items.size()-5;
  2863. draw(screen2);
  2864. }
  2865. }
  2866. else
  2867. throw 0;
  2868. }
  2869. }
  2870. void CTownList::clickRight(tribool down)
  2871. {
  2872. if (down)
  2873. {
  2874. /***************************ARROWS*****************************************/
  2875. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2876. {
  2877. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  2878. }
  2879. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2880. {
  2881. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  2882. }
  2883. //if not buttons then towns
  2884. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2885. hx-=pos.x;
  2886. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2887. int ny = hy/32;
  2888. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2889. {
  2890. return;
  2891. }
  2892. //show popup
  2893. CInfoPopup * ip = new CInfoPopup(
  2894. graphics->townWins[items[from+ny]->id],
  2895. LOCPLINT->current->motion.x-graphics->townWins[items[from+ny]->id]->w,
  2896. LOCPLINT->current->motion.y-graphics->townWins[items[from+ny]->id]->h,
  2897. false);
  2898. LOCPLINT->pushInt(ip);
  2899. }
  2900. else
  2901. {
  2902. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  2903. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  2904. }
  2905. }
  2906. void CTownList::hover (bool on)
  2907. {
  2908. }
  2909. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  2910. {
  2911. }
  2912. void CTownList::draw(SDL_Surface * to)
  2913. {
  2914. for (int iT=0+from;iT<SIZE+from;iT++)
  2915. {
  2916. int i = iT-from;
  2917. if (iT>=items.size())
  2918. {
  2919. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  2920. continue;
  2921. }
  2922. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32,to);
  2923. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2924. {
  2925. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  2926. }
  2927. }
  2928. if (from>0)
  2929. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  2930. else
  2931. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  2932. if (items.size()-from>SIZE)
  2933. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  2934. else
  2935. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  2936. }
  2937. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  2938. :c(cre),big(Big)
  2939. {
  2940. anim = new CCreatureAnimation(cre->animDefName);
  2941. }
  2942. CCreaturePic::~CCreaturePic()
  2943. {
  2944. delete anim;
  2945. }
  2946. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  2947. {
  2948. SDL_Rect dst;
  2949. if(big)
  2950. {
  2951. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  2952. dst = genRect(130,100,x,y);
  2953. }
  2954. else
  2955. {
  2956. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  2957. dst = genRect(120,100,x,y);
  2958. }
  2959. if(c->isDoubleWide())
  2960. x-=15;
  2961. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  2962. }
  2963. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  2964. {
  2965. //TODO: write
  2966. return NULL;
  2967. }
  2968. void CRecrutationWindow::close()
  2969. {
  2970. LOCPLINT->popIntTotally(this);
  2971. }
  2972. void CRecrutationWindow::Max()
  2973. {
  2974. slider->moveTo(slider->amount);
  2975. }
  2976. void CRecrutationWindow::Buy()
  2977. {
  2978. recruit(creatures[which].ID,slider->value);
  2979. close();
  2980. }
  2981. void CRecrutationWindow::Cancel()
  2982. {
  2983. close();
  2984. }
  2985. void CRecrutationWindow::sliderMoved(int to)
  2986. {
  2987. buy->block(!to);
  2988. }
  2989. void CRecrutationWindow::clickLeft(tribool down)
  2990. {
  2991. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2992. for(int i=0;i<creatures.size();i++)
  2993. {
  2994. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2995. {
  2996. which = i;
  2997. int newAmount = std::min(amounts[i],creatures[i].amount);
  2998. slider->amount = newAmount;
  2999. if(slider->value > newAmount)
  3000. slider->moveTo(newAmount);
  3001. else
  3002. slider->moveTo(slider->value);
  3003. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3004. for(int j=0;j<creatures.size();j++)
  3005. {
  3006. if(which==j)
  3007. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  3008. else
  3009. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  3010. curx += 120;
  3011. }
  3012. break;
  3013. }
  3014. curx += 120;
  3015. }
  3016. }
  3017. void CRecrutationWindow::clickRight( boost::logic::tribool down )
  3018. {
  3019. if(down)
  3020. {
  3021. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3022. for(int i=0;i<creatures.size();i++)
  3023. {
  3024. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  3025. {
  3026. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID,0,0,NULL,NULL,NULL,NULL);
  3027. LOCPLINT->pushInt(popup);
  3028. break;
  3029. }
  3030. curx += 120;
  3031. }
  3032. }
  3033. }
  3034. void CRecrutationWindow::activate()
  3035. {
  3036. ClickableL::activate();
  3037. ClickableR::activate();
  3038. buy->activate();
  3039. max->activate();
  3040. cancel->activate();
  3041. slider->activate();
  3042. LOCPLINT->statusbar = bar;
  3043. }
  3044. void CRecrutationWindow::deactivate()
  3045. {
  3046. ClickableL::deactivate();
  3047. ClickableR::deactivate();
  3048. buy->deactivate();
  3049. max->deactivate();
  3050. cancel->deactivate();
  3051. slider->deactivate();
  3052. }
  3053. void CRecrutationWindow::show(SDL_Surface * to)
  3054. {
  3055. static char c=0;
  3056. blitAt(bitmap,pos.x,pos.y,to);
  3057. buy->show(to);
  3058. max->show(to);
  3059. cancel->show(to);
  3060. slider->show(to);
  3061. char pom[15];
  3062. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  3063. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,to);
  3064. SDL_itoa(slider->value,pom,10); //recruit
  3065. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,to);
  3066. printAtMiddle(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID].namePl,pos.x+243,pos.y+32,GEOR16,tytulowy,to); //eg "Recruit Dragon flies"
  3067. int curx = pos.x+115-creatures[which].res.size()*16;
  3068. for(int i=0;i<creatures[which].res.size();i++)
  3069. {
  3070. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  3071. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  3072. SDL_itoa(creatures[which].res[i].second,pom,10);
  3073. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,to);
  3074. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  3075. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,to);
  3076. curx+=32;
  3077. }
  3078. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3079. for(int i=0;i<creatures.size();i++)
  3080. {
  3081. creatures[i].pic->blitPic(to,curx-50,pos.y+130-65,!(c%4));
  3082. curx += 120;
  3083. }
  3084. c++;
  3085. bar->show(to);
  3086. }
  3087. CRecrutationWindow::CRecrutationWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  3088. :recruit(Recruit)
  3089. {
  3090. which = 0;
  3091. creatures.resize(Creatures.size());
  3092. amounts.resize(Creatures.size());
  3093. for(int i=0;i<creatures.size();i++)
  3094. {
  3095. creatures[i].amount = Creatures[i].second;
  3096. creatures[i].ID = Creatures[i].first;
  3097. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  3098. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  3099. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  3100. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  3101. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  3102. }
  3103. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  3104. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  3105. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  3106. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3107. SDL_FreeSurface(hhlp);
  3108. pos.x = screen->w/2 - bitmap->w/2;
  3109. pos.y = screen->h/2 - bitmap->h/2;
  3110. pos.w = bitmap->w;
  3111. pos.h = bitmap->h;
  3112. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  3113. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecrutationWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  3114. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecrutationWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  3115. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecrutationWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  3116. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecrutationWindow::sliderMoved,this, _1),1,std::min(amounts[0],creatures[0].amount),0,true);
  3117. std::string pom;
  3118. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  3119. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  3120. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  3121. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  3122. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  3123. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  3124. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  3125. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  3126. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  3127. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  3128. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  3129. //border for creatures
  3130. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3131. for(int i=0;i<creatures.size();i++)
  3132. {
  3133. creatures[i].pos.x = curx+1;
  3134. creatures[i].pos.y = 65;
  3135. creatures[i].pos.w = 100;
  3136. creatures[i].pos.h = 130;
  3137. if(which==i)
  3138. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  3139. else
  3140. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  3141. curx += 120;
  3142. }
  3143. if(!creatures[0].amount || !amounts[0])
  3144. {
  3145. max->block(true);
  3146. slider->block(true);
  3147. }
  3148. //buy->block(true); //not needed, will be blocked by initing slider on 0
  3149. }
  3150. CRecrutationWindow::~CRecrutationWindow()
  3151. {
  3152. for(int i=0;i<creatures.size();i++)
  3153. {
  3154. delete creatures[i].pic;
  3155. }
  3156. delete max;
  3157. delete buy;
  3158. delete cancel;
  3159. SDL_FreeSurface(bitmap);
  3160. delete slider;
  3161. delete bar;
  3162. }
  3163. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  3164. {
  3165. last = Last;
  3166. which = 1;
  3167. c=cid;
  3168. slider = NULL;
  3169. gar = Owner;
  3170. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  3171. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  3172. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3173. pos.x = screen->w/2 - bitmap->w/2;
  3174. pos.y = screen->h/2 - bitmap->h/2;
  3175. pos.w = bitmap->w;
  3176. pos.h = bitmap->h;
  3177. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  3178. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  3179. int sliderPositions = max - (last>=0) - (last==2);
  3180. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,sliderPositions,val,true);
  3181. a1 = max-val;
  3182. a2 = val;
  3183. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  3184. anim->anim->setType(1);
  3185. std::string title = CGI->generaltexth->allTexts[256];
  3186. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  3187. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  3188. }
  3189. CSplitWindow::~CSplitWindow()
  3190. {
  3191. SDL_FreeSurface(bitmap);
  3192. delete ok;
  3193. delete cancel;
  3194. delete slider;
  3195. delete anim;
  3196. }
  3197. void CSplitWindow::activate()
  3198. {
  3199. ClickableL::activate();
  3200. KeyInterested::activate();
  3201. ok->activate();
  3202. cancel->activate();
  3203. slider->activate();
  3204. }
  3205. void CSplitWindow::deactivate()
  3206. {
  3207. ClickableL::deactivate();
  3208. KeyInterested::deactivate();
  3209. ok->deactivate();
  3210. cancel->deactivate();
  3211. slider->deactivate();
  3212. }
  3213. void CSplitWindow::split()
  3214. {
  3215. gar->splitStacks(a2);
  3216. close();
  3217. }
  3218. void CSplitWindow::close()
  3219. {
  3220. LOCPLINT->popIntTotally(this);
  3221. }
  3222. void CSplitWindow::sliderMoved(int to)
  3223. {
  3224. int all = a1+a2;
  3225. a2 = to + (last==1 || last==2);
  3226. if(slider)
  3227. a1 = all - a2;
  3228. }
  3229. void CSplitWindow::show(SDL_Surface * to)
  3230. {
  3231. blitAt(bitmap,pos.x,pos.y,to);
  3232. ok->show(to);
  3233. cancel->show(to);
  3234. slider->show(to);
  3235. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,GEOR16,zwykly,to);
  3236. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,GEOR16,zwykly,to);
  3237. anim->blitPic(to,pos.x+20,pos.y+54,false);
  3238. anim->blitPic(to,pos.x+177,pos.y+54,false);
  3239. }
  3240. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  3241. {
  3242. if(key.state != SDL_PRESSED)
  3243. return;
  3244. int &cur = (which ? a2 : a1),
  3245. &sec = (which ? a1 : a2),
  3246. ncur = cur;
  3247. if (key.keysym.sym == SDLK_BACKSPACE)
  3248. {
  3249. ncur /= 10;
  3250. }
  3251. else if(key.keysym.sym == SDLK_TAB)
  3252. {
  3253. which = !which;
  3254. }
  3255. else
  3256. {
  3257. int number = key.keysym.sym - SDLK_0;
  3258. if (number < 0 || number > 9) //not a number pressed
  3259. {
  3260. return;
  3261. }
  3262. ncur = cur*10 + number;
  3263. }
  3264. int delta = ncur - cur;
  3265. if(delta > sec)
  3266. {
  3267. cur += sec;
  3268. sec = 0;
  3269. }
  3270. slider->moveTo(which ? ncur : a1+a2-ncur);
  3271. }
  3272. void CSplitWindow::clickLeft( boost::logic::tribool down )
  3273. {
  3274. if(down)
  3275. {
  3276. Point click(LOCPLINT->current->motion.x,LOCPLINT->current->motion.y);
  3277. click -= pos.topLeft();
  3278. if(Rect(19,216,105,40).isIn(click))
  3279. which = 0;
  3280. else if(Rect(175,216,105,40).isIn(click))
  3281. which = 1;
  3282. }
  3283. }
  3284. void CCreInfoWindow::show(SDL_Surface * to)
  3285. {
  3286. char pom[15];
  3287. blitAt(bitmap,pos.x,pos.y,to);
  3288. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  3289. if(++anf==4)
  3290. anf=0;
  3291. if(count.size())
  3292. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly,to);
  3293. if(upgrade)
  3294. upgrade->show(to);
  3295. if(dismiss)
  3296. dismiss->show(to);
  3297. if(ok)
  3298. ok->show(to);
  3299. }
  3300. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  3301. :ok(0),dismiss(0),upgrade(0),type(Type),dsm(Dsm)
  3302. {
  3303. //active = false;
  3304. anf = 0;
  3305. c = &CGI->creh->creatures[Cid];
  3306. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  3307. pos.x = screen->w/2 - bitmap->w/2;
  3308. pos.y = screen->h/2 - bitmap->h/2;
  3309. pos.w = bitmap->w;
  3310. pos.h = bitmap->h;
  3311. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  3312. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3313. anim = new CCreaturePic(c);
  3314. if(!type) anim->anim->setType(2);
  3315. char pom[75];int hlp=0;
  3316. if(creatureCount)
  3317. {
  3318. SDL_itoa(creatureCount,pom,10);
  3319. count = pom;
  3320. }
  3321. printAtMiddle(c->namePl,149,30,GEOR13,tytulowy,bitmap); //creature name
  3322. //atttack
  3323. printAt(CGI->generaltexth->primarySkillNames[0],155,48,GEOR13,zwykly,bitmap);
  3324. SDL_itoa(c->attack,pom,10);
  3325. if(State && State->attackBonus)
  3326. {
  3327. int hlp;
  3328. if(c->attack > 0)
  3329. hlp = log10f(c->attack)+2;
  3330. else
  3331. hlp = 2;
  3332. pom[hlp-1] = ' '; pom[hlp] = '(';
  3333. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  3334. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  3335. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3336. }
  3337. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  3338. //defense
  3339. printAt(CGI->generaltexth->primarySkillNames[1],155,67,GEOR13,zwykly,bitmap);
  3340. SDL_itoa(c->defence,pom,10);
  3341. if(State && State->defenseBonus)
  3342. {
  3343. int hlp;
  3344. if(c->defence > 0)
  3345. hlp = log10f(c->defence)+2;
  3346. else
  3347. hlp = 2;
  3348. pom[hlp-1] = ' '; pom[hlp] = '(';
  3349. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  3350. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  3351. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3352. }
  3353. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  3354. //shots
  3355. if(c->shots)
  3356. {
  3357. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  3358. if(State)
  3359. sprintf(pom,"%d(%d)",c->shots,State->shotsLeft);
  3360. else
  3361. SDL_itoa(c->shots,pom,10);
  3362. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  3363. }
  3364. //damage
  3365. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  3366. SDL_itoa(c->damageMin,pom,10);
  3367. if(c->damageMin > 0)
  3368. hlp = log10f(c->damageMin)+2;
  3369. else
  3370. hlp = 2;
  3371. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  3372. SDL_itoa(c->damageMax,pom+hlp+2,10);
  3373. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  3374. //health
  3375. printAt(CGI->generaltexth->allTexts[388],155,124,GEOR13,zwykly,bitmap);
  3376. SDL_itoa(c->hitPoints,pom,10);
  3377. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  3378. //remaining health
  3379. if(State && State->currentHealth)
  3380. {
  3381. printAt(CGI->generaltexth->allTexts[200],155,143,GEOR13,zwykly,bitmap);
  3382. SDL_itoa(State->currentHealth,pom,10);
  3383. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  3384. }
  3385. //speed
  3386. printAt(CGI->generaltexth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  3387. SDL_itoa(c->speed,pom,10);
  3388. if(State && State->speedBonus)
  3389. {
  3390. int hlp;
  3391. if(c->speed > 0)
  3392. hlp = log10f(c->speed)+2;
  3393. else
  3394. hlp = 2;
  3395. pom[hlp-1] = ' '; pom[hlp] = '(';
  3396. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  3397. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  3398. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3399. }
  3400. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  3401. //luck and morale
  3402. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  3403. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  3404. //print abilities text - if r-click popup
  3405. if(type)
  3406. {
  3407. if(Upg && ui)
  3408. {
  3409. bool enough = true;
  3410. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  3411. {
  3412. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  3413. enough = false;
  3414. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  3415. }
  3416. if(enough)
  3417. {
  3418. CFunctionList<void()> fs[2];
  3419. fs[0] += Upg;
  3420. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  3421. CFunctionList<void()> cfl;
  3422. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  3423. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  3424. }
  3425. else
  3426. {
  3427. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  3428. upgrade->callback.funcs.clear();
  3429. upgrade->bitmapOffset = 2;
  3430. }
  3431. }
  3432. if(Dsm)
  3433. {
  3434. CFunctionList<void()> fs[2];
  3435. //on dismiss confirmed
  3436. fs[0] += Dsm; //dismiss
  3437. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  3438. CFunctionList<void()> cfl;
  3439. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  3440. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  3441. }
  3442. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  3443. }
  3444. else
  3445. {
  3446. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  3447. }
  3448. //spell effects
  3449. if(State)
  3450. {
  3451. int printed=0; //how many effect pics have been printed
  3452. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  3453. {
  3454. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  3455. ++printed;
  3456. if(printed >= 3)
  3457. {
  3458. break;
  3459. }
  3460. }
  3461. }
  3462. }
  3463. CCreInfoWindow::~CCreInfoWindow()
  3464. {
  3465. SDL_FreeSurface(bitmap);
  3466. delete anim;
  3467. delete upgrade;
  3468. delete ok;
  3469. delete dismiss;
  3470. for(int i=0; i<upgResCost.size();i++)
  3471. delete upgResCost[i];
  3472. }
  3473. void CCreInfoWindow::activate()
  3474. {
  3475. //if(active) return;
  3476. //active = true;
  3477. if(!type)
  3478. ClickableR::activate();
  3479. if(ok)
  3480. ok->activate();
  3481. if(dismiss)
  3482. dismiss->activate();
  3483. if(upgrade)
  3484. upgrade->activate();
  3485. }
  3486. void CCreInfoWindow::close()
  3487. {
  3488. LOCPLINT->popIntTotally(this);
  3489. }
  3490. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  3491. {
  3492. if(down)
  3493. return;
  3494. close();
  3495. }
  3496. void CCreInfoWindow::dismissF()
  3497. {
  3498. dsm();
  3499. close();
  3500. }
  3501. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  3502. {
  3503. }
  3504. void CCreInfoWindow::deactivate()
  3505. {
  3506. //if(!active) return;
  3507. //active = false;
  3508. if(!type)
  3509. ClickableR::deactivate();
  3510. if(ok)
  3511. ok->deactivate();
  3512. if(dismiss)
  3513. dismiss->deactivate();
  3514. if(upgrade)
  3515. upgrade->deactivate();
  3516. }
  3517. void CLevelWindow::close()
  3518. {
  3519. for(int i=0;i<comps.size();i++)
  3520. {
  3521. if(comps[i]->selected)
  3522. {
  3523. cb(i);
  3524. break;
  3525. }
  3526. }
  3527. LOCPLINT->popIntTotally(this);
  3528. LOCPLINT->showingDialog->setn(false);
  3529. }
  3530. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  3531. {
  3532. LOCPLINT->showingDialog->setn(true);
  3533. heroType = hero->subID;
  3534. cb = callback;
  3535. for(int i=0;i<skills.size();i++)
  3536. {
  3537. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  3538. comps.back()->assignedKeys.insert(SDLK_1 + i);
  3539. }
  3540. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  3541. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  3542. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3543. pos.x = screen->w/2 - bitmap->w/2;
  3544. pos.y = screen->h/2 - bitmap->h/2;
  3545. pos.w = bitmap->w;
  3546. pos.h = bitmap->h;
  3547. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  3548. //draw window
  3549. char buf[100], buf2[100];
  3550. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  3551. sprintf(buf,buf2,hero->name.c_str());
  3552. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  3553. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  3554. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  3555. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  3556. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  3557. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  3558. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  3559. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  3560. for(int i=0;i<comps.size();i++)
  3561. {
  3562. comps[i]->pos.x = curx+pos.x;
  3563. comps[i]->pos.y = 326+pos.y;
  3564. if( i < (comps.size()-1) )
  3565. {
  3566. curx += 44 + 18; //skill width + margin to "or"
  3567. blitAt(ort,curx,346,bitmap);
  3568. curx += ort->w + 18;
  3569. }
  3570. }
  3571. if(comps.size() > 1)
  3572. {
  3573. ok->block(true);
  3574. }
  3575. else if(comps.size() == 1)
  3576. {
  3577. comps[0]->select(true);
  3578. }
  3579. SDL_FreeSurface(ort);
  3580. }
  3581. void CLevelWindow::selectionChanged(unsigned to)
  3582. {
  3583. if(ok->blocked)
  3584. ok->block(false);
  3585. for(int i=0;i<comps.size();i++)
  3586. if(i==to)
  3587. comps[i]->select(true);
  3588. else
  3589. comps[i]->select(false);
  3590. }
  3591. CLevelWindow::~CLevelWindow()
  3592. {
  3593. delete ok;
  3594. for(int i=0;i<comps.size();i++)
  3595. delete comps[i];
  3596. SDL_FreeSurface(bitmap);
  3597. }
  3598. void CLevelWindow::activate()
  3599. {
  3600. ok->activate();
  3601. for(int i=0;i<comps.size();i++)
  3602. comps[i]->activate();
  3603. }
  3604. void CLevelWindow::deactivate()
  3605. {
  3606. ok->deactivate();
  3607. for(int i=0;i<comps.size();i++)
  3608. comps[i]->deactivate();
  3609. }
  3610. void CLevelWindow::show(SDL_Surface * to)
  3611. {
  3612. blitAt(bitmap,pos.x,pos.y,to);
  3613. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y,to);
  3614. ok->show(to);
  3615. for(int i=0;i<comps.size();i++)
  3616. comps[i]->show(to);
  3617. }
  3618. void CMinorResDataBar::show(SDL_Surface * to)
  3619. {
  3620. blitAt(bg,pos.x,pos.y,to);
  3621. char buf[30];
  3622. for (int i=0;i<7;i++)
  3623. {
  3624. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3625. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly,to);
  3626. }
  3627. std::vector<std::string> temp;
  3628. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  3629. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  3630. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  3631. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  3632. CGI->generaltexth->allTexts[62]
  3633. +": %s, "
  3634. + CGI->generaltexth->allTexts[63]
  3635. + ": %s, "
  3636. + CGI->generaltexth->allTexts[64]
  3637. + ": %s",temp)
  3638. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly,to);
  3639. }
  3640. CMinorResDataBar::CMinorResDataBar()
  3641. {
  3642. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  3643. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3644. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3645. pos.x = 7;
  3646. pos.y = 575;
  3647. pos.w = bg->w;
  3648. pos.h = bg->h;
  3649. }
  3650. CMinorResDataBar::~CMinorResDataBar()
  3651. {
  3652. SDL_FreeSurface(bg);
  3653. }
  3654. SDL_Surface * CCustomImgComponent::getImg()
  3655. {
  3656. return bmp;
  3657. }
  3658. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  3659. :bmp(sur), free(freeSur)
  3660. {
  3661. init(Type,Subtype,Val);
  3662. }
  3663. CCustomImgComponent::~CCustomImgComponent()
  3664. {
  3665. if(free)
  3666. SDL_FreeSurface(bmp);
  3667. }
  3668. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  3669. {
  3670. left = Left;
  3671. type = Type;
  3672. id = ID;
  3673. }
  3674. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  3675. {
  3676. SDL_Surface *hlp = getSurface();
  3677. blitAt(hlp,pos.x+19,pos.y+9,to);
  3678. }
  3679. void CMarketplaceWindow::CTradeableItem::clickLeft( boost::logic::tribool down )
  3680. {
  3681. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(LOCPLINT->topInt());
  3682. assert(mw);
  3683. if(down)
  3684. {
  3685. if(left)
  3686. {
  3687. if(mw->hLeft != this)
  3688. mw->hLeft = this;
  3689. else
  3690. return;
  3691. }
  3692. else
  3693. {
  3694. if(mw->hRight != this)
  3695. mw->hRight = this;
  3696. else
  3697. return;
  3698. }
  3699. mw->selectionChanged(left);
  3700. }
  3701. }
  3702. void CMarketplaceWindow::CTradeableItem::activate()
  3703. {
  3704. ClickableL::activate();
  3705. }
  3706. void CMarketplaceWindow::CTradeableItem::deactivate()
  3707. {
  3708. ClickableL::deactivate();
  3709. }
  3710. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  3711. {
  3712. switch(type)
  3713. {
  3714. case 0:
  3715. return graphics->resources32->ourImages[id].bitmap;
  3716. case 1:
  3717. return graphics->artDefs->ourImages[id].bitmap;
  3718. default:
  3719. return NULL;
  3720. }
  3721. }
  3722. void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  3723. {
  3724. i.resize(amount);
  3725. for(int j=0;j<amount;j++)
  3726. {
  3727. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  3728. i[j]->pos = p[j];
  3729. }
  3730. }
  3731. void CMarketplaceWindow::setMode( int mode )
  3732. {
  3733. std::vector<SDL_Rect> lpos, rpos;
  3734. clear();
  3735. switch(mode)
  3736. {
  3737. case 0:
  3738. {
  3739. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  3740. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  3741. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  3742. bg = SDL_ConvertSurface(bg2,screen->format,0);
  3743. SDL_FreeSurface(bg2);
  3744. lpos += genRect(66,69,39,180), genRect(66,69,122,180), genRect(66,69,204,180),
  3745. genRect(66,69,39,259), genRect(66,69,122,259), genRect(66,69,204,259),
  3746. genRect(66,69,122,338);
  3747. for(int i=0;i<lpos.size();i++)
  3748. {
  3749. lpos[i].x += pos.x;
  3750. lpos[i].y += pos.y;
  3751. rpos.push_back(lpos[i]);
  3752. rpos[rpos.size()-1].x += 288;
  3753. }
  3754. initItems(left,lpos,0,7,true,NULL);
  3755. initItems(right,rpos,0,7,false,NULL);
  3756. printAtMiddle(CGI->generaltexth->allTexts[158],303,28,GEORXX,tytulowy,bg); //title
  3757. printAtMiddle(CGI->generaltexth->allTexts[270],158,148,GEOR13,zwykly,bg); //kingdom res.
  3758. printAtMiddle(CGI->generaltexth->allTexts[168],450,148,GEOR13,zwykly,bg); //available for trade
  3759. }
  3760. }
  3761. }
  3762. void CMarketplaceWindow::clear()
  3763. {
  3764. for(int i=0;i<left.size();i++)
  3765. delete left[i];
  3766. for(int i=0;i<right.size();i++)
  3767. delete right[i];
  3768. left.clear();
  3769. right.clear();
  3770. SDL_FreeSurface(bg);
  3771. }
  3772. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  3773. {
  3774. mode = Mode;
  3775. bg = NULL;
  3776. ok = max = deal = NULL;
  3777. pos.x = screen->w/2 - 300;
  3778. pos.y = screen->h/2 - 296;
  3779. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  3780. setMode(mode);
  3781. hLeft = hRight = NULL;
  3782. ok = new AdventureMapButton("","",boost::bind(&CPlayerInterface::popIntTotally,LOCPLINT,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  3783. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  3784. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  3785. max->block(true);
  3786. deal->block(true);
  3787. }
  3788. CMarketplaceWindow::~CMarketplaceWindow()
  3789. {
  3790. clear();
  3791. delete slider;
  3792. }
  3793. void CMarketplaceWindow::show(SDL_Surface * to)
  3794. {
  3795. blitAt(bg,pos,to);
  3796. if(hRight)
  3797. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  3798. if(hLeft)
  3799. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  3800. ok->show(to);
  3801. deal->show(to);
  3802. max->show(to);
  3803. slider->show(to);
  3804. for(int i=0;i<left.size();i++)
  3805. left[i]->show(to);
  3806. for(int i=0;i<right.size();i++)
  3807. right[i]->show(to);
  3808. if(mode==0)
  3809. {
  3810. char buf[15];
  3811. for(int i=0;i<left.size();i++)
  3812. {
  3813. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3814. printAtMiddle(buf,left[i]->pos.x+35,left[i]->pos.y+56,GEOR13,zwykly,to);
  3815. }
  3816. if(hLeft) //print prices
  3817. {
  3818. for(int i=0; i<right.size();i++)
  3819. {
  3820. if(right[i]->id != hLeft->id)
  3821. printAtMiddle(rSubs[i],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  3822. else
  3823. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  3824. }
  3825. }
  3826. if(hLeft && hRight && hLeft->id!= hRight->id)
  3827. {
  3828. blitAt(hLeft->getSurface(),pos.x+142,pos.y+457,to);
  3829. blitAt(hRight->getSurface(),pos.x+430,pos.y+457,to);
  3830. SDL_itoa(slider->value * r1,buf,10);
  3831. printAtMiddle(buf,pos.x+158,pos.y+504,GEOR13,zwykly,to);
  3832. SDL_itoa(slider->value * r2,buf,10);
  3833. printAtMiddle(buf,pos.x+446,pos.y+504,GEOR13,zwykly,to);
  3834. }
  3835. }
  3836. }
  3837. void CMarketplaceWindow::activate()
  3838. {
  3839. for(int i=0;i<left.size();i++)
  3840. left[i]->activate();
  3841. for(int i=0;i<right.size();i++)
  3842. right[i]->activate();
  3843. ok->activate();
  3844. max->activate();
  3845. deal->activate();
  3846. slider->activate();
  3847. }
  3848. void CMarketplaceWindow::deactivate()
  3849. {
  3850. for(int i=0;i<left.size();i++)
  3851. left[i]->deactivate();
  3852. for(int i=0;i<right.size();i++)
  3853. right[i]->deactivate();
  3854. ok->deactivate();
  3855. max->deactivate();
  3856. deal->deactivate();
  3857. slider->deactivate();
  3858. }
  3859. void CMarketplaceWindow::setMax()
  3860. {
  3861. slider->moveTo(slider->amount);
  3862. }
  3863. void CMarketplaceWindow::makeDeal()
  3864. {
  3865. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  3866. slider->moveTo(0);
  3867. hLeft = NULL;
  3868. selectionChanged(true);
  3869. }
  3870. void CMarketplaceWindow::sliderMoved( int to )
  3871. {
  3872. }
  3873. void CMarketplaceWindow::selectionChanged(bool side)
  3874. {
  3875. if(hLeft && hRight && hLeft->id!= hRight->id)
  3876. {
  3877. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  3878. slider->amount = LOCPLINT->cb->getResourceAmount(hLeft->id) / r1;
  3879. slider->moveTo(0);
  3880. max->block(false);
  3881. deal->block(false);
  3882. }
  3883. else
  3884. {
  3885. max->block(true);
  3886. deal->block(true);
  3887. slider->amount = 0;
  3888. slider->moveTo(0);
  3889. }
  3890. if(side && hLeft) //left selection changed, recalculate offers
  3891. {
  3892. rSubs.clear();
  3893. rSubs.resize(right.size());
  3894. int h1, h2;
  3895. for(int i=0;i<right.size();i++)
  3896. {
  3897. std::ostringstream oss;
  3898. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  3899. oss << h2;
  3900. if(h1!=1)
  3901. oss << "/" << h1;
  3902. rSubs[i] = oss.str();
  3903. }
  3904. }
  3905. }
  3906. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  3907. {
  3908. this->pos = pos;
  3909. background = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  3910. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  3911. //printing texts
  3912. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 240, 32, GEOR16, tytulowy, background); //window title
  3913. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 65, GEOR16, tytulowy, background); //hero speed
  3914. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 131, GEOR16, tytulowy, background); //enemy speed
  3915. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 197, GEOR16, tytulowy, background); //map scroll speed
  3916. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 263, GEOR16, tytulowy, background); //video quality
  3917. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 348, GEOR16, tytulowy, background); //music volume
  3918. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 414, GEOR16, tytulowy, background); //effects volume
  3919. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 283, 57, GEOR16, zwykly, background); //show move path
  3920. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 283, 89, GEOR16, zwykly, background); //show hero reminder
  3921. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 283, 121, GEOR16, zwykly, background); //quick combat
  3922. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 283, 153, GEOR16, zwykly, background); //video subtitles
  3923. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 283, 185, GEOR16, zwykly, background); //town building outlines
  3924. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 283, 217, GEOR16, zwykly, background); //spell book animation
  3925. //setting up buttons
  3926. save = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 516, 354, "SOSAVE.DEF", SDLK_s);
  3927. std::swap(save->imgs[0][0], save->imgs[0][1]);
  3928. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 405, 471, "soquit.def", SDLK_q);
  3929. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  3930. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 516, 471, "soretrn.def", SDLK_RETURN);
  3931. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3932. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  3933. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3934. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].first),CGI->generaltexth->zelp[349].second, "sysopb1.def", 187, 134, 1);
  3935. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].first),CGI->generaltexth->zelp[350].second, "sysopb2.def", 235, 134, 2);
  3936. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].first),CGI->generaltexth->zelp[351].second, "sysopb3.def", 283, 134, 4);
  3937. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].first),CGI->generaltexth->zelp[352].second, "sysopb4.def", 331, 134, 8);
  3938. heroMoveSpeed->select(owner->heroMoveSpeed, 1);
  3939. heroMoveSpeed->onChange = boost::bind(&CPlayerInterface::setHeroMoveSpeed, owner, _1);
  3940. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3941. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].first),CGI->generaltexth->zelp[357].second, "sysopb9.def", 187, 267, 1);
  3942. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].first),CGI->generaltexth->zelp[358].second, "sysob10.def", 251, 267, 2);
  3943. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].first),CGI->generaltexth->zelp[359].second, "sysob11.def", 315, 267, 4);
  3944. mapScrollSpeed->select(owner->mapScrollingSpeed, 1);
  3945. mapScrollSpeed->onChange = boost::bind(&CPlayerInterface::setMapScrollingSpeed, owner, _1);
  3946. }
  3947. CSystemOptionsWindow::~CSystemOptionsWindow()
  3948. {
  3949. SDL_FreeSurface(background);
  3950. delete save;
  3951. delete quitGame;
  3952. delete backToMap;
  3953. delete heroMoveSpeed;
  3954. delete mapScrollSpeed;
  3955. }
  3956. void CSystemOptionsWindow::bquitf()
  3957. {
  3958. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(exit, 0), boost::bind(&CSystemOptionsWindow::activate, this), false);
  3959. }
  3960. void CSystemOptionsWindow::breturnf()
  3961. {
  3962. LOCPLINT->popIntTotally(this);
  3963. }
  3964. void CSystemOptionsWindow::bsavef()
  3965. {
  3966. using namespace boost::posix_time;
  3967. std::ostringstream fnameStream;
  3968. fnameStream << second_clock::local_time();
  3969. std::string fname = fnameStream.str();
  3970. boost::algorithm::replace_all(fname,":","");
  3971. boost::algorithm::replace_all(fname," ","-");
  3972. LOCPLINT->showYesNoDialog("Do you want to save current game as " + fname, std::vector<SComponent*>(), boost::bind(&CCallback::save, LOCPLINT->cb, fname), boost::bind(&CSystemOptionsWindow::activate, this), false);
  3973. }
  3974. void CSystemOptionsWindow::activate()
  3975. {
  3976. save->activate();
  3977. quitGame->activate();
  3978. backToMap->activate();
  3979. heroMoveSpeed->activate();
  3980. mapScrollSpeed->activate();
  3981. }
  3982. void CSystemOptionsWindow::deactivate()
  3983. {
  3984. save->deactivate();
  3985. quitGame->deactivate();
  3986. backToMap->deactivate();
  3987. heroMoveSpeed->deactivate();
  3988. mapScrollSpeed->deactivate();
  3989. }
  3990. void CSystemOptionsWindow::show(SDL_Surface *to)
  3991. {
  3992. SDL_BlitSurface(background, NULL, to, &pos);
  3993. save->show(to);
  3994. quitGame->show(to);
  3995. backToMap->show(to);
  3996. heroMoveSpeed->show(to);
  3997. mapScrollSpeed->show(to);
  3998. }
  3999. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  4000. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  4001. {
  4002. if(H1)
  4003. selected = 0;
  4004. else
  4005. selected = -1;
  4006. bg = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  4007. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  4008. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  4009. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,GEOR16,tytulowy,bg);
  4010. printAtMiddle("2500",320,328,GEOR13,zwykly,bg);
  4011. printAtMiddle(CGI->generaltexth->jktexts[38],146,283,GEOR16,tytulowy,bg);
  4012. printAtMiddleWB(gossip,200,220,GEOR13,50,zwykly,bg);
  4013. pos.w = bg->w;
  4014. pos.h = bg->h;
  4015. pos.x = (screen->w-bg->w)/2;
  4016. pos.y = (screen->h-bg->h)/2;
  4017. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  4018. h1.pos.x += pos.x;
  4019. h2.pos.x += pos.x;
  4020. h1.pos.y += pos.y;
  4021. h2.pos.y += pos.y;
  4022. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"",boost::bind(&CTavernWindow::close,this),pos.x+310,pos.y+428,"ICANCEL.DEF",SDLK_ESCAPE);
  4023. recruit = new AdventureMapButton("","",boost::bind(&CTavernWindow::recruitb,this),pos.x+272,pos.y+355,"TPTAV01.DEF",SDLK_RETURN);
  4024. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"",0,pos.x+22,pos.y+428,"TPTAV02.DEF",SDLK_t);
  4025. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  4026. {
  4027. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  4028. recruit->block(2);
  4029. }
  4030. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  4031. {
  4032. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  4033. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  4034. recruit->block(2);
  4035. }
  4036. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  4037. {
  4038. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  4039. recruit->block(2);
  4040. }
  4041. else
  4042. {
  4043. if(H1)
  4044. {
  4045. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  4046. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",H1->name);
  4047. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",H1->type->heroClass->name);
  4048. }
  4049. else
  4050. recruit->block(1);
  4051. }
  4052. }
  4053. void CTavernWindow::recruitb()
  4054. {
  4055. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  4056. close();
  4057. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  4058. }
  4059. CTavernWindow::~CTavernWindow()
  4060. {
  4061. SDL_FreeSurface(bg);
  4062. delete cancel;
  4063. delete thiefGuild;
  4064. delete recruit;
  4065. delete bar;
  4066. }
  4067. void CTavernWindow::activate()
  4068. {
  4069. thiefGuild->activate();
  4070. cancel->activate();
  4071. if(h1.h)
  4072. h1.activate();
  4073. if(h2.h)
  4074. h2.activate();
  4075. recruit->activate();
  4076. LOCPLINT->statusbar = bar;
  4077. }
  4078. void CTavernWindow::deactivate()
  4079. {
  4080. thiefGuild->deactivate();
  4081. cancel->deactivate();
  4082. if(h1.h)
  4083. h1.deactivate();
  4084. if(h2.h)
  4085. h2.deactivate();
  4086. recruit->deactivate();
  4087. }
  4088. void CTavernWindow::close()
  4089. {
  4090. LOCPLINT->popIntTotally(this);
  4091. }
  4092. void CTavernWindow::show(SDL_Surface * to)
  4093. {
  4094. blitAt(bg,pos.x,pos.y,to);
  4095. if(h1.h)
  4096. h1.show(to);
  4097. if(h2.h)
  4098. h2.show(to);
  4099. thiefGuild->show(to);
  4100. cancel->show(to);
  4101. recruit->show(to);
  4102. bar->show(to);
  4103. if(selected >= 0)
  4104. {
  4105. HeroPortrait *sel = selected ? &h2 : &h1;
  4106. char descr[300];
  4107. int artifs = sel->h->artifWorn.size()+sel->h->artifacts.size();
  4108. for(int i=13; i<=17; i++) //war machines and spellbook doesn't count
  4109. if(vstd::contains(sel->h->artifWorn,i))
  4110. artifs--;
  4111. sprintf_s(descr,300,CGI->generaltexth->allTexts[215].c_str(),
  4112. sel->h->name.c_str(),sel->h->level,sel->h->type->heroClass->name.c_str(),artifs);
  4113. printAtMiddleWB(descr,pos.x+146,pos.y+389,GEOR13,40,zwykly,to);
  4114. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  4115. }
  4116. }
  4117. void CTavernWindow::HeroPortrait::clickLeft(boost::logic::tribool down)
  4118. {
  4119. if(pressedL && !down)
  4120. as();
  4121. ClickableL::clickLeft(down);
  4122. }
  4123. void CTavernWindow::HeroPortrait::activate()
  4124. {
  4125. ClickableL::activate();
  4126. ClickableR::activate();
  4127. Hoverable::activate();
  4128. }
  4129. void CTavernWindow::HeroPortrait::deactivate()
  4130. {
  4131. ClickableL::deactivate();
  4132. ClickableR::deactivate();
  4133. Hoverable::deactivate();
  4134. }
  4135. void CTavernWindow::HeroPortrait::clickRight(boost::logic::tribool down)
  4136. {
  4137. if(down)
  4138. {
  4139. LOCPLINT->adventureInt->heroWindow->setHero(h);
  4140. LOCPLINT->pushInt(new CRClickPopupInt(LOCPLINT->adventureInt->heroWindow,false));
  4141. }
  4142. }
  4143. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  4144. :as(sel,id)
  4145. {
  4146. h = H;
  4147. pos.x = x;
  4148. pos.y = y;
  4149. pos.w = 58;
  4150. pos.h = 64;
  4151. if(H)
  4152. {
  4153. hoverName = CGI->generaltexth->tavernInfo[4];
  4154. boost::algorithm::replace_first(hoverName,"%s",H->name);
  4155. }
  4156. }
  4157. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  4158. {
  4159. blitAt(graphics->portraitLarge[h->subID],pos,to);
  4160. }
  4161. void CTavernWindow::HeroPortrait::hover( bool on )
  4162. {
  4163. Hoverable::hover(on);
  4164. if(on)
  4165. LOCPLINT->statusbar->print(hoverName);
  4166. else
  4167. LOCPLINT->statusbar->clear();
  4168. }
  4169. void CInGameConsole::activate()
  4170. {
  4171. KeyInterested::activate();
  4172. }
  4173. void CInGameConsole::deactivate()
  4174. {
  4175. KeyInterested::deactivate();
  4176. }
  4177. void CInGameConsole::show(SDL_Surface * to)
  4178. {
  4179. int number = 0;
  4180. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  4181. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  4182. {
  4183. SDL_Color green = {0,0xff,0,0};
  4184. Point leftBottomCorner(0, screen->h);
  4185. if(LOCPLINT->battleInt)
  4186. {
  4187. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  4188. }
  4189. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, TNRB16, green);
  4190. if(SDL_GetTicks() - it->second > defaultTimeout)
  4191. {
  4192. toDel.push_back(it);
  4193. }
  4194. }
  4195. for(int it=0; it<toDel.size(); ++it)
  4196. {
  4197. texts.erase(toDel[it]);
  4198. }
  4199. }
  4200. void CInGameConsole::print(const std::string &txt)
  4201. {
  4202. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  4203. if(texts.size() > maxDisplayedTexts)
  4204. {
  4205. texts.pop_front();
  4206. }
  4207. }
  4208. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  4209. {
  4210. if(key.type != SDL_KEYDOWN) return;
  4211. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  4212. switch(key.keysym.sym)
  4213. {
  4214. case SDLK_TAB:
  4215. {
  4216. if(captureAllKeys)
  4217. {
  4218. captureAllKeys = false;
  4219. endEnteringText(false);
  4220. }
  4221. else
  4222. {
  4223. captureAllKeys = true;
  4224. startEnteringText();
  4225. }
  4226. break;
  4227. }
  4228. case SDLK_RETURN: //enter key
  4229. {
  4230. if(enteredText.size() > 0 && captureAllKeys)
  4231. {
  4232. captureAllKeys = false;
  4233. endEnteringText(true);
  4234. }
  4235. break;
  4236. }
  4237. case SDLK_BACKSPACE:
  4238. {
  4239. if(enteredText.size() > 1)
  4240. {
  4241. enteredText.resize(enteredText.size()-1);
  4242. enteredText[enteredText.size()-1] = '_';
  4243. refreshEnteredText();
  4244. }
  4245. break;
  4246. }
  4247. case SDLK_UP: //up arrow
  4248. {
  4249. if(prevEntDisp == -1)
  4250. {
  4251. prevEntDisp = previouslyEntered.size() - 1;
  4252. enteredText = previouslyEntered[prevEntDisp] + "_";
  4253. refreshEnteredText();
  4254. }
  4255. else if( prevEntDisp > 0)
  4256. {
  4257. --prevEntDisp;
  4258. enteredText = previouslyEntered[prevEntDisp] + "_";
  4259. refreshEnteredText();
  4260. }
  4261. break;
  4262. }
  4263. case SDLK_DOWN: //down arrow
  4264. {
  4265. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  4266. {
  4267. ++prevEntDisp;
  4268. enteredText = previouslyEntered[prevEntDisp] + "_";
  4269. refreshEnteredText();
  4270. }
  4271. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  4272. {
  4273. prevEntDisp = -1;
  4274. enteredText = "_";
  4275. refreshEnteredText();
  4276. }
  4277. break;
  4278. }
  4279. default:
  4280. {
  4281. if(enteredText.size() > 0)
  4282. {
  4283. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  4284. {
  4285. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  4286. enteredText += "_";
  4287. refreshEnteredText();
  4288. }
  4289. }
  4290. break;
  4291. }
  4292. }
  4293. }
  4294. void CInGameConsole::startEnteringText()
  4295. {
  4296. enteredText = "_";
  4297. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  4298. {
  4299. LOCPLINT->statusbar->print(enteredText);
  4300. }
  4301. else if(LOCPLINT->battleInt)
  4302. {
  4303. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  4304. }
  4305. }
  4306. void CInGameConsole::endEnteringText(bool printEnteredText)
  4307. {
  4308. prevEntDisp = -1;
  4309. if(printEnteredText)
  4310. {
  4311. std::string txt = enteredText.substr(0, enteredText.size()-1);
  4312. LOCPLINT->cb->sendMessage(txt);
  4313. previouslyEntered.push_back(txt);
  4314. print(txt);
  4315. }
  4316. enteredText = "";
  4317. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  4318. {
  4319. LOCPLINT->statusbar->clear();
  4320. }
  4321. else if(LOCPLINT->battleInt)
  4322. {
  4323. LOCPLINT->battleInt->console->ingcAlter = "";
  4324. }
  4325. }
  4326. void CInGameConsole::refreshEnteredText()
  4327. {
  4328. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  4329. {
  4330. LOCPLINT->statusbar->print(enteredText);
  4331. }
  4332. else if(LOCPLINT->battleInt)
  4333. {
  4334. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  4335. }
  4336. }
  4337. CInGameConsole::CInGameConsole() : defaultTimeout(10000), maxDisplayedTexts(10), prevEntDisp(-1)
  4338. {
  4339. }
  4340. void CGarrisonWindow::close()
  4341. {
  4342. LOCPLINT->popIntTotally(this);
  4343. }
  4344. void CGarrisonWindow::activate()
  4345. {
  4346. split->activate();
  4347. quit->activate();
  4348. garr->activate();
  4349. }
  4350. void CGarrisonWindow::deactivate()
  4351. {
  4352. split->deactivate();
  4353. quit->deactivate();
  4354. garr->deactivate();
  4355. }
  4356. void CGarrisonWindow::show(SDL_Surface * to)
  4357. {
  4358. blitAt(bg,pos,to);
  4359. split->show(to);
  4360. quit->show(to);
  4361. garr->show(to);
  4362. }
  4363. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down )
  4364. {
  4365. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  4366. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  4367. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  4368. pos.x = screen->w/2 - bg->w/2;
  4369. pos.y = screen->h/2 - bg->h/2;
  4370. pos.w = screen->w;
  4371. pos.h = screen->h;
  4372. garr = new CGarrisonInt(pos.x+92, pos.y+129, 4, 30, bg, 92, 129, up, down);
  4373. split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF");
  4374. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  4375. }
  4376. CGarrisonWindow::~CGarrisonWindow()
  4377. {
  4378. SDL_FreeSurface(bg);
  4379. delete split;
  4380. delete quit;
  4381. delete garr;
  4382. }
  4383. IShowActivable::IShowActivable()
  4384. {
  4385. type = 0;
  4386. }
  4387. CWindowWithGarrison::CWindowWithGarrison()
  4388. {
  4389. type |= WITH_GARRISON;
  4390. }
  4391. void CRClickPopupInt::show(SDL_Surface * to)
  4392. {
  4393. inner->show(to);
  4394. }
  4395. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  4396. {
  4397. inner = our;
  4398. delInner = deleteInt;
  4399. }
  4400. CRClickPopupInt::~CRClickPopupInt()
  4401. {
  4402. if(delInner)
  4403. delete inner;
  4404. }