CPlayerInterface.cpp 128 KB

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