CPlayerInterface.cpp 140 KB

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