CPlayerInterface.cpp 137 KB

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