CPlayerInterface.cpp 143 KB

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