CPlayerInterface.cpp 103 KB

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