CPlayerInterface.cpp 142 KB

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