CPlayerInterface.cpp 102 KB

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