CPlayerInterface.cpp 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809
  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();
  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();
  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();
  483. }
  484. }
  485. void CGarrisonInt::splitClick()
  486. {
  487. if(!highlighted)
  488. return;
  489. splitting = !splitting;
  490. //show();
  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();
  2496. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2497. }
  2498. if (which>=items.size())
  2499. return;
  2500. selected = which;
  2501. LOCPLINT->adventureInt->centerOn(items[which].first->pos);
  2502. LOCPLINT->adventureInt->selection = items[which].first;
  2503. //recalculationg path in case of something has changed on map
  2504. if(items[which].second)
  2505. {
  2506. CPath * newPath = LOCPLINT->cb->getPath(items[which].second->startPos(), items[which].second->endPos(), items[which].first);
  2507. delete items[which].second;
  2508. LOCPLINT->adventureInt->terrain.currentPath = items[which].second = newPath;
  2509. }
  2510. else
  2511. {
  2512. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2513. }
  2514. //recalculated and assigned
  2515. //draw();
  2516. //LOCPLINT->adventureInt->townList.draw();
  2517. //LOCPLINT->adventureInt->infoBar.draw(NULL);
  2518. LOCPLINT->cb->setSelection(items[which].first);
  2519. }
  2520. void CHeroList::clickLeft(tribool down)
  2521. {
  2522. if (down)
  2523. {
  2524. /***************************ARROWS*****************************************/
  2525. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2526. {
  2527. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2528. pressed = true;
  2529. return;
  2530. }
  2531. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2532. {
  2533. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2534. pressed = false;
  2535. return;
  2536. }
  2537. /***************************HEROES*****************************************/
  2538. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2539. hx-=pos.x;
  2540. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2541. int ny = hy/32;
  2542. if (ny>=SIZE || ny<0)
  2543. return;
  2544. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2545. LOCPLINT->openHeroWindow(items[selected].first);//print hero screen
  2546. select(ny+from);
  2547. }
  2548. else
  2549. {
  2550. if (indeterminate(pressed))
  2551. return;
  2552. if (pressed) //up
  2553. {
  2554. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2555. pressed = indeterminate;
  2556. if (!down)
  2557. {
  2558. from--;
  2559. if (from<0)
  2560. from=0;
  2561. //draw();
  2562. }
  2563. }
  2564. else if (!pressed) //down
  2565. {
  2566. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2567. pressed = indeterminate;
  2568. if (!down)
  2569. {
  2570. from++;
  2571. //if (from<items.size()-5)
  2572. // from=items.size()-5;
  2573. //draw();
  2574. }
  2575. }
  2576. else
  2577. throw 0;
  2578. }
  2579. }
  2580. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  2581. {
  2582. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2583. {
  2584. if (from>0)
  2585. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  2586. else
  2587. LOCPLINT->adventureInt->statusbar.clear();
  2588. return;
  2589. }
  2590. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2591. {
  2592. if ((items.size()-from) > SIZE)
  2593. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  2594. else
  2595. LOCPLINT->adventureInt->statusbar.clear();
  2596. return;
  2597. }
  2598. //if not buttons then heroes
  2599. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2600. hx-=pos.x;
  2601. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2602. int ny = hy/32;
  2603. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2604. {
  2605. LOCPLINT->adventureInt->statusbar.clear();
  2606. return;
  2607. }
  2608. std::vector<std::string> temp;
  2609. temp.push_back(items[from+ny].first->name);
  2610. temp.push_back(items[from+ny].first->type->heroClass->name);
  2611. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  2612. //select(ny+from);
  2613. }
  2614. void CHeroList::clickRight(tribool down)
  2615. {
  2616. if (down)
  2617. {
  2618. /***************************ARROWS*****************************************/
  2619. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2620. {
  2621. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  2622. }
  2623. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2624. {
  2625. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  2626. }
  2627. //if not buttons then heroes
  2628. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2629. hx-=pos.x;
  2630. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2631. int ny = hy/32;
  2632. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2633. {
  2634. return;
  2635. }
  2636. //show popup
  2637. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[items[from+ny].first->subID],
  2638. LOCPLINT->current->motion.x-graphics->heroWins[items[from+ny].first->subID]->w,
  2639. LOCPLINT->current->motion.y-graphics->heroWins[items[from+ny].first->subID]->h,
  2640. false);
  2641. LOCPLINT->pushInt(ip);
  2642. }
  2643. else
  2644. {
  2645. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  2646. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  2647. }
  2648. }
  2649. void CHeroList::hover (bool on)
  2650. {
  2651. }
  2652. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  2653. {
  2654. }
  2655. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  2656. {
  2657. if(toRemove) //remove specific hero
  2658. {
  2659. for (std::vector<std::pair<const CGHeroInstance*, CPath *> >::iterator i=items.begin(); i != items.end(); i++)
  2660. {
  2661. if(i->first == toRemove)
  2662. {
  2663. delete i->second;
  2664. items.erase(i);
  2665. break;
  2666. }
  2667. }
  2668. }
  2669. else
  2670. {
  2671. items.clear();
  2672. genList();
  2673. }
  2674. if(selected>=items.size())
  2675. select(items.size()-1);
  2676. if(items.size()==0)
  2677. LOCPLINT->adventureInt->townList.select(0);
  2678. else
  2679. select(selected);
  2680. }
  2681. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  2682. {
  2683. int ser = -1;
  2684. for(int i=0; i<items.size() && ser<0; i++)
  2685. if(items[i].first->subID == which->subID)
  2686. ser = i;
  2687. ser -= from;
  2688. if(ser<0 || ser > SIZE) return;
  2689. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  2690. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  2691. }
  2692. void CHeroList::draw(SDL_Surface * to)
  2693. {
  2694. for (int iT=0+from;iT<SIZE+from;iT++)
  2695. {
  2696. int i = iT-from;
  2697. if (iT>=items.size())
  2698. {
  2699. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  2700. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  2701. blitAt(empty,posporx,pospory+i*32,to);
  2702. continue;
  2703. }
  2704. const CGHeroInstance *cur = items[iT].first;
  2705. int pom = cur->movement / 100;
  2706. if (pom>25) pom=25;
  2707. if (pom<0) pom=0;
  2708. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  2709. pom = cur->mana / 5;
  2710. if (pom>25) pom=25;
  2711. if (pom<0) pom=0;
  2712. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  2713. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  2714. blitAt(temp,posporx,pospory+i*32,to);
  2715. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2716. {
  2717. blitAt(selection,posporx,pospory+i*32,to);
  2718. }
  2719. //TODO: support for custom portraits
  2720. }
  2721. if (from>0)
  2722. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  2723. else
  2724. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  2725. if (items.size()-from > SIZE)
  2726. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  2727. else
  2728. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  2729. }
  2730. int CHeroList::getPosOfHero(const CArmedInstance* h)
  2731. {
  2732. return vstd::findPos(
  2733. items,h,
  2734. boost::bind(vstd::equal<std::pair<const CGHeroInstance*, CPath *>,
  2735. const CArmedInstance *,const CGHeroInstance*>,_1,
  2736. &std::pair<const CGHeroInstance*, CPath *>::first,_2));
  2737. }
  2738. CTownList::~CTownList()
  2739. {
  2740. delete arrup;
  2741. delete arrdo;
  2742. }
  2743. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  2744. :CList(Size)
  2745. {
  2746. arrup = CDefHandler::giveDef(arrupg);
  2747. arrdo = CDefHandler::giveDef(arrdog);
  2748. pos.x = x;
  2749. pos.y = y;
  2750. pos.w = std::max(arrdo->width, arrup->width);
  2751. pos.h = arrdo->height + arrup->height + Size*32;
  2752. arrupp.x=x;
  2753. arrupp.y=y;
  2754. arrupp.w=arrup->width;
  2755. arrupp.h=arrup->height;
  2756. arrdop.x=x;
  2757. arrdop.y=y+arrup->height+32*SIZE;
  2758. arrdop.w=arrdo->width;
  2759. arrdop.h=arrdo->height;
  2760. posporx = arrdop.x;
  2761. pospory = arrupp.y + arrupp.h;
  2762. pressed = indeterminate;
  2763. from = 0;
  2764. }
  2765. void CTownList::genList()
  2766. {
  2767. int howMany = LOCPLINT->cb->howManyTowns();
  2768. for (int i=0;i<howMany;i++)
  2769. {
  2770. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  2771. }
  2772. }
  2773. void CTownList::select(int which)
  2774. {
  2775. if (which>=items.size())
  2776. return;
  2777. selected = which;
  2778. if(!fun.empty())
  2779. fun();
  2780. }
  2781. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  2782. {
  2783. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2784. {
  2785. if (from>0)
  2786. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[306].first);
  2787. else
  2788. LOCPLINT->statusbar->clear();
  2789. return;
  2790. }
  2791. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2792. {
  2793. if ((items.size()-from) > SIZE)
  2794. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[307].first);
  2795. else
  2796. LOCPLINT->statusbar->clear();
  2797. return;
  2798. }
  2799. //if not buttons then towns
  2800. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2801. hx-=pos.x;
  2802. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2803. int ny = hy/32;
  2804. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2805. {
  2806. LOCPLINT->statusbar->clear();
  2807. return;
  2808. };
  2809. LOCPLINT->statusbar->print(items[from+ny]->name);
  2810. }
  2811. void CTownList::clickLeft(tribool down)
  2812. {
  2813. if (down)
  2814. {
  2815. /***************************ARROWS*****************************************/
  2816. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2817. {
  2818. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2819. pressed = true;
  2820. return;
  2821. }
  2822. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2823. {
  2824. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2825. pressed = false;
  2826. return;
  2827. }
  2828. /***************************TOWNS*****************************************/
  2829. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2830. hx-=pos.x;
  2831. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2832. int ny = hy/32;
  2833. if (ny>SIZE || ny<0)
  2834. return;
  2835. if(LOCPLINT->topInt() == LOCPLINT->adventureInt
  2836. && (ny+from)==selected
  2837. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  2838. )
  2839. LOCPLINT->openTownWindow(items[selected]);//print town screen
  2840. else
  2841. select(ny+from);
  2842. }
  2843. else
  2844. {
  2845. if (indeterminate(pressed))
  2846. return;
  2847. if (pressed) //up
  2848. {
  2849. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2850. pressed = indeterminate;
  2851. if (!down)
  2852. {
  2853. from--;
  2854. if (from<0)
  2855. from=0;
  2856. //draw();
  2857. }
  2858. }
  2859. else if (!pressed) //down
  2860. {
  2861. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2862. pressed = indeterminate;
  2863. if (!down)
  2864. {
  2865. from++;
  2866. //if (from<items.size()-5)
  2867. // from=items.size()-5;
  2868. //draw();
  2869. }
  2870. }
  2871. else
  2872. throw 0;
  2873. }
  2874. }
  2875. void CTownList::clickRight(tribool down)
  2876. {
  2877. if (down)
  2878. {
  2879. /***************************ARROWS*****************************************/
  2880. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2881. {
  2882. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  2883. }
  2884. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2885. {
  2886. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  2887. }
  2888. //if not buttons then towns
  2889. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2890. hx-=pos.x;
  2891. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2892. int ny = hy/32;
  2893. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2894. {
  2895. return;
  2896. }
  2897. //show popup
  2898. CInfoPopup * ip = new CInfoPopup(
  2899. graphics->townWins[items[from+ny]->id],
  2900. LOCPLINT->current->motion.x-graphics->townWins[items[from+ny]->id]->w,
  2901. LOCPLINT->current->motion.y-graphics->townWins[items[from+ny]->id]->h,
  2902. false);
  2903. LOCPLINT->pushInt(ip);
  2904. }
  2905. else
  2906. {
  2907. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  2908. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  2909. }
  2910. }
  2911. void CTownList::hover (bool on)
  2912. {
  2913. }
  2914. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  2915. {
  2916. }
  2917. void CTownList::draw(SDL_Surface * to)
  2918. {
  2919. for (int iT=0+from;iT<SIZE+from;iT++)
  2920. {
  2921. int i = iT-from;
  2922. if (iT>=items.size())
  2923. {
  2924. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  2925. continue;
  2926. }
  2927. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32,to);
  2928. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2929. {
  2930. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  2931. }
  2932. }
  2933. if (from>0)
  2934. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  2935. else
  2936. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  2937. if (items.size()-from>SIZE)
  2938. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  2939. else
  2940. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  2941. }
  2942. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  2943. :c(cre),big(Big)
  2944. {
  2945. anim = new CCreatureAnimation(cre->animDefName);
  2946. }
  2947. CCreaturePic::~CCreaturePic()
  2948. {
  2949. delete anim;
  2950. }
  2951. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  2952. {
  2953. SDL_Rect dst;
  2954. if(big)
  2955. {
  2956. blitAt(graphics->backgrounds[c->faction],x,y,to);//curx-50,pos.y+130-65);
  2957. dst = genRect(130,100,x,y);
  2958. }
  2959. else
  2960. {
  2961. blitAt(graphics->backgroundsm[c->faction],x,y,to);//curx-50,pos.y+130-65);
  2962. dst = genRect(120,100,x,y);
  2963. }
  2964. if(c->isDoubleWide())
  2965. x-=15;
  2966. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  2967. }
  2968. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  2969. {
  2970. //TODO: write
  2971. return NULL;
  2972. }
  2973. void CRecrutationWindow::close()
  2974. {
  2975. LOCPLINT->popIntTotally(this);
  2976. }
  2977. void CRecrutationWindow::Max()
  2978. {
  2979. slider->moveTo(slider->amount);
  2980. }
  2981. void CRecrutationWindow::Buy()
  2982. {
  2983. recruit(creatures[which].ID,slider->value);
  2984. close();
  2985. }
  2986. void CRecrutationWindow::Cancel()
  2987. {
  2988. close();
  2989. }
  2990. void CRecrutationWindow::sliderMoved(int to)
  2991. {
  2992. buy->block(!to);
  2993. }
  2994. void CRecrutationWindow::clickLeft(tribool down)
  2995. {
  2996. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2997. for(int i=0;i<creatures.size();i++)
  2998. {
  2999. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  3000. {
  3001. which = i;
  3002. int newAmount = std::min(amounts[i],creatures[i].amount);
  3003. slider->amount = newAmount;
  3004. if(slider->value > newAmount)
  3005. slider->moveTo(newAmount);
  3006. else
  3007. slider->moveTo(slider->value);
  3008. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3009. for(int j=0;j<creatures.size();j++)
  3010. {
  3011. if(which==j)
  3012. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  3013. else
  3014. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  3015. curx += 120;
  3016. }
  3017. break;
  3018. }
  3019. curx += 120;
  3020. }
  3021. }
  3022. void CRecrutationWindow::clickRight( boost::logic::tribool down )
  3023. {
  3024. if(down)
  3025. {
  3026. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3027. for(int i=0;i<creatures.size();i++)
  3028. {
  3029. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  3030. {
  3031. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID,0,0,NULL,NULL,NULL,NULL);
  3032. LOCPLINT->pushInt(popup);
  3033. break;
  3034. }
  3035. curx += 120;
  3036. }
  3037. }
  3038. }
  3039. void CRecrutationWindow::activate()
  3040. {
  3041. ClickableL::activate();
  3042. ClickableR::activate();
  3043. buy->activate();
  3044. max->activate();
  3045. cancel->activate();
  3046. slider->activate();
  3047. LOCPLINT->statusbar = bar;
  3048. }
  3049. void CRecrutationWindow::deactivate()
  3050. {
  3051. ClickableL::deactivate();
  3052. ClickableR::deactivate();
  3053. buy->deactivate();
  3054. max->deactivate();
  3055. cancel->deactivate();
  3056. slider->deactivate();
  3057. }
  3058. void CRecrutationWindow::show(SDL_Surface * to)
  3059. {
  3060. static char c=0;
  3061. blitAt(bitmap,pos.x,pos.y,to);
  3062. buy->show(to);
  3063. max->show(to);
  3064. cancel->show(to);
  3065. slider->show(to);
  3066. char pom[15];
  3067. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  3068. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,to);
  3069. SDL_itoa(slider->value,pom,10); //recruit
  3070. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,to);
  3071. 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"
  3072. int curx = pos.x+115-creatures[which].res.size()*16;
  3073. for(int i=0;i<creatures[which].res.size();i++)
  3074. {
  3075. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,to);
  3076. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,to);
  3077. SDL_itoa(creatures[which].res[i].second,pom,10);
  3078. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,to);
  3079. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  3080. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,to);
  3081. curx+=32;
  3082. }
  3083. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3084. for(int i=0;i<creatures.size();i++)
  3085. {
  3086. creatures[i].pic->blitPic(to,curx-50,pos.y+130-65,!(c%4));
  3087. curx += 120;
  3088. }
  3089. c++;
  3090. bar->show(to);
  3091. }
  3092. CRecrutationWindow::CRecrutationWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  3093. :recruit(Recruit)
  3094. {
  3095. which = 0;
  3096. creatures.resize(Creatures.size());
  3097. amounts.resize(Creatures.size());
  3098. for(int i=0;i<creatures.size();i++)
  3099. {
  3100. creatures[i].amount = Creatures[i].second;
  3101. creatures[i].ID = Creatures[i].first;
  3102. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  3103. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  3104. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  3105. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  3106. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  3107. }
  3108. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  3109. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  3110. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  3111. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3112. SDL_FreeSurface(hhlp);
  3113. pos.x = screen->w/2 - bitmap->w/2;
  3114. pos.y = screen->h/2 - bitmap->h/2;
  3115. pos.w = bitmap->w;
  3116. pos.h = bitmap->h;
  3117. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  3118. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecrutationWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  3119. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecrutationWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  3120. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecrutationWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  3121. 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);
  3122. std::string pom;
  3123. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  3124. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  3125. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  3126. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  3127. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  3128. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  3129. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  3130. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  3131. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  3132. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  3133. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  3134. //border for creatures
  3135. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3136. for(int i=0;i<creatures.size();i++)
  3137. {
  3138. creatures[i].pos.x = curx+1;
  3139. creatures[i].pos.y = 65;
  3140. creatures[i].pos.w = 100;
  3141. creatures[i].pos.h = 130;
  3142. if(which==i)
  3143. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  3144. else
  3145. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  3146. curx += 120;
  3147. }
  3148. if(!creatures[0].amount || !amounts[0])
  3149. {
  3150. max->block(true);
  3151. slider->block(true);
  3152. }
  3153. //buy->block(true); //not needed, will be blocked by initing slider on 0
  3154. }
  3155. CRecrutationWindow::~CRecrutationWindow()
  3156. {
  3157. for(int i=0;i<creatures.size();i++)
  3158. {
  3159. delete creatures[i].pic;
  3160. }
  3161. delete max;
  3162. delete buy;
  3163. delete cancel;
  3164. SDL_FreeSurface(bitmap);
  3165. delete slider;
  3166. delete bar;
  3167. }
  3168. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  3169. {
  3170. last = Last;
  3171. which = 1;
  3172. c=cid;
  3173. slider = NULL;
  3174. gar = Owner;
  3175. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  3176. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  3177. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3178. pos.x = screen->w/2 - bitmap->w/2;
  3179. pos.y = screen->h/2 - bitmap->h/2;
  3180. pos.w = bitmap->w;
  3181. pos.h = bitmap->h;
  3182. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  3183. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  3184. int sliderPositions = max - (last>=0) - (last==2);
  3185. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,sliderPositions,val,true);
  3186. a1 = max-val;
  3187. a2 = val;
  3188. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  3189. anim->anim->setType(1);
  3190. std::string title = CGI->generaltexth->allTexts[256];
  3191. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  3192. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  3193. }
  3194. CSplitWindow::~CSplitWindow()
  3195. {
  3196. SDL_FreeSurface(bitmap);
  3197. delete ok;
  3198. delete cancel;
  3199. delete slider;
  3200. delete anim;
  3201. }
  3202. void CSplitWindow::activate()
  3203. {
  3204. ClickableL::activate();
  3205. KeyInterested::activate();
  3206. ok->activate();
  3207. cancel->activate();
  3208. slider->activate();
  3209. }
  3210. void CSplitWindow::deactivate()
  3211. {
  3212. ClickableL::deactivate();
  3213. KeyInterested::deactivate();
  3214. ok->deactivate();
  3215. cancel->deactivate();
  3216. slider->deactivate();
  3217. }
  3218. void CSplitWindow::split()
  3219. {
  3220. gar->splitStacks(a2);
  3221. close();
  3222. }
  3223. void CSplitWindow::close()
  3224. {
  3225. LOCPLINT->popIntTotally(this);
  3226. }
  3227. void CSplitWindow::sliderMoved(int to)
  3228. {
  3229. int all = a1+a2;
  3230. a2 = to + (last==1 || last==2);
  3231. if(slider)
  3232. a1 = all - a2;
  3233. }
  3234. void CSplitWindow::show(SDL_Surface * to)
  3235. {
  3236. blitAt(bitmap,pos.x,pos.y,to);
  3237. ok->show(to);
  3238. cancel->show(to);
  3239. slider->show(to);
  3240. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,GEOR16,zwykly,to);
  3241. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,GEOR16,zwykly,to);
  3242. anim->blitPic(to,pos.x+20,pos.y+54,false);
  3243. anim->blitPic(to,pos.x+177,pos.y+54,false);
  3244. }
  3245. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  3246. {
  3247. if(key.state != SDL_PRESSED)
  3248. return;
  3249. int &cur = (which ? a2 : a1),
  3250. &sec = (which ? a1 : a2),
  3251. ncur = cur;
  3252. if (key.keysym.sym == SDLK_BACKSPACE)
  3253. {
  3254. ncur /= 10;
  3255. }
  3256. else if(key.keysym.sym == SDLK_TAB)
  3257. {
  3258. which = !which;
  3259. }
  3260. else
  3261. {
  3262. int number = key.keysym.sym - SDLK_0;
  3263. if (number < 0 || number > 9) //not a number pressed
  3264. {
  3265. return;
  3266. }
  3267. ncur = cur*10 + number;
  3268. }
  3269. int delta = ncur - cur;
  3270. if(delta > sec)
  3271. {
  3272. cur += sec;
  3273. sec = 0;
  3274. }
  3275. slider->moveTo(which ? ncur : a1+a2-ncur);
  3276. }
  3277. void CSplitWindow::clickLeft( boost::logic::tribool down )
  3278. {
  3279. if(down)
  3280. {
  3281. Point click(LOCPLINT->current->motion.x,LOCPLINT->current->motion.y);
  3282. click -= pos.topLeft();
  3283. if(Rect(19,216,105,40).isIn(click))
  3284. which = 0;
  3285. else if(Rect(175,216,105,40).isIn(click))
  3286. which = 1;
  3287. }
  3288. }
  3289. void CCreInfoWindow::show(SDL_Surface * to)
  3290. {
  3291. char pom[15];
  3292. blitAt(bitmap,pos.x,pos.y,to);
  3293. anim->blitPic(to,pos.x+21,pos.y+48,(type) && !(anf%4));
  3294. if(++anf==4)
  3295. anf=0;
  3296. if(count.size())
  3297. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly,to);
  3298. if(upgrade)
  3299. upgrade->show(to);
  3300. if(dismiss)
  3301. dismiss->show(to);
  3302. if(ok)
  3303. ok->show(to);
  3304. }
  3305. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  3306. :ok(0),dismiss(0),upgrade(0),type(Type),dsm(Dsm)
  3307. {
  3308. //active = false;
  3309. anf = 0;
  3310. c = &CGI->creh->creatures[Cid];
  3311. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  3312. pos.x = screen->w/2 - bitmap->w/2;
  3313. pos.y = screen->h/2 - bitmap->h/2;
  3314. pos.w = bitmap->w;
  3315. pos.h = bitmap->h;
  3316. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  3317. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3318. anim = new CCreaturePic(c);
  3319. if(!type) anim->anim->setType(2);
  3320. char pom[75];int hlp=0;
  3321. if(creatureCount)
  3322. {
  3323. SDL_itoa(creatureCount,pom,10);
  3324. count = pom;
  3325. }
  3326. printAtMiddle(c->namePl,149,30,GEOR13,tytulowy,bitmap); //creature name
  3327. //atttack
  3328. printAt(CGI->generaltexth->primarySkillNames[0],155,48,GEOR13,zwykly,bitmap);
  3329. SDL_itoa(c->attack,pom,10);
  3330. if(State && State->attackBonus)
  3331. {
  3332. int hlp;
  3333. if(c->attack > 0)
  3334. hlp = log10f(c->attack)+2;
  3335. else
  3336. hlp = 2;
  3337. pom[hlp-1] = ' '; pom[hlp] = '(';
  3338. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  3339. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  3340. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3341. }
  3342. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  3343. //defense
  3344. printAt(CGI->generaltexth->primarySkillNames[1],155,67,GEOR13,zwykly,bitmap);
  3345. SDL_itoa(c->defence,pom,10);
  3346. if(State && State->defenseBonus)
  3347. {
  3348. int hlp;
  3349. if(c->defence > 0)
  3350. hlp = log10f(c->defence)+2;
  3351. else
  3352. hlp = 2;
  3353. pom[hlp-1] = ' '; pom[hlp] = '(';
  3354. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  3355. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  3356. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3357. }
  3358. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  3359. //shots
  3360. if(c->shots)
  3361. {
  3362. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  3363. if(State)
  3364. sprintf(pom,"%d(%d)",c->shots,State->shotsLeft);
  3365. else
  3366. SDL_itoa(c->shots,pom,10);
  3367. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  3368. }
  3369. //damage
  3370. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  3371. SDL_itoa(c->damageMin,pom,10);
  3372. if(c->damageMin > 0)
  3373. hlp = log10f(c->damageMin)+2;
  3374. else
  3375. hlp = 2;
  3376. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  3377. SDL_itoa(c->damageMax,pom+hlp+2,10);
  3378. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  3379. //health
  3380. printAt(CGI->generaltexth->allTexts[388],155,124,GEOR13,zwykly,bitmap);
  3381. SDL_itoa(c->hitPoints,pom,10);
  3382. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  3383. //remaining health
  3384. if(State && State->currentHealth)
  3385. {
  3386. printAt(CGI->generaltexth->allTexts[200],155,143,GEOR13,zwykly,bitmap);
  3387. SDL_itoa(State->currentHealth,pom,10);
  3388. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  3389. }
  3390. //speed
  3391. printAt(CGI->generaltexth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  3392. SDL_itoa(c->speed,pom,10);
  3393. if(State && State->speedBonus)
  3394. {
  3395. int hlp;
  3396. if(c->speed > 0)
  3397. hlp = log10f(c->speed)+2;
  3398. else
  3399. hlp = 2;
  3400. pom[hlp-1] = ' '; pom[hlp] = '(';
  3401. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  3402. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  3403. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3404. }
  3405. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  3406. //luck and morale
  3407. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  3408. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  3409. //print abilities text - if r-click popup
  3410. if(type)
  3411. {
  3412. if(Upg && ui)
  3413. {
  3414. bool enough = true;
  3415. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  3416. {
  3417. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  3418. enough = false;
  3419. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  3420. }
  3421. if(enough)
  3422. {
  3423. CFunctionList<void()> fs[2];
  3424. fs[0] += Upg;
  3425. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  3426. CFunctionList<void()> cfl;
  3427. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false);
  3428. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  3429. }
  3430. else
  3431. {
  3432. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  3433. upgrade->callback.funcs.clear();
  3434. upgrade->bitmapOffset = 2;
  3435. }
  3436. }
  3437. if(Dsm)
  3438. {
  3439. CFunctionList<void()> fs[2];
  3440. //on dismiss confirmed
  3441. fs[0] += Dsm; //dismiss
  3442. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  3443. CFunctionList<void()> cfl;
  3444. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  3445. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  3446. }
  3447. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  3448. }
  3449. else
  3450. {
  3451. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  3452. }
  3453. //spell effects
  3454. if(State)
  3455. {
  3456. int printed=0; //how many effect pics have been printed
  3457. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  3458. {
  3459. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  3460. ++printed;
  3461. if(printed >= 3)
  3462. {
  3463. break;
  3464. }
  3465. }
  3466. }
  3467. }
  3468. CCreInfoWindow::~CCreInfoWindow()
  3469. {
  3470. SDL_FreeSurface(bitmap);
  3471. delete anim;
  3472. delete upgrade;
  3473. delete ok;
  3474. delete dismiss;
  3475. for(int i=0; i<upgResCost.size();i++)
  3476. delete upgResCost[i];
  3477. }
  3478. void CCreInfoWindow::activate()
  3479. {
  3480. //if(active) return;
  3481. //active = true;
  3482. if(!type)
  3483. ClickableR::activate();
  3484. if(ok)
  3485. ok->activate();
  3486. if(dismiss)
  3487. dismiss->activate();
  3488. if(upgrade)
  3489. upgrade->activate();
  3490. }
  3491. void CCreInfoWindow::close()
  3492. {
  3493. LOCPLINT->popIntTotally(this);
  3494. }
  3495. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  3496. {
  3497. if(down)
  3498. return;
  3499. close();
  3500. }
  3501. void CCreInfoWindow::dismissF()
  3502. {
  3503. dsm();
  3504. close();
  3505. }
  3506. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  3507. {
  3508. }
  3509. void CCreInfoWindow::deactivate()
  3510. {
  3511. //if(!active) return;
  3512. //active = false;
  3513. if(!type)
  3514. ClickableR::deactivate();
  3515. if(ok)
  3516. ok->deactivate();
  3517. if(dismiss)
  3518. dismiss->deactivate();
  3519. if(upgrade)
  3520. upgrade->deactivate();
  3521. }
  3522. void CLevelWindow::close()
  3523. {
  3524. for(int i=0;i<comps.size();i++)
  3525. {
  3526. if(comps[i]->selected)
  3527. {
  3528. cb(i);
  3529. break;
  3530. }
  3531. }
  3532. LOCPLINT->popIntTotally(this);
  3533. LOCPLINT->showingDialog->setn(false);
  3534. }
  3535. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  3536. {
  3537. LOCPLINT->showingDialog->setn(true);
  3538. heroType = hero->subID;
  3539. cb = callback;
  3540. for(int i=0;i<skills.size();i++)
  3541. {
  3542. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  3543. comps.back()->assignedKeys.insert(SDLK_1 + i);
  3544. }
  3545. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  3546. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  3547. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3548. pos.x = screen->w/2 - bitmap->w/2;
  3549. pos.y = screen->h/2 - bitmap->h/2;
  3550. pos.w = bitmap->w;
  3551. pos.h = bitmap->h;
  3552. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  3553. //draw window
  3554. char buf[100], buf2[100];
  3555. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  3556. sprintf(buf,buf2,hero->name.c_str());
  3557. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  3558. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  3559. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  3560. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  3561. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  3562. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  3563. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  3564. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  3565. for(int i=0;i<comps.size();i++)
  3566. {
  3567. comps[i]->pos.x = curx+pos.x;
  3568. comps[i]->pos.y = 326+pos.y;
  3569. if( i < (comps.size()-1) )
  3570. {
  3571. curx += 44 + 18; //skill width + margin to "or"
  3572. blitAt(ort,curx,346,bitmap);
  3573. curx += ort->w + 18;
  3574. }
  3575. }
  3576. if(comps.size() > 1)
  3577. {
  3578. ok->block(true);
  3579. }
  3580. else if(comps.size() == 1)
  3581. {
  3582. comps[0]->select(true);
  3583. }
  3584. SDL_FreeSurface(ort);
  3585. }
  3586. void CLevelWindow::selectionChanged(unsigned to)
  3587. {
  3588. if(ok->blocked)
  3589. ok->block(false);
  3590. for(int i=0;i<comps.size();i++)
  3591. if(i==to)
  3592. comps[i]->select(true);
  3593. else
  3594. comps[i]->select(false);
  3595. }
  3596. CLevelWindow::~CLevelWindow()
  3597. {
  3598. delete ok;
  3599. for(int i=0;i<comps.size();i++)
  3600. delete comps[i];
  3601. SDL_FreeSurface(bitmap);
  3602. }
  3603. void CLevelWindow::activate()
  3604. {
  3605. ok->activate();
  3606. for(int i=0;i<comps.size();i++)
  3607. comps[i]->activate();
  3608. }
  3609. void CLevelWindow::deactivate()
  3610. {
  3611. ok->deactivate();
  3612. for(int i=0;i<comps.size();i++)
  3613. comps[i]->deactivate();
  3614. }
  3615. void CLevelWindow::show(SDL_Surface * to)
  3616. {
  3617. blitAt(bitmap,pos.x,pos.y,to);
  3618. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y,to);
  3619. ok->show(to);
  3620. for(int i=0;i<comps.size();i++)
  3621. comps[i]->show(to);
  3622. }
  3623. void CMinorResDataBar::show(SDL_Surface * to)
  3624. {
  3625. blitAt(bg,pos.x,pos.y,to);
  3626. char buf[30];
  3627. for (int i=0;i<7;i++)
  3628. {
  3629. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3630. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly,to);
  3631. }
  3632. std::vector<std::string> temp;
  3633. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  3634. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  3635. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  3636. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  3637. CGI->generaltexth->allTexts[62]
  3638. +": %s, "
  3639. + CGI->generaltexth->allTexts[63]
  3640. + ": %s, "
  3641. + CGI->generaltexth->allTexts[64]
  3642. + ": %s",temp)
  3643. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly,to);
  3644. }
  3645. CMinorResDataBar::CMinorResDataBar()
  3646. {
  3647. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  3648. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3649. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3650. pos.x = 7;
  3651. pos.y = 575;
  3652. pos.w = bg->w;
  3653. pos.h = bg->h;
  3654. }
  3655. CMinorResDataBar::~CMinorResDataBar()
  3656. {
  3657. SDL_FreeSurface(bg);
  3658. }
  3659. SDL_Surface * CCustomImgComponent::getImg()
  3660. {
  3661. return bmp;
  3662. }
  3663. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  3664. :bmp(sur), free(freeSur)
  3665. {
  3666. init(Type,Subtype,Val);
  3667. }
  3668. CCustomImgComponent::~CCustomImgComponent()
  3669. {
  3670. if(free)
  3671. SDL_FreeSurface(bmp);
  3672. }
  3673. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  3674. {
  3675. left = Left;
  3676. type = Type;
  3677. id = ID;
  3678. }
  3679. void CMarketplaceWindow::CTradeableItem::show(SDL_Surface * to)
  3680. {
  3681. SDL_Surface *hlp = getSurface();
  3682. blitAt(hlp,pos.x+19,pos.y+9,to);
  3683. }
  3684. void CMarketplaceWindow::CTradeableItem::clickLeft( boost::logic::tribool down )
  3685. {
  3686. CMarketplaceWindow *mw = dynamic_cast<CMarketplaceWindow *>(LOCPLINT->topInt());
  3687. assert(mw);
  3688. if(down)
  3689. {
  3690. if(left)
  3691. {
  3692. if(mw->hLeft != this)
  3693. mw->hLeft = this;
  3694. else
  3695. return;
  3696. }
  3697. else
  3698. {
  3699. if(mw->hRight != this)
  3700. mw->hRight = this;
  3701. else
  3702. return;
  3703. }
  3704. mw->selectionChanged(left);
  3705. }
  3706. }
  3707. void CMarketplaceWindow::CTradeableItem::activate()
  3708. {
  3709. ClickableL::activate();
  3710. }
  3711. void CMarketplaceWindow::CTradeableItem::deactivate()
  3712. {
  3713. ClickableL::deactivate();
  3714. }
  3715. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  3716. {
  3717. switch(type)
  3718. {
  3719. case 0:
  3720. return graphics->resources32->ourImages[id].bitmap;
  3721. case 1:
  3722. return graphics->artDefs->ourImages[id].bitmap;
  3723. default:
  3724. return NULL;
  3725. }
  3726. }
  3727. void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  3728. {
  3729. i.resize(amount);
  3730. for(int j=0;j<amount;j++)
  3731. {
  3732. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  3733. i[j]->pos = p[j];
  3734. }
  3735. }
  3736. void CMarketplaceWindow::setMode( int mode )
  3737. {
  3738. std::vector<SDL_Rect> lpos, rpos;
  3739. clear();
  3740. switch(mode)
  3741. {
  3742. case 0:
  3743. {
  3744. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  3745. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  3746. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  3747. bg = SDL_ConvertSurface(bg2,screen->format,0);
  3748. SDL_FreeSurface(bg2);
  3749. lpos += genRect(66,69,39,180), genRect(66,69,122,180), genRect(66,69,204,180),
  3750. genRect(66,69,39,259), genRect(66,69,122,259), genRect(66,69,204,259),
  3751. genRect(66,69,122,338);
  3752. for(int i=0;i<lpos.size();i++)
  3753. {
  3754. lpos[i].x += pos.x;
  3755. lpos[i].y += pos.y;
  3756. rpos.push_back(lpos[i]);
  3757. rpos[rpos.size()-1].x += 288;
  3758. }
  3759. initItems(left,lpos,0,7,true,NULL);
  3760. initItems(right,rpos,0,7,false,NULL);
  3761. printAtMiddle(CGI->generaltexth->allTexts[158],303,28,GEORXX,tytulowy,bg); //title
  3762. printAtMiddle(CGI->generaltexth->allTexts[270],158,148,GEOR13,zwykly,bg); //kingdom res.
  3763. printAtMiddle(CGI->generaltexth->allTexts[168],450,148,GEOR13,zwykly,bg); //available for trade
  3764. }
  3765. }
  3766. }
  3767. void CMarketplaceWindow::clear()
  3768. {
  3769. for(int i=0;i<left.size();i++)
  3770. delete left[i];
  3771. for(int i=0;i<right.size();i++)
  3772. delete right[i];
  3773. left.clear();
  3774. right.clear();
  3775. SDL_FreeSurface(bg);
  3776. }
  3777. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  3778. {
  3779. mode = Mode;
  3780. bg = NULL;
  3781. ok = max = deal = NULL;
  3782. pos.x = screen->w/2 - 300;
  3783. pos.y = screen->h/2 - 296;
  3784. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  3785. setMode(mode);
  3786. hLeft = hRight = NULL;
  3787. ok = new AdventureMapButton("","",boost::bind(&CPlayerInterface::popIntTotally,LOCPLINT,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  3788. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  3789. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  3790. max->block(true);
  3791. deal->block(true);
  3792. }
  3793. CMarketplaceWindow::~CMarketplaceWindow()
  3794. {
  3795. clear();
  3796. delete slider;
  3797. }
  3798. void CMarketplaceWindow::show(SDL_Surface * to)
  3799. {
  3800. blitAt(bg,pos,to);
  3801. if(hRight)
  3802. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  3803. if(hLeft)
  3804. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  3805. ok->show(to);
  3806. deal->show(to);
  3807. max->show(to);
  3808. slider->show(to);
  3809. for(int i=0;i<left.size();i++)
  3810. left[i]->show(to);
  3811. for(int i=0;i<right.size();i++)
  3812. right[i]->show(to);
  3813. if(mode==0)
  3814. {
  3815. char buf[15];
  3816. for(int i=0;i<left.size();i++)
  3817. {
  3818. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3819. printAtMiddle(buf,left[i]->pos.x+35,left[i]->pos.y+56,GEOR13,zwykly,to);
  3820. }
  3821. if(hLeft) //print prices
  3822. {
  3823. for(int i=0; i<right.size();i++)
  3824. {
  3825. if(right[i]->id != hLeft->id)
  3826. printAtMiddle(rSubs[i],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  3827. else
  3828. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly,to);
  3829. }
  3830. }
  3831. if(hLeft && hRight && hLeft->id!= hRight->id)
  3832. {
  3833. blitAt(hLeft->getSurface(),pos.x+142,pos.y+457,to);
  3834. blitAt(hRight->getSurface(),pos.x+430,pos.y+457,to);
  3835. SDL_itoa(slider->value * r1,buf,10);
  3836. printAtMiddle(buf,pos.x+158,pos.y+504,GEOR13,zwykly,to);
  3837. SDL_itoa(slider->value * r2,buf,10);
  3838. printAtMiddle(buf,pos.x+446,pos.y+504,GEOR13,zwykly,to);
  3839. }
  3840. }
  3841. }
  3842. void CMarketplaceWindow::activate()
  3843. {
  3844. for(int i=0;i<left.size();i++)
  3845. left[i]->activate();
  3846. for(int i=0;i<right.size();i++)
  3847. right[i]->activate();
  3848. ok->activate();
  3849. max->activate();
  3850. deal->activate();
  3851. slider->activate();
  3852. }
  3853. void CMarketplaceWindow::deactivate()
  3854. {
  3855. for(int i=0;i<left.size();i++)
  3856. left[i]->deactivate();
  3857. for(int i=0;i<right.size();i++)
  3858. right[i]->deactivate();
  3859. ok->deactivate();
  3860. max->deactivate();
  3861. deal->deactivate();
  3862. slider->deactivate();
  3863. }
  3864. void CMarketplaceWindow::setMax()
  3865. {
  3866. slider->moveTo(slider->amount);
  3867. }
  3868. void CMarketplaceWindow::makeDeal()
  3869. {
  3870. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  3871. slider->moveTo(0);
  3872. hLeft = NULL;
  3873. selectionChanged(true);
  3874. }
  3875. void CMarketplaceWindow::sliderMoved( int to )
  3876. {
  3877. }
  3878. void CMarketplaceWindow::selectionChanged(bool side)
  3879. {
  3880. if(hLeft && hRight && hLeft->id!= hRight->id)
  3881. {
  3882. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  3883. slider->amount = LOCPLINT->cb->getResourceAmount(hLeft->id) / r1;
  3884. slider->moveTo(0);
  3885. max->block(false);
  3886. deal->block(false);
  3887. }
  3888. else
  3889. {
  3890. max->block(true);
  3891. deal->block(true);
  3892. slider->amount = 0;
  3893. slider->moveTo(0);
  3894. }
  3895. if(side && hLeft) //left selection changed, recalculate offers
  3896. {
  3897. rSubs.clear();
  3898. rSubs.resize(right.size());
  3899. int h1, h2;
  3900. for(int i=0;i<right.size();i++)
  3901. {
  3902. std::ostringstream oss;
  3903. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  3904. oss << h2;
  3905. if(h1!=1)
  3906. oss << "/" << h1;
  3907. rSubs[i] = oss.str();
  3908. }
  3909. }
  3910. }
  3911. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  3912. {
  3913. this->pos = pos;
  3914. background = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  3915. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  3916. //printing texts
  3917. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 240, 32, GEOR16, tytulowy, background); //window title
  3918. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 65, GEOR16, tytulowy, background); //hero speed
  3919. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 131, GEOR16, tytulowy, background); //enemy speed
  3920. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 197, GEOR16, tytulowy, background); //map scroll speed
  3921. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 263, GEOR16, tytulowy, background); //video quality
  3922. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 348, GEOR16, tytulowy, background); //music volume
  3923. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 414, GEOR16, tytulowy, background); //effects volume
  3924. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 283, 57, GEOR16, zwykly, background); //show move path
  3925. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 283, 89, GEOR16, zwykly, background); //show hero reminder
  3926. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 283, 121, GEOR16, zwykly, background); //quick combat
  3927. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 283, 153, GEOR16, zwykly, background); //video subtitles
  3928. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 283, 185, GEOR16, zwykly, background); //town building outlines
  3929. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 283, 217, GEOR16, zwykly, background); //spell book animation
  3930. //setting up buttons
  3931. save = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 516, 354, "SOSAVE.DEF", SDLK_s);
  3932. std::swap(save->imgs[0][0], save->imgs[0][1]);
  3933. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 405, 471, "soquit.def", SDLK_q);
  3934. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  3935. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 516, 471, "soretrn.def", SDLK_RETURN);
  3936. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3937. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  3938. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3939. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].first),CGI->generaltexth->zelp[349].second, "sysopb1.def", 187, 134, 1);
  3940. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].first),CGI->generaltexth->zelp[350].second, "sysopb2.def", 235, 134, 2);
  3941. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].first),CGI->generaltexth->zelp[351].second, "sysopb3.def", 283, 134, 4);
  3942. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].first),CGI->generaltexth->zelp[352].second, "sysopb4.def", 331, 134, 8);
  3943. heroMoveSpeed->select(owner->heroMoveSpeed, 1);
  3944. heroMoveSpeed->onChange = boost::bind(&CPlayerInterface::setHeroMoveSpeed, owner, _1);
  3945. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3946. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].first),CGI->generaltexth->zelp[357].second, "sysopb9.def", 187, 267, 1);
  3947. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].first),CGI->generaltexth->zelp[358].second, "sysob10.def", 251, 267, 2);
  3948. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].first),CGI->generaltexth->zelp[359].second, "sysob11.def", 315, 267, 4);
  3949. mapScrollSpeed->select(owner->mapScrollingSpeed, 1);
  3950. mapScrollSpeed->onChange = boost::bind(&CPlayerInterface::setMapScrollingSpeed, owner, _1);
  3951. }
  3952. CSystemOptionsWindow::~CSystemOptionsWindow()
  3953. {
  3954. SDL_FreeSurface(background);
  3955. delete save;
  3956. delete quitGame;
  3957. delete backToMap;
  3958. delete heroMoveSpeed;
  3959. delete mapScrollSpeed;
  3960. }
  3961. void CSystemOptionsWindow::bquitf()
  3962. {
  3963. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(exit, 0), boost::bind(&CSystemOptionsWindow::activate, this), false);
  3964. }
  3965. void CSystemOptionsWindow::breturnf()
  3966. {
  3967. LOCPLINT->popIntTotally(this);
  3968. }
  3969. void CSystemOptionsWindow::bsavef()
  3970. {
  3971. using namespace boost::posix_time;
  3972. std::ostringstream fnameStream;
  3973. fnameStream << second_clock::local_time();
  3974. std::string fname = fnameStream.str();
  3975. boost::algorithm::replace_all(fname,":","");
  3976. boost::algorithm::replace_all(fname," ","-");
  3977. 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);
  3978. }
  3979. void CSystemOptionsWindow::activate()
  3980. {
  3981. save->activate();
  3982. quitGame->activate();
  3983. backToMap->activate();
  3984. heroMoveSpeed->activate();
  3985. mapScrollSpeed->activate();
  3986. }
  3987. void CSystemOptionsWindow::deactivate()
  3988. {
  3989. save->deactivate();
  3990. quitGame->deactivate();
  3991. backToMap->deactivate();
  3992. heroMoveSpeed->deactivate();
  3993. mapScrollSpeed->deactivate();
  3994. }
  3995. void CSystemOptionsWindow::show(SDL_Surface *to)
  3996. {
  3997. SDL_BlitSurface(background, NULL, to, &pos);
  3998. save->show(to);
  3999. quitGame->show(to);
  4000. backToMap->show(to);
  4001. heroMoveSpeed->show(to);
  4002. mapScrollSpeed->show(to);
  4003. }
  4004. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  4005. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  4006. {
  4007. if(H1)
  4008. selected = 0;
  4009. else
  4010. selected = -1;
  4011. bg = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  4012. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  4013. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  4014. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,GEOR16,tytulowy,bg);
  4015. printAtMiddle("2500",320,328,GEOR13,zwykly,bg);
  4016. printAtMiddle(CGI->generaltexth->jktexts[38],146,283,GEOR16,tytulowy,bg);
  4017. printAtMiddleWB(gossip,200,220,GEOR13,50,zwykly,bg);
  4018. pos.w = bg->w;
  4019. pos.h = bg->h;
  4020. pos.x = (screen->w-bg->w)/2;
  4021. pos.y = (screen->h-bg->h)/2;
  4022. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  4023. h1.pos.x += pos.x;
  4024. h2.pos.x += pos.x;
  4025. h1.pos.y += pos.y;
  4026. h2.pos.y += pos.y;
  4027. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"",boost::bind(&CTavernWindow::close,this),pos.x+310,pos.y+428,"ICANCEL.DEF",SDLK_ESCAPE);
  4028. recruit = new AdventureMapButton("","",boost::bind(&CTavernWindow::recruitb,this),pos.x+272,pos.y+355,"TPTAV01.DEF",SDLK_RETURN);
  4029. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"",0,pos.x+22,pos.y+428,"TPTAV02.DEF",SDLK_t);
  4030. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  4031. {
  4032. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  4033. recruit->block(2);
  4034. }
  4035. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  4036. {
  4037. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  4038. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  4039. recruit->block(2);
  4040. }
  4041. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  4042. {
  4043. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  4044. recruit->block(2);
  4045. }
  4046. else
  4047. {
  4048. if(H1)
  4049. {
  4050. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  4051. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",H1->name);
  4052. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",H1->type->heroClass->name);
  4053. }
  4054. else
  4055. recruit->block(1);
  4056. }
  4057. }
  4058. void CTavernWindow::recruitb()
  4059. {
  4060. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  4061. close();
  4062. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  4063. }
  4064. CTavernWindow::~CTavernWindow()
  4065. {
  4066. SDL_FreeSurface(bg);
  4067. delete cancel;
  4068. delete thiefGuild;
  4069. delete recruit;
  4070. delete bar;
  4071. }
  4072. void CTavernWindow::activate()
  4073. {
  4074. thiefGuild->activate();
  4075. cancel->activate();
  4076. if(h1.h)
  4077. h1.activate();
  4078. if(h2.h)
  4079. h2.activate();
  4080. recruit->activate();
  4081. LOCPLINT->statusbar = bar;
  4082. }
  4083. void CTavernWindow::deactivate()
  4084. {
  4085. thiefGuild->deactivate();
  4086. cancel->deactivate();
  4087. if(h1.h)
  4088. h1.deactivate();
  4089. if(h2.h)
  4090. h2.deactivate();
  4091. recruit->deactivate();
  4092. }
  4093. void CTavernWindow::close()
  4094. {
  4095. LOCPLINT->popIntTotally(this);
  4096. }
  4097. void CTavernWindow::show(SDL_Surface * to)
  4098. {
  4099. blitAt(bg,pos.x,pos.y,to);
  4100. if(h1.h)
  4101. h1.show(to);
  4102. if(h2.h)
  4103. h2.show(to);
  4104. thiefGuild->show(to);
  4105. cancel->show(to);
  4106. recruit->show(to);
  4107. bar->show(to);
  4108. if(selected >= 0)
  4109. {
  4110. HeroPortrait *sel = selected ? &h2 : &h1;
  4111. char descr[300];
  4112. int artifs = sel->h->artifWorn.size()+sel->h->artifacts.size();
  4113. for(int i=13; i<=17; i++) //war machines and spellbook doesn't count
  4114. if(vstd::contains(sel->h->artifWorn,i))
  4115. artifs--;
  4116. sprintf_s(descr,300,CGI->generaltexth->allTexts[215].c_str(),
  4117. sel->h->name.c_str(),sel->h->level,sel->h->type->heroClass->name.c_str(),artifs);
  4118. printAtMiddleWB(descr,pos.x+146,pos.y+389,GEOR13,40,zwykly,to);
  4119. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  4120. }
  4121. }
  4122. void CTavernWindow::HeroPortrait::clickLeft(boost::logic::tribool down)
  4123. {
  4124. if(pressedL && !down)
  4125. as();
  4126. ClickableL::clickLeft(down);
  4127. }
  4128. void CTavernWindow::HeroPortrait::activate()
  4129. {
  4130. ClickableL::activate();
  4131. ClickableR::activate();
  4132. Hoverable::activate();
  4133. }
  4134. void CTavernWindow::HeroPortrait::deactivate()
  4135. {
  4136. ClickableL::deactivate();
  4137. ClickableR::deactivate();
  4138. Hoverable::deactivate();
  4139. }
  4140. void CTavernWindow::HeroPortrait::clickRight(boost::logic::tribool down)
  4141. {
  4142. if(down)
  4143. {
  4144. LOCPLINT->adventureInt->heroWindow->setHero(h);
  4145. LOCPLINT->pushInt(new CRClickPopupInt(LOCPLINT->adventureInt->heroWindow,false));
  4146. }
  4147. }
  4148. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  4149. :as(sel,id)
  4150. {
  4151. h = H;
  4152. pos.x = x;
  4153. pos.y = y;
  4154. pos.w = 58;
  4155. pos.h = 64;
  4156. if(H)
  4157. {
  4158. hoverName = CGI->generaltexth->tavernInfo[4];
  4159. boost::algorithm::replace_first(hoverName,"%s",H->name);
  4160. }
  4161. }
  4162. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  4163. {
  4164. blitAt(graphics->portraitLarge[h->subID],pos,to);
  4165. }
  4166. void CTavernWindow::HeroPortrait::hover( bool on )
  4167. {
  4168. Hoverable::hover(on);
  4169. if(on)
  4170. LOCPLINT->statusbar->print(hoverName);
  4171. else
  4172. LOCPLINT->statusbar->clear();
  4173. }
  4174. void CInGameConsole::activate()
  4175. {
  4176. KeyInterested::activate();
  4177. }
  4178. void CInGameConsole::deactivate()
  4179. {
  4180. KeyInterested::deactivate();
  4181. }
  4182. void CInGameConsole::show(SDL_Surface * to)
  4183. {
  4184. int number = 0;
  4185. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  4186. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  4187. {
  4188. SDL_Color green = {0,0xff,0,0};
  4189. Point leftBottomCorner(0, screen->h);
  4190. if(LOCPLINT->battleInt)
  4191. {
  4192. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  4193. }
  4194. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, TNRB16, green);
  4195. if(SDL_GetTicks() - it->second > defaultTimeout)
  4196. {
  4197. toDel.push_back(it);
  4198. }
  4199. }
  4200. for(int it=0; it<toDel.size(); ++it)
  4201. {
  4202. texts.erase(toDel[it]);
  4203. }
  4204. }
  4205. void CInGameConsole::print(const std::string &txt)
  4206. {
  4207. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  4208. if(texts.size() > maxDisplayedTexts)
  4209. {
  4210. texts.pop_front();
  4211. }
  4212. }
  4213. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  4214. {
  4215. if(key.type != SDL_KEYDOWN) return;
  4216. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  4217. switch(key.keysym.sym)
  4218. {
  4219. case SDLK_TAB:
  4220. {
  4221. if(captureAllKeys)
  4222. {
  4223. captureAllKeys = false;
  4224. endEnteringText(false);
  4225. }
  4226. else
  4227. {
  4228. captureAllKeys = true;
  4229. startEnteringText();
  4230. }
  4231. break;
  4232. }
  4233. case SDLK_RETURN: //enter key
  4234. {
  4235. if(enteredText.size() > 0 && captureAllKeys)
  4236. {
  4237. captureAllKeys = false;
  4238. endEnteringText(true);
  4239. }
  4240. break;
  4241. }
  4242. case SDLK_BACKSPACE:
  4243. {
  4244. if(enteredText.size() > 1)
  4245. {
  4246. enteredText.resize(enteredText.size()-1);
  4247. enteredText[enteredText.size()-1] = '_';
  4248. refreshEnteredText();
  4249. }
  4250. break;
  4251. }
  4252. case SDLK_UP: //up arrow
  4253. {
  4254. if(prevEntDisp == -1)
  4255. {
  4256. prevEntDisp = previouslyEntered.size() - 1;
  4257. enteredText = previouslyEntered[prevEntDisp] + "_";
  4258. refreshEnteredText();
  4259. }
  4260. else if( prevEntDisp > 0)
  4261. {
  4262. --prevEntDisp;
  4263. enteredText = previouslyEntered[prevEntDisp] + "_";
  4264. refreshEnteredText();
  4265. }
  4266. break;
  4267. }
  4268. case SDLK_DOWN: //down arrow
  4269. {
  4270. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  4271. {
  4272. ++prevEntDisp;
  4273. enteredText = previouslyEntered[prevEntDisp] + "_";
  4274. refreshEnteredText();
  4275. }
  4276. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  4277. {
  4278. prevEntDisp = -1;
  4279. enteredText = "_";
  4280. refreshEnteredText();
  4281. }
  4282. break;
  4283. }
  4284. default:
  4285. {
  4286. if(enteredText.size() > 0)
  4287. {
  4288. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  4289. {
  4290. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  4291. enteredText += "_";
  4292. refreshEnteredText();
  4293. }
  4294. }
  4295. break;
  4296. }
  4297. }
  4298. }
  4299. void CInGameConsole::startEnteringText()
  4300. {
  4301. enteredText = "_";
  4302. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  4303. {
  4304. LOCPLINT->statusbar->print(enteredText);
  4305. }
  4306. else if(LOCPLINT->battleInt)
  4307. {
  4308. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  4309. }
  4310. }
  4311. void CInGameConsole::endEnteringText(bool printEnteredText)
  4312. {
  4313. prevEntDisp = -1;
  4314. if(printEnteredText)
  4315. {
  4316. std::string txt = enteredText.substr(0, enteredText.size()-1);
  4317. LOCPLINT->cb->sendMessage(txt);
  4318. previouslyEntered.push_back(txt);
  4319. print(txt);
  4320. }
  4321. enteredText = "";
  4322. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  4323. {
  4324. LOCPLINT->statusbar->clear();
  4325. }
  4326. else if(LOCPLINT->battleInt)
  4327. {
  4328. LOCPLINT->battleInt->console->ingcAlter = "";
  4329. }
  4330. }
  4331. void CInGameConsole::refreshEnteredText()
  4332. {
  4333. if(LOCPLINT->topInt() == LOCPLINT->adventureInt)
  4334. {
  4335. LOCPLINT->statusbar->print(enteredText);
  4336. }
  4337. else if(LOCPLINT->battleInt)
  4338. {
  4339. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  4340. }
  4341. }
  4342. CInGameConsole::CInGameConsole() : defaultTimeout(10000), maxDisplayedTexts(10), prevEntDisp(-1)
  4343. {
  4344. }
  4345. void CGarrisonWindow::close()
  4346. {
  4347. LOCPLINT->popIntTotally(this);
  4348. }
  4349. void CGarrisonWindow::activate()
  4350. {
  4351. split->activate();
  4352. quit->activate();
  4353. garr->activate();
  4354. }
  4355. void CGarrisonWindow::deactivate()
  4356. {
  4357. split->deactivate();
  4358. quit->deactivate();
  4359. garr->deactivate();
  4360. }
  4361. void CGarrisonWindow::show(SDL_Surface * to)
  4362. {
  4363. blitAt(bg,pos,to);
  4364. split->show(to);
  4365. quit->show(to);
  4366. garr->show(to);
  4367. }
  4368. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down )
  4369. {
  4370. bg = BitmapHandler::loadBitmap("GARRISON.bmp");
  4371. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  4372. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  4373. pos.x = screen->w/2 - bg->w/2;
  4374. pos.y = screen->h/2 - bg->h/2;
  4375. pos.w = screen->w;
  4376. pos.h = screen->h;
  4377. garr = new CGarrisonInt(pos.x+92, pos.y+129, 4, 30, bg, 92, 129, up, down);
  4378. split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+88,pos.y+314,"IDV6432.DEF");
  4379. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),pos.x+399,pos.y+314,"IOK6432.DEF",SDLK_RETURN);
  4380. }
  4381. CGarrisonWindow::~CGarrisonWindow()
  4382. {
  4383. SDL_FreeSurface(bg);
  4384. delete split;
  4385. delete quit;
  4386. delete garr;
  4387. }
  4388. IShowActivable::IShowActivable()
  4389. {
  4390. type = 0;
  4391. }
  4392. CWindowWithGarrison::CWindowWithGarrison()
  4393. {
  4394. type |= WITH_GARRISON;
  4395. }
  4396. void CRClickPopupInt::show(SDL_Surface * to)
  4397. {
  4398. inner->show(to);
  4399. }
  4400. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  4401. {
  4402. inner = our;
  4403. delInner = deleteInt;
  4404. }
  4405. CRClickPopupInt::~CRClickPopupInt()
  4406. {
  4407. if(delInner)
  4408. delete inner;
  4409. }