CPlayerInterface.cpp 102 KB

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