CPlayerInterface.cpp 97 KB

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