CPlayerInterface.cpp 128 KB

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