CPlayerInterface.cpp 130 KB

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