CPlayerInterface.cpp 144 KB

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