CPlayerInterface.cpp 115 KB

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