CPlayerInterface.cpp 112 KB

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