CPlayerInterface.cpp 138 KB

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