CPlayerInterface.cpp 140 KB

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