CPlayerInterface.cpp 118 KB

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