CPlayerInterface.cpp 104 KB

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