CPlayerInterface.cpp 118 KB

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