CPlayerInterface.cpp 129 KB

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