CPlayerInterface.cpp 134 KB

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