CPlayerInterface.cpp 118 KB

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