2
0

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