GUIClasses.cpp 180 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "UIFramework/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "BattleInterface/CBattleInterface.h"
  6. #include "BattleInterface/CBattleInterfaceClasses.h"
  7. #include "../CCallback.h"
  8. #include "CCastleInterface.h"
  9. #include "CCreatureWindow.h"
  10. #include "UIFramework/CCursorHandler.h"
  11. #include "CGameInfo.h"
  12. #include "CHeroWindow.h"
  13. #include "CMessage.h"
  14. #include "CConfigHandler.h"
  15. #include "CConfigHandler.h"
  16. #include "BattleInterface/CCreatureAnimation.h"
  17. #include "CPlayerInterface.h"
  18. #include "Graphics.h"
  19. #include "CAnimation.h"
  20. #include "../lib/CArtHandler.h"
  21. #include "../lib/CBuildingHandler.h"
  22. #include "../lib/CGeneralTextHandler.h"
  23. #include "../lib/CHeroHandler.h"
  24. #include "../lib/CLodHandler.h"
  25. #include "../lib/CObjectHandler.h"
  26. #include "../lib/CSpellHandler.h"
  27. #include "../lib/CTownHandler.h"
  28. #include "../lib/CondSh.h"
  29. #include "../lib/map.h"
  30. #include "mapHandler.h"
  31. #include "../lib/CStopWatch.h"
  32. #include "../lib/NetPacks.h"
  33. #include "CSpellWindow.h"
  34. #include "CHeroWindow.h"
  35. #include "CVideoHandler.h"
  36. #include "../lib/StartInfo.h"
  37. #include "CPreGame.h"
  38. #include "../lib/HeroBonus.h"
  39. #include "../lib/CCreatureHandler.h"
  40. #include "CMusicHandler.h"
  41. #include "../lib/BattleState.h"
  42. #include "../lib/CGameState.h"
  43. #include "../lib/GameConstants.h"
  44. #include "UIFramework/CGuiHandler.h"
  45. /*
  46. * GUIClasses.cpp, part of VCMI engine
  47. *
  48. * Authors: listed in file AUTHORS in main folder
  49. *
  50. * License: GNU General Public License v2.0 or later
  51. * Full text of license available in license.txt file, in main folder
  52. *
  53. */
  54. using namespace boost::assign;
  55. using namespace CSDL_Ext;
  56. extern std::queue<SDL_Event*> events;
  57. extern boost::mutex eventsM;
  58. std::list<CFocusable*> CFocusable::focusables;
  59. CFocusable * CFocusable::inputWithFocus;
  60. #undef min
  61. #undef max
  62. void CGarrisonSlot::hover (bool on)
  63. {
  64. ////Hoverable::hover(on);
  65. if(on)
  66. {
  67. std::string temp;
  68. if(creature)
  69. {
  70. if(owner->highlighted)
  71. {
  72. if(owner->highlighted == this)
  73. {
  74. temp = CGI->generaltexth->tcommands[4]; //View %s
  75. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  76. }
  77. else if (owner->highlighted->creature == creature)
  78. {
  79. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  80. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  81. }
  82. else if (owner->highlighted->creature)
  83. {
  84. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  85. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  86. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  87. }
  88. else
  89. {
  90. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  91. tlog2 << "Highlighted set to NULL"<<std::endl;
  92. owner->highlighted = NULL;
  93. }
  94. }
  95. else
  96. {
  97. if(upg)
  98. {
  99. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  100. }
  101. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == GameConstants::TOWNI_TYPE)
  102. {
  103. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  104. }
  105. else
  106. {
  107. temp = CGI->generaltexth->allTexts[481]; //Select %s
  108. }
  109. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  110. };
  111. }
  112. else
  113. {
  114. if(owner->highlighted)
  115. {
  116. const CArmedInstance *highl = owner->highlighted->getObj();
  117. if( highl->needsLastStack() //we are moving stack from hero's
  118. && highl->stacksCount() == 1 //it's only stack
  119. && owner->highlighted->upg != upg //we're moving it to the other garrison
  120. )
  121. {
  122. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  123. }
  124. else
  125. {
  126. temp = CGI->generaltexth->tcommands[6]; //Move %s
  127. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  128. }
  129. }
  130. else
  131. {
  132. temp = CGI->generaltexth->tcommands[11]; //Empty
  133. }
  134. }
  135. GH.statusbar->print(temp);
  136. }
  137. else
  138. {
  139. GH.statusbar->clear();
  140. }
  141. }
  142. const CArmedInstance * CGarrisonSlot::getObj()
  143. {
  144. return (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  145. }
  146. bool CGarrisonSlot::our()
  147. {
  148. return upg?(owner->owned[1]):(owner->owned[0]);
  149. }
  150. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  151. {
  152. if(down && creature)
  153. {
  154. GH.pushInt(createCreWindow(myStack, 2));
  155. }
  156. }
  157. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  158. {
  159. if(down)
  160. {
  161. bool refr = false;
  162. if(owner->highlighted)
  163. {
  164. if(owner->highlighted == this) //view info
  165. {
  166. UpgradeInfo pom;
  167. LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
  168. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  169. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  170. boost::function<void()> upgr = NULL;
  171. boost::function<void()> dism = NULL;
  172. if (canUpgrade) upgr = boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb, getObj(), ID, pom.newID[0]);
  173. if (canDismiss) dism = boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID);
  174. owner->highlighted = NULL;
  175. owner->splitting = false;
  176. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  177. owner->splitButtons[i]->block(true);
  178. redraw();
  179. refr = true;
  180. CIntObject *creWindow = createCreWindow(myStack, 3, upgr, dism, &pom);
  181. GH.pushInt(creWindow);
  182. }
  183. else
  184. {
  185. // Only allow certain moves if troops aren't removable or not ours.
  186. if ( ( owner->highlighted->our()//our creature is selected
  187. || owner->highlighted->creature == creature )//or we are rebalancing army
  188. && ( owner->removableUnits
  189. || (upg == 0 && ( owner->highlighted->upg == 1 && !creature ) )
  190. || (upg == 1 && owner->highlighted->upg == 1 ) ) )
  191. {
  192. //we want to split
  193. if((owner->splitting || LOCPLINT->shiftPressed())
  194. && (!creature
  195. || (creature == owner->highlighted->creature)))
  196. {
  197. owner->p2 = ID; //store the second stack pos
  198. owner->pb = upg;//store the second stack owner (up or down army)
  199. owner->splitting = false;
  200. int totalAmount = owner->highlighted->count;
  201. if(creature)
  202. totalAmount += count;
  203. int last = -1;
  204. if(upg != owner->highlighted->upg) //not splitting within same army
  205. {
  206. if(owner->highlighted->getObj()->stacksCount() == 1 //we're splitting away the last stack
  207. && owner->highlighted->getObj()->needsLastStack() )
  208. {
  209. last = 0;
  210. }
  211. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  212. && owner->highlighted->creature == creature
  213. && getObj()->needsLastStack() )
  214. {
  215. last += 2;
  216. }
  217. }
  218. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  219. GH.pushInt(spw);
  220. refr = true;
  221. }
  222. else if(creature != owner->highlighted->creature) //swap
  223. {
  224. LOCPLINT->cb->swapCreatures(
  225. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  226. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  227. ID,owner->highlighted->ID);
  228. }
  229. else //merge
  230. {
  231. LOCPLINT->cb->mergeStacks(
  232. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  233. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  234. owner->highlighted->ID,ID);
  235. }
  236. }
  237. else // Highlight
  238. {
  239. if(creature)
  240. owner->highlighted = this;
  241. redraw();
  242. refr = true;
  243. }
  244. }
  245. }
  246. else //highlight
  247. {
  248. if(creature)
  249. {
  250. owner->highlighted = this;
  251. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  252. owner->splitButtons[i]->block(false);
  253. }
  254. redraw();
  255. refr = true;
  256. }
  257. if(refr) {hover(false); hover(true); } //to refresh statusbar
  258. }
  259. }
  260. void CGarrisonSlot::activate()
  261. {
  262. if(!active) active=true;
  263. else return;
  264. activateLClick();
  265. activateRClick();
  266. activateHover();
  267. }
  268. void CGarrisonSlot::deactivate()
  269. {
  270. if(active) active=false;
  271. else return;
  272. deactivateLClick();
  273. deactivateRClick();
  274. deactivateHover();
  275. }
  276. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CStackInstance * Creature)
  277. {
  278. //assert(Creature == CGI->creh->creatures[Creature->idNumber]);
  279. active = false;
  280. upg = Upg;
  281. ID = IID;
  282. myStack = Creature;
  283. creature = Creature ? Creature->type : NULL;
  284. count = Creature ? Creature->count : 0;
  285. pos.x += x;
  286. pos.y += y;
  287. if(Owner->smallIcons)
  288. {
  289. pos.w = 32;
  290. pos.h = 32;
  291. }
  292. else
  293. {
  294. pos.w = 58;
  295. pos.h = 64;
  296. }
  297. owner = Owner;
  298. }
  299. CGarrisonSlot::~CGarrisonSlot()
  300. {
  301. if(active)
  302. deactivate();
  303. }
  304. void CGarrisonSlot::showAll(SDL_Surface * to)
  305. {
  306. std::map<int,SDL_Surface*> &imgs = (owner->smallIcons ? graphics->smallImgs : graphics->bigImgs);
  307. if(creature)
  308. {
  309. char buf[15];
  310. SDL_itoa(count,buf,10);
  311. blitAt(imgs[creature->idNumber],pos,to);
  312. printTo(buf, pos.x+pos.w, pos.y+pos.h+1, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, Colors::Cornsilk, to);
  313. if((owner->highlighted==this)
  314. || (owner->splitting && owner->highlighted->creature == creature))
  315. {
  316. blitAt(imgs[-1],pos,to);
  317. }
  318. }
  319. else//empty slot
  320. {
  321. if(owner->splitting && owner->highlighted->our())
  322. blitAt(imgs[-1],pos,to);
  323. }
  324. }
  325. CGarrisonInt::~CGarrisonInt()
  326. {/*
  327. for(size_t i = 0; i<splitButtons.size(); i++)
  328. delete splitButtons[i];*/
  329. }
  330. void CGarrisonInt::addSplitBtn(CAdventureMapButton * button)
  331. {
  332. addChild(button);
  333. button->recActions = defActions;
  334. splitButtons.push_back(button);
  335. }
  336. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  337. {
  338. ret.resize(7);
  339. for(TSlots::const_iterator i=set->Slots().begin(); i!=set->Slots().end(); i++)
  340. {
  341. ret[i->first] = new CGarrisonSlot(this, posX + (i->first*distance), posY, i->first, Upg, i->second);
  342. }
  343. for(int i=0; i<ret.size(); i++)
  344. if(!ret[i])
  345. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY,i,Upg,NULL);
  346. if (twoRows)
  347. for (int i=4; i<ret.size(); i++)
  348. {
  349. ret[i]->pos.x -= 126;
  350. ret[i]->pos.y += 37;
  351. };
  352. }
  353. void CGarrisonInt::createSlots()
  354. {
  355. OBJ_CONSTRUCTION_CAPTURING_ALL;
  356. int width = smallIcons? 32 : 58;
  357. if(armedObjs[0])
  358. createSet(slotsUp, armedObjs[0], 0, 0, width+interx, 0);
  359. if(armedObjs[1])
  360. createSet (slotsDown, armedObjs[1], garOffset.x, garOffset.y, width+interx, 1);
  361. }
  362. void CGarrisonInt::deleteSlots()
  363. {
  364. for (int i=0; i<slotsUp.size(); i++)
  365. delChildNUll(slotsUp[i]);
  366. for (int i=0; i<slotsDown.size(); i++)
  367. delChildNUll(slotsDown[i]);
  368. }
  369. void CGarrisonInt::recreateSlots()
  370. {
  371. splitting = false;
  372. highlighted = NULL;
  373. for(size_t i = 0; i<splitButtons.size(); i++)
  374. splitButtons[i]->block(true);
  375. bool wasActive = active;
  376. if(active)
  377. {
  378. deactivate();
  379. }
  380. deleteSlots();
  381. createSlots();
  382. if(wasActive)
  383. {
  384. activate();
  385. showAll(screen2);
  386. }
  387. }
  388. void CGarrisonInt::splitClick()
  389. {
  390. if(!highlighted)
  391. return;
  392. splitting = !splitting;
  393. redraw();
  394. }
  395. void CGarrisonInt::splitStacks(int am2)
  396. {
  397. LOCPLINT->cb->splitStack(armedObjs[highlighted->upg], armedObjs[pb], highlighted->ID, p2, am2);
  398. }
  399. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  400. SDL_Surface *pomsur, const Point& SurOffset,
  401. const CArmedInstance *s1, const CArmedInstance *s2,
  402. bool _removableUnits, bool smallImgs, bool _twoRows )
  403. : interx(inx), garOffset(garsOffset), highlighted(NULL), splitting(false),
  404. smallIcons(smallImgs), removableUnits (_removableUnits), twoRows(_twoRows)
  405. {
  406. setArmy(s1, false);
  407. setArmy(s2, true);
  408. pos.x += x;
  409. pos.y += y;
  410. createSlots();
  411. }
  412. void CGarrisonInt::activate()
  413. {
  414. for(size_t i = 0; i<splitButtons.size(); i++)
  415. if( (splitButtons[i]->isBlocked()) != !highlighted)
  416. splitButtons[i]->block(!highlighted);
  417. CIntObject::activate();
  418. }
  419. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  420. {
  421. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == 254) : false; //254 - neutral objects (pandora, banks)
  422. armedObjs[bottomGarrison] = army;
  423. }
  424. CInfoWindow::CInfoWindow(std::string Text, int player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  425. {
  426. OBJ_CONSTRUCTION_CAPTURING_ALL;
  427. type |= BLOCK_ADV_HOTKEYS;
  428. ID = -1;
  429. for(int i=0;i<Buttons.size();i++)
  430. {
  431. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first);
  432. button->borderColor = Colors::MetallicGold;
  433. button->borderEnabled = true;
  434. button->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  435. buttons.push_back(button);
  436. }
  437. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::Cornsilk);
  438. if(!text->slider)
  439. {
  440. text->pos.w = text->maxW;
  441. text->pos.h = text->maxH;
  442. }
  443. if(buttons.size())
  444. {
  445. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  446. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  447. }
  448. for(int i=0;i<comps.size();i++)
  449. {
  450. comps[i]->recActions = 0xff;
  451. addChild(comps[i]);
  452. comps[i]->recActions &= ~(SHOWALL | UPDATE);
  453. components.push_back(comps[i]);
  454. }
  455. setDelComps(delComps);
  456. CMessage::drawIWindow(this,Text,player);
  457. }
  458. CInfoWindow::CInfoWindow()
  459. {
  460. ID = -1;
  461. setDelComps(false);
  462. text = NULL;
  463. }
  464. void CInfoWindow::close()
  465. {
  466. GH.popIntTotally(this);
  467. if(LOCPLINT)
  468. LOCPLINT->showingDialog->setn(false);
  469. }
  470. void CInfoWindow::show(SDL_Surface * to)
  471. {
  472. CIntObject::show(to);
  473. }
  474. CInfoWindow::~CInfoWindow()
  475. {
  476. if(!delComps)
  477. {
  478. for (int i=0;i<components.size();i++)
  479. removeChild(components[i]);
  480. }
  481. }
  482. void CInfoWindow::showAll(SDL_Surface * to)
  483. {
  484. CSimpleWindow::show(to);
  485. CIntObject::showAll(to);
  486. }
  487. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, int player)
  488. {
  489. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  490. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  491. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  492. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  493. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  494. for(int i=0;i<onYes.funcs.size();i++)
  495. temp->buttons[0]->callback += onYes.funcs[i];
  496. for(int i=0;i<onNo.funcs.size();i++)
  497. temp->buttons[1]->callback += onNo.funcs[i];
  498. GH.pushInt(temp);
  499. }
  500. CInfoWindow * CInfoWindow::create(const std::string &text, int playerID /*= 1*/, const std::vector<CComponent*> *components /*= NULL*/, bool DelComps)
  501. {
  502. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  503. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  504. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  505. return ret;
  506. }
  507. void CInfoWindow::setDelComps(bool DelComps)
  508. {
  509. delComps = DelComps;
  510. BOOST_FOREACH(CComponent *comp, components)
  511. {
  512. if(delComps)
  513. comp->recActions |= DISPOSE;
  514. else
  515. comp->recActions &= ~DISPOSE;
  516. }
  517. }
  518. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  519. :free(Free),bitmap(Bitmap)
  520. {
  521. init(x, y);
  522. }
  523. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  524. : free(Free),bitmap(Bitmap)
  525. {
  526. switch(alignment)
  527. {
  528. case BOTTOMRIGHT:
  529. init(p.x - Bitmap->w, p.y - Bitmap->h);
  530. break;
  531. case CENTER:
  532. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  533. break;
  534. case TOPLEFT:
  535. init(p.x, p.y);
  536. break;
  537. default:
  538. assert(0); //not implemented
  539. }
  540. }
  541. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  542. {
  543. CCS->curh->hide();
  544. free=Free;
  545. bitmap=Bitmap;
  546. if(bitmap)
  547. {
  548. pos.x = screen->w/2 - bitmap->w/2;
  549. pos.y = screen->h/2 - bitmap->h/2;
  550. pos.h = bitmap->h;
  551. pos.w = bitmap->w;
  552. }
  553. }
  554. void CInfoPopup::close()
  555. {
  556. if(free)
  557. SDL_FreeSurface(bitmap);
  558. GH.popIntTotally(this);
  559. }
  560. void CInfoPopup::show(SDL_Surface * to)
  561. {
  562. blitAt(bitmap,pos.x,pos.y,to);
  563. }
  564. CInfoPopup::~CInfoPopup()
  565. {
  566. CCS->curh->show();
  567. }
  568. void CInfoPopup::init(int x, int y)
  569. {
  570. CCS->curh->hide();
  571. pos.x = x;
  572. pos.y = y;
  573. pos.h = bitmap->h;
  574. pos.w = bitmap->w;
  575. // Put the window back on screen if necessary
  576. vstd::amax(pos.x, 0);
  577. vstd::amax(pos.y, 0);
  578. vstd::amin(pos.x, screen->w - bitmap->w);
  579. vstd::amin(pos.y, screen->h - bitmap->h);
  580. }
  581. void CComponent::init(Etype Type, int Subtype, int Val)
  582. {
  583. std::ostringstream oss;
  584. switch (Type)
  585. {
  586. case artifact:
  587. description = CGI->arth->artifacts[Subtype]->Description();
  588. subtitle = CGI->arth->artifacts[Subtype]->Name();
  589. break;
  590. case primskill:
  591. oss << std::showpos << Val << " ";
  592. if(Subtype < 4)
  593. {
  594. description = CGI->generaltexth->arraytxt[2+Subtype];
  595. oss << CGI->generaltexth->primarySkillNames[Subtype];
  596. }
  597. else if(Subtype == 5) //spell points
  598. {
  599. description = CGI->generaltexth->allTexts[149];
  600. oss << CGI->generaltexth->allTexts[387];
  601. }
  602. else
  603. {
  604. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  605. }
  606. subtitle = oss.str();
  607. break;
  608. case building:
  609. description = CGI->buildh->buildings[Subtype][Val]->Description();
  610. subtitle = CGI->buildh->buildings[Subtype][Val]->Name();
  611. break;
  612. case secskill44: case secskill:
  613. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  614. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  615. break;
  616. case morale:
  617. description = CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  618. break;
  619. case luck:
  620. description = CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  621. break;
  622. case resource:
  623. description = CGI->generaltexth->allTexts[242];
  624. oss << Val;
  625. subtitle = oss.str();
  626. break;
  627. case spell:
  628. description = CGI->spellh->spells[Subtype]->descriptions[Val];
  629. subtitle = CGI->spellh->spells[Subtype]->name;
  630. break;
  631. case creature:
  632. subtitle = (Val? boost::lexical_cast<std::string>(Val) + " " : "") + CGI->creh->creatures[Subtype]->*(Val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  633. break;
  634. case experience:
  635. description = CGI->generaltexth->allTexts[241];
  636. oss << Val ;
  637. if(Subtype && Val==1)
  638. {
  639. subtitle = CGI->generaltexth->allTexts[442];
  640. }
  641. else
  642. {
  643. subtitle = oss.str();
  644. }
  645. break;
  646. case hero:
  647. subtitle = description = CGI->heroh->heroes[Subtype]->name;
  648. break;
  649. case flag:
  650. subtitle = CGI->generaltexth->capColors[Subtype];
  651. break;
  652. }
  653. img = NULL;
  654. free = false;
  655. type = Type;
  656. subtype = Subtype;
  657. val = Val;
  658. SDL_Surface * temp = this->getImg();
  659. if(!temp)
  660. {
  661. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  662. return;
  663. }
  664. pos.w = temp->w;
  665. pos.h = temp->h;
  666. }
  667. CComponent::CComponent(Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur):img(sur),free(freeSur)
  668. {
  669. init(Type,Subtype,Val);
  670. }
  671. CComponent::CComponent(const Component &c)
  672. {
  673. if(c.id==5)
  674. init(experience,c.subtype,c.val);
  675. else if(c.id == Component::SPELL)
  676. init(spell,c.subtype,c.val);
  677. else
  678. init((Etype)c.id,c.subtype,c.val);
  679. if(c.id==2 && c.when==-1)
  680. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  681. }
  682. CComponent::CComponent()
  683. {
  684. img = NULL;
  685. }
  686. CComponent::~CComponent()
  687. {
  688. if (free && img)
  689. SDL_FreeSurface(img);
  690. }
  691. SDL_Surface * CComponent::setSurface(std:: string defname, int imagepos)
  692. {
  693. if (img)
  694. tlog1<<"CComponent::setSurface: Warning - surface is already set!\n";
  695. CDefEssential * def = CDefHandler::giveDefEss(defname);
  696. free = true;
  697. img = def->ourImages[imagepos].bitmap;
  698. img->refcount++;//to preserve surface whed def is deleted
  699. delete def;
  700. return img;
  701. }
  702. void CComponent::show(SDL_Surface * to)
  703. {
  704. blitAt(getImg(),pos.x,pos.y,to);
  705. }
  706. SDL_Surface * CComponent::getImg()
  707. {
  708. if (img)
  709. return img;
  710. switch (type)
  711. {
  712. case artifact:
  713. return graphics->artDefs->ourImages[subtype].bitmap;
  714. case primskill:
  715. return graphics->pskillsb->ourImages[subtype].bitmap;
  716. case secskill44:
  717. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  718. case secskill:
  719. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  720. case resource:
  721. return graphics->resources->ourImages[subtype].bitmap;
  722. case experience:
  723. return graphics->pskillsb->ourImages[4].bitmap;
  724. case morale:
  725. return graphics->morale82->ourImages[val+3].bitmap;
  726. case luck:
  727. return graphics->luck82->ourImages[val+3].bitmap;
  728. case spell:
  729. return graphics->spellscr->ourImages[subtype].bitmap;
  730. case building:
  731. return setSurface(graphics->buildingPics[subtype],val);
  732. case creature:
  733. return graphics->bigImgs[subtype];
  734. case hero:
  735. return graphics->portraitLarge[subtype];
  736. case flag:
  737. return graphics->flags->ourImages[subtype].bitmap;
  738. }
  739. return NULL;
  740. }
  741. void CComponent::clickRight(tribool down, bool previousState)
  742. {
  743. if(description.size())
  744. adventureInt->handleRightClick(description,down);
  745. }
  746. void CComponent::activate()
  747. {
  748. activateRClick();
  749. }
  750. void CComponent::deactivate()
  751. {
  752. deactivateRClick();
  753. }
  754. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  755. {
  756. if (down)
  757. {
  758. if(onSelect)
  759. onSelect();
  760. }
  761. }
  762. void CSelectableComponent::init()
  763. {
  764. selected = false;
  765. }
  766. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect)
  767. :CComponent(c),onSelect(OnSelect)
  768. {
  769. init();
  770. }
  771. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect)
  772. :CComponent(Type,Sub,Val),onSelect(OnSelect)
  773. {
  774. init();
  775. }
  776. CSelectableComponent::~CSelectableComponent()
  777. {
  778. }
  779. void CSelectableComponent::activate()
  780. {
  781. activateKeys();
  782. CComponent::activate();
  783. activateLClick();
  784. }
  785. void CSelectableComponent::deactivate()
  786. {
  787. deactivateKeys();
  788. CComponent::deactivate();
  789. deactivateLClick();
  790. }
  791. void CSelectableComponent::select(bool on)
  792. {
  793. if(on != selected)
  794. {
  795. selected = on;
  796. return;
  797. }
  798. else
  799. {
  800. return;
  801. }
  802. }
  803. void CSelectableComponent::show(SDL_Surface * to)
  804. {
  805. blitAt(getImg(),pos.x,pos.y,to);
  806. if(selected)
  807. {
  808. CSDL_Ext::drawBorder(to, Rect::around(Rect(pos.x, pos.y, getImg()->w, getImg()->h)), int3(239,215,123));
  809. }
  810. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+25,FONT_SMALL,12,Colors::Cornsilk,to);
  811. }
  812. void CSelWindow::selectionChange(unsigned to)
  813. {
  814. for (unsigned i=0;i<components.size();i++)
  815. {
  816. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  817. if (!pom)
  818. continue;
  819. pom->select(i==to);
  820. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  821. }
  822. }
  823. 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)
  824. {
  825. OBJ_CONSTRUCTION_CAPTURING_ALL;
  826. ID = askID;
  827. for(int i=0;i<Buttons.size();i++)
  828. {
  829. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  830. if(!i && askID >= 0)
  831. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  832. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  833. }
  834. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::Cornsilk);
  835. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  836. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  837. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  838. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb,0,askID);
  839. for(int i=0;i<comps.size();i++)
  840. {
  841. comps[i]->recActions = 255;
  842. addChild(comps[i]);
  843. components.push_back(comps[i]);
  844. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  845. if(i<9)
  846. comps[i]->assignedKeys.insert(SDLK_1+i);
  847. }
  848. CMessage::drawIWindow(this, Text, player);
  849. BOOST_FOREACH(CComponent *c, components)
  850. c->subtitle = "";//workaround - erase subtitles since they were hard-blitted by function drawing window
  851. }
  852. void CSelWindow::madeChoice()
  853. {
  854. if(ID < 0)
  855. return;
  856. int ret = -1;
  857. for (int i=0;i<components.size();i++)
  858. {
  859. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  860. {
  861. ret = i;
  862. }
  863. }
  864. LOCPLINT->cb->selectionMade(ret+1,ID);
  865. }
  866. CHeroList::CHeroList(int Size)
  867. :CList(Size)
  868. {
  869. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  870. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  871. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  872. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  873. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  874. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  875. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  876. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  877. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  878. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  879. //32px per hero
  880. posmobx = pos.x+1;
  881. posmoby = pos.y+arrup->height+1;
  882. posporx = pos.x+mobile->width+2;
  883. pospory = pos.y+arrup->height;
  884. posmanx = pos.x+1+50+mobile->width;
  885. posmany = pos.y+arrup->height+1;
  886. from = 0;
  887. selected = -1;
  888. pressed = indeterminate;
  889. }
  890. void CHeroList::init()
  891. {
  892. int w = pos.w+1, h = pos.h+4;
  893. bg = CSDL_Ext::newSurface(w,h,screen);
  894. Rect srcRect = genRect(w, h, pos.x, pos.y);
  895. Rect dstRect = genRect(w, h, 0, 0);
  896. CSDL_Ext::blitSurface(adventureInt->bg, &srcRect, bg, &dstRect);
  897. }
  898. void CHeroList::genList()
  899. {
  900. //int howMany = LOCPLINT->cb->howManyHeroes();
  901. //for (int i=0;i<howMany;i++)
  902. //{
  903. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  904. // if(!h->inTownGarrison)
  905. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  906. //}
  907. }
  908. void CHeroList::select(int which)
  909. {
  910. if (which<0)
  911. {
  912. selected = which;
  913. adventureInt->selection = NULL;
  914. adventureInt->terrain.currentPath = NULL;
  915. draw(screen);
  916. adventureInt->infoBar.showAll(screen);
  917. }
  918. if (which>=LOCPLINT->wanderingHeroes.size())
  919. return;
  920. selected = which;
  921. adventureInt->select(LOCPLINT->wanderingHeroes[which]);
  922. fixPos();
  923. draw(screen);
  924. }
  925. void CHeroList::clickLeft(tribool down, bool previousState)
  926. {
  927. if (down)
  928. {
  929. /***************************ARROWS*****************************************/
  930. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  931. {
  932. if(from>0)
  933. {
  934. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  935. pressed = true;
  936. }
  937. return;
  938. }
  939. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  940. {
  941. if(LOCPLINT->wanderingHeroes.size()-from>SIZE)
  942. {
  943. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  944. pressed = false;
  945. }
  946. return;
  947. }
  948. /***************************HEROES*****************************************/
  949. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  950. hx-=pos.x;
  951. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  952. int ny = hy/32;
  953. if (ny>=SIZE || ny<0)
  954. return;
  955. if ( (ny+from)==selected && (adventureInt->selection->ID == GameConstants::HEROI_TYPE))
  956. LOCPLINT->openHeroWindow(LOCPLINT->wanderingHeroes[selected]);//print hero screen
  957. select(ny+from);
  958. }
  959. else
  960. {
  961. if (indeterminate(pressed))
  962. return;
  963. if (pressed) //up
  964. {
  965. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  966. pressed = indeterminate;
  967. if (!down)
  968. {
  969. from--;
  970. if (from<0)
  971. from=0;
  972. draw(screen);
  973. }
  974. }
  975. else if (!pressed) //down
  976. {
  977. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  978. pressed = indeterminate;
  979. if (!down)
  980. {
  981. from++;
  982. //if (from<items.size()-5)
  983. // from=items.size()-5;
  984. draw(screen);
  985. }
  986. }
  987. else
  988. throw 0;
  989. }
  990. }
  991. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  992. {
  993. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  994. {
  995. if (from>0)
  996. adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  997. else
  998. adventureInt->statusbar.clear();
  999. return;
  1000. }
  1001. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1002. {
  1003. if ((LOCPLINT->wanderingHeroes.size()-from) > SIZE)
  1004. adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1005. else
  1006. adventureInt->statusbar.clear();
  1007. return;
  1008. }
  1009. //if not buttons then heroes
  1010. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1011. hx-=pos.x;
  1012. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1013. int ny = hy/32;
  1014. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1015. {
  1016. adventureInt->statusbar.clear();
  1017. return;
  1018. }
  1019. std::vector<std::string> temp;
  1020. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->name);
  1021. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->type->heroClass->name);
  1022. adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1023. //select(ny+from);
  1024. }
  1025. void CHeroList::clickRight(tribool down, bool previousState)
  1026. {
  1027. if (down)
  1028. {
  1029. /***************************ARROWS*****************************************/
  1030. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1031. {
  1032. adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down);
  1033. }
  1034. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->wanderingHeroes.size()-from>5))
  1035. {
  1036. adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down);
  1037. }
  1038. else
  1039. {
  1040. //if not buttons then heroes
  1041. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1042. hx-=pos.x;
  1043. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1044. int ny = hy/32;
  1045. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1046. {
  1047. return;
  1048. } //show popup
  1049. CRClickPopup::createAndPush(LOCPLINT->wanderingHeroes[from+ny], GH.current->motion);
  1050. }
  1051. }
  1052. }
  1053. void CHeroList::hover (bool on)
  1054. {
  1055. }
  1056. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1057. {
  1058. }
  1059. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1060. {
  1061. if(toRemove) //remove specific hero
  1062. LOCPLINT->wanderingHeroes -= toRemove;
  1063. else
  1064. LOCPLINT->recreateHeroTownList();
  1065. if(selected >= LOCPLINT->wanderingHeroes.size())
  1066. select(LOCPLINT->wanderingHeroes.size()-1);
  1067. if(toRemove)
  1068. {
  1069. if(LOCPLINT->wanderingHeroes.size() == 0)
  1070. adventureInt->townList.select(0);
  1071. else
  1072. select(selected);
  1073. }
  1074. adventureInt->updateNextHero(NULL);
  1075. }
  1076. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1077. {
  1078. int ser = -1;
  1079. for(int i=0; i<LOCPLINT->wanderingHeroes.size() && ser<0; i++)
  1080. if(LOCPLINT->wanderingHeroes[i]->subID == which->subID)
  1081. ser = i;
  1082. ser -= from;
  1083. if(ser<0 || ser >= SIZE) return;
  1084. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1085. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1086. }
  1087. void CHeroList::draw(SDL_Surface * to)
  1088. {
  1089. for (int iT=0+from;iT<SIZE+from;iT++)
  1090. {
  1091. int i = iT-from;
  1092. if (iT>=LOCPLINT->wanderingHeroes.size())
  1093. {
  1094. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1095. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1096. blitAt(empty,posporx,pospory+i*32,to);
  1097. continue;
  1098. }
  1099. const CGHeroInstance *cur = LOCPLINT->wanderingHeroes[iT];
  1100. int pom = cur->movement / 100;
  1101. if (pom>25) pom=25;
  1102. if (pom<0) pom=0;
  1103. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1104. pom = cur->mana / 5;
  1105. if (pom>25) pom=25;
  1106. if (pom<0) pom=0;
  1107. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1108. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1109. blitAt(temp,posporx,pospory+i*32,to);
  1110. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == GameConstants::HEROI_TYPE))
  1111. {
  1112. blitAt(selection,posporx,pospory+i*32,to);
  1113. }
  1114. //TODO: support for custom portraits
  1115. }
  1116. if (from>0)
  1117. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1118. else
  1119. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1120. if (LOCPLINT->wanderingHeroes.size()-from > SIZE)
  1121. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1122. else
  1123. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1124. }
  1125. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1126. {
  1127. return vstd::find_pos(LOCPLINT->wanderingHeroes, h, std::equal_to<const CGHeroInstance*>());
  1128. }
  1129. void CHeroList::show(SDL_Surface * to)
  1130. {
  1131. }
  1132. void CHeroList::showAll(SDL_Surface * to)
  1133. {
  1134. draw(to);
  1135. }
  1136. int CHeroList::size()
  1137. {
  1138. return LOCPLINT->wanderingHeroes.size();
  1139. }
  1140. CTownList::~CTownList()
  1141. {
  1142. delete arrup;
  1143. delete arrdo;
  1144. }
  1145. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1146. :CList(Size)
  1147. {
  1148. arrup = CDefHandler::giveDef(arrupg);
  1149. arrdo = CDefHandler::giveDef(arrdog);
  1150. pos.x += x;
  1151. pos.y += y;
  1152. pos.w = std::max(arrdo->width, arrup->width);
  1153. pos.h = arrdo->height + arrup->height + Size*32;
  1154. arrupp.x=pos.x;
  1155. arrupp.y=pos.y;
  1156. arrupp.w=arrup->width;
  1157. arrupp.h=arrup->height;
  1158. arrdop.x=pos.x;
  1159. arrdop.y=pos.y+arrup->height+32*SIZE;
  1160. arrdop.w=arrdo->width;
  1161. arrdop.h=arrdo->height;
  1162. posporx = arrdop.x;
  1163. pospory = arrupp.y + arrupp.h;
  1164. pressed = indeterminate;
  1165. from = 0;
  1166. selected = -1;
  1167. }
  1168. void CTownList::genList()
  1169. {
  1170. // LOCPLINT->towns.clear();
  1171. // int howMany = LOCPLINT->cb->howManyTowns();
  1172. // for (int i=0;i<howMany;i++)
  1173. // {
  1174. // LOCPLINT->towns.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1175. // }
  1176. }
  1177. void CTownList::select(int which)
  1178. {
  1179. if (which>=LOCPLINT->towns.size())
  1180. return;
  1181. selected = which;
  1182. fixPos();
  1183. if(!fun.empty())
  1184. fun();
  1185. }
  1186. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1187. {
  1188. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1189. {
  1190. if (from>0)
  1191. GH.statusbar->print(CGI->generaltexth->zelp[306].first);
  1192. else
  1193. GH.statusbar->clear();
  1194. return;
  1195. }
  1196. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1197. {
  1198. if ((LOCPLINT->towns.size()-from) > SIZE)
  1199. GH.statusbar->print(CGI->generaltexth->zelp[307].first);
  1200. else
  1201. GH.statusbar->clear();
  1202. return;
  1203. }
  1204. //if not buttons then towns
  1205. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1206. hx-=pos.x;
  1207. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1208. int ny = hy/32;
  1209. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1210. {
  1211. GH.statusbar->clear();
  1212. return;
  1213. };
  1214. std::string temp = CGI->generaltexth->tcommands[4];
  1215. boost::algorithm::replace_first(temp,"%s",LOCPLINT->towns[from+ny]->name);
  1216. temp += ", "+LOCPLINT->towns[from+ny]->town->Name();
  1217. GH.statusbar->print(temp);
  1218. }
  1219. void CTownList::clickLeft(tribool down, bool previousState)
  1220. {
  1221. if (down)
  1222. {
  1223. /***************************ARROWS*****************************************/
  1224. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1225. {
  1226. if(from>0)
  1227. {
  1228. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1229. pressed = true;
  1230. }
  1231. return;
  1232. }
  1233. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1234. {
  1235. if(LOCPLINT->towns.size()-from > SIZE)
  1236. {
  1237. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1238. pressed = false;
  1239. }
  1240. return;
  1241. }
  1242. /***************************TOWNS*****************************************/
  1243. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1244. hx-=pos.x;
  1245. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1246. int ny = hy/32;
  1247. if (ny>=SIZE || ny<0)
  1248. return;
  1249. if(GH.topInt() == adventureInt
  1250. && (ny+from)==selected
  1251. && adventureInt->selection->ID == GameConstants::TOWNI_TYPE
  1252. )
  1253. LOCPLINT->openTownWindow(LOCPLINT->towns[selected]);//print town screen
  1254. else
  1255. select(ny+from);
  1256. }
  1257. else
  1258. {
  1259. if (indeterminate(pressed))
  1260. return;
  1261. if (pressed) //up
  1262. {
  1263. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1264. pressed = indeterminate;
  1265. if (!down)
  1266. {
  1267. from--;
  1268. if (from<0)
  1269. from=0;
  1270. draw(screenBuf);
  1271. }
  1272. }
  1273. else if (!pressed) //down
  1274. {
  1275. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1276. pressed = indeterminate;
  1277. if (!down)
  1278. {
  1279. from++;
  1280. //if (from<LOCPLINT->towns.size()-5)
  1281. // from=LOCPLINT->towns.size()-5;
  1282. draw(screenBuf);
  1283. }
  1284. }
  1285. else
  1286. throw 0;
  1287. }
  1288. }
  1289. void CTownList::clickRight(tribool down, bool previousState)
  1290. {
  1291. if (down)
  1292. {
  1293. /***************************ARROWS*****************************************/
  1294. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1295. {
  1296. adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down);
  1297. }
  1298. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->towns.size()-from>5))
  1299. {
  1300. adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down);
  1301. }
  1302. //if not buttons then towns
  1303. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1304. hx-=pos.x;
  1305. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1306. int ny = hy/32;
  1307. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1308. {
  1309. return;
  1310. }
  1311. //show popup
  1312. CRClickPopup::createAndPush(LOCPLINT->towns[from+ny], GH.current->motion);
  1313. }
  1314. }
  1315. void CTownList::hover (bool on)
  1316. {
  1317. }
  1318. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1319. {
  1320. }
  1321. void CTownList::draw(SDL_Surface * to)
  1322. {
  1323. for (int iT=0+from;iT<SIZE+from;iT++)
  1324. {
  1325. int i = iT-from;
  1326. if (iT>=LOCPLINT->towns.size())
  1327. {
  1328. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1329. continue;
  1330. }
  1331. blitAt(graphics->getPic(LOCPLINT->towns[iT]->subID,LOCPLINT->towns[iT]->hasFort(),LOCPLINT->towns[iT]->builded),posporx,pospory+i*32,to);
  1332. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == GameConstants::TOWNI_TYPE))
  1333. {
  1334. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1335. }
  1336. }
  1337. if (from>0)
  1338. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1339. else
  1340. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1341. if (LOCPLINT->towns.size()-from>SIZE)
  1342. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1343. else
  1344. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1345. }
  1346. void CTownList::show(SDL_Surface * to)
  1347. {
  1348. }
  1349. void CTownList::showAll(SDL_Surface * to)
  1350. {
  1351. draw(to);
  1352. }
  1353. int CTownList::size()
  1354. {
  1355. return LOCPLINT->towns.size();
  1356. }
  1357. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  1358. {
  1359. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1360. pos.x+=x;
  1361. pos.y+=y;
  1362. if(Big)
  1363. bg = new CPicture(graphics->backgrounds[cre->faction],0,0,false);
  1364. else
  1365. bg = new CPicture(graphics->backgroundsm[cre->faction],0,0,false);
  1366. bg->needRefresh = true;
  1367. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  1368. anim->clipRect(cre->doubleWide?170:150, 155, bg->pos.w, bg->pos.h);
  1369. anim->startPreview();
  1370. }
  1371. CCreaturePic::~CCreaturePic()
  1372. {
  1373. }
  1374. void CRecruitmentWindow::close()
  1375. {
  1376. GH.popIntTotally(this);
  1377. }
  1378. void CRecruitmentWindow::Max()
  1379. {
  1380. slider->moveToMax();
  1381. }
  1382. void CRecruitmentWindow::Buy()
  1383. {
  1384. int crid = creatures[which].ID,
  1385. dstslot = dst-> getSlotFor(crid);
  1386. if(dstslot < 0 && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->convertMachineID(crid, true))) //no available slot
  1387. {
  1388. std::string txt;
  1389. if(dst->ID == GameConstants::HEROI_TYPE)
  1390. {
  1391. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1392. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1393. }
  1394. else
  1395. {
  1396. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1397. }
  1398. LOCPLINT->showInfoDialog(txt);
  1399. return;
  1400. }
  1401. recruit(crid, slider->value);
  1402. if(level >= 0)
  1403. close();
  1404. else
  1405. slider->moveTo(0);
  1406. }
  1407. void CRecruitmentWindow::Cancel()
  1408. {
  1409. close();
  1410. }
  1411. void CRecruitmentWindow::sliderMoved(int to)
  1412. {
  1413. buy->block(!to);
  1414. redraw();
  1415. }
  1416. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1417. {
  1418. for(int i=0;i<creatures.size();i++)
  1419. {
  1420. Rect creaPos = Rect(creatures[i].pos) + pos;
  1421. if(isItIn(&creaPos, GH.current->motion.x, GH.current->motion.y))
  1422. {
  1423. which = i;
  1424. int newAmount = std::min(amounts[i],creatures[i].amount);
  1425. slider->setAmount(newAmount);
  1426. max->block(!newAmount);
  1427. if(slider->value > newAmount)
  1428. slider->moveTo(newAmount);
  1429. else
  1430. slider->moveTo(slider->value);
  1431. redraw();
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1437. {
  1438. if(down)
  1439. {
  1440. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1441. for(int i=0;i<creatures.size();i++)
  1442. {
  1443. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1444. Rect creatureRect = genRect(132, sCREATURE_WIDTH, pos.x+curx, pos.y+64);
  1445. if(isItIn(&creatureRect, GH.current->motion.x, GH.current->motion.y))
  1446. {
  1447. CIntObject *popup = createCreWindow(creatures[i].ID, 0, 0);
  1448. GH.pushInt(popup);
  1449. break;
  1450. }
  1451. curx += TOTAL_CREATURE_WIDTH;
  1452. }
  1453. }
  1454. }
  1455. void CRecruitmentWindow::showAll(SDL_Surface * to)
  1456. {
  1457. CIntObject::showAll(to);
  1458. char pom[15];
  1459. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1460. printAtMiddleLoc(pom,205,253,FONT_SMALL,Colors::Cornsilk,to);
  1461. SDL_itoa(slider->value,pom,10); //recruit
  1462. printAtMiddleLoc(pom,279,253,FONT_SMALL,Colors::Cornsilk,to);
  1463. printAtMiddleLoc(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID]->namePl,243,32,FONT_BIG,Colors::Jasmine,to); //eg "Recruit Dragon flies"
  1464. int curx = 122-creatures[which].res.size()*24;
  1465. for(int i=creatures[which].res.size()-1; i>=0; i--)// decrement used to make gold displayed as first res
  1466. {
  1467. blitAtLoc(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,243,to);
  1468. blitAtLoc(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,243,to);
  1469. SDL_itoa(creatures[which].res[i].second,pom,10);
  1470. printAtMiddleLoc(pom,curx+15,287,FONT_SMALL,Colors::Cornsilk,to);
  1471. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1472. printAtMiddleLoc(pom,curx+15+258,287,FONT_SMALL,Colors::Cornsilk,to);
  1473. curx+=32+16;//size of bitmap + distance between them
  1474. }
  1475. for(int j=0;j<creatures.size();j++)
  1476. {
  1477. if(which==j)
  1478. drawBorderLoc(to,creatures[j].pos,int3(255,0,0));
  1479. else
  1480. drawBorderLoc(to,creatures[j].pos,int3(239,215,123));
  1481. }
  1482. }
  1483. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit, int y_offset)
  1484. :recruit(Recruit), dwelling(Dwelling), level(Level), dst(Dst)
  1485. {
  1486. used = LCLICK | RCLICK;
  1487. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1488. which = 0;
  1489. bitmap = new CPicture("TPRCRT.bmp");
  1490. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  1491. bitmap->center();
  1492. pos = (bitmap->pos += Point(0, y_offset));
  1493. bar = new CGStatusBar(8, 370, "APHLFTRT.bmp", 471);
  1494. max = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),134,313,"IRCBTNS.DEF",SDLK_m);
  1495. buy = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  1496. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  1497. slider = new CSlider(176,279,135,0,0,0,0,true);
  1498. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  1499. initCres();
  1500. printAtMiddle(CGI->generaltexth->allTexts[346],113,232,FONT_SMALL,Colors::Cornsilk,*bitmap); //cost per troop t
  1501. printAtMiddle(CGI->generaltexth->allTexts[465],205,233,FONT_SMALL,Colors::Cornsilk,*bitmap); //available t
  1502. printAtMiddle(CGI->generaltexth->allTexts[16],279,233,FONT_SMALL,Colors::Cornsilk,*bitmap); //recruit t
  1503. printAtMiddle(CGI->generaltexth->allTexts[466],371,232,FONT_SMALL,Colors::Cornsilk,*bitmap); //total cost t
  1504. drawBorder(*bitmap,172,222,67,42,int3(239,215,123));
  1505. drawBorder(*bitmap,246,222,67,42,int3(239,215,123));
  1506. drawBorder(*bitmap,64,222,99,76,int3(239,215,123));
  1507. drawBorder(*bitmap,322,222,99,76,int3(239,215,123));
  1508. drawBorder(*bitmap,133,312,66,34,int3(173,142,66));
  1509. drawBorder(*bitmap,211,312,66,34,int3(173,142,66));
  1510. drawBorder(*bitmap,289,312,66,34,int3(173,142,66));
  1511. //border for creatures
  1512. int curx = 192 + 50 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1513. for(int i=0;i<creatures.size();i++)
  1514. {
  1515. creatures[i].pos.x = curx-1;
  1516. creatures[i].pos.y = 65 - 1;
  1517. creatures[i].pos.w = 100 + 2;
  1518. creatures[i].pos.h = 130 + 2;
  1519. // if(which==i)
  1520. // drawBorder(*bitmap,curx-1,64,CREATURE_WIDTH,132,int3(255,0,0));
  1521. // else
  1522. // drawBorder(*bitmap,curx-1,64,CREATURE_WIDTH,132,int3(239,215,123));
  1523. creatures[i].pic = new CCreaturePic(curx, 65, CGI->creh->creatures[creatures[i].ID]);
  1524. curx += TOTAL_CREATURE_WIDTH;
  1525. }
  1526. if(!creatures[0].amount || !amounts[0])
  1527. {
  1528. max->block(true);
  1529. slider->block(true);
  1530. }
  1531. buy->block(true);
  1532. }
  1533. CRecruitmentWindow::~CRecruitmentWindow()
  1534. {
  1535. }
  1536. void CRecruitmentWindow::initCres()
  1537. {
  1538. creatures.clear();
  1539. amounts.clear();
  1540. for(int i=0; i<dwelling->creatures.size(); i++)
  1541. {
  1542. if(level >= 0 && i != level)
  1543. continue;
  1544. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1545. {
  1546. creatures.resize(creatures.size()+1);
  1547. creinfo &cur = creatures.back();
  1548. cur.amount = dwelling->creatures[i].first;
  1549. cur.ID = dwelling->creatures[i].second[j];
  1550. const CCreature * cre= CGI->creh->creatures[cur.ID];
  1551. for(int k=0; k<cre->cost.size(); k++)
  1552. if(cre->cost[k])
  1553. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1554. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1555. }
  1556. }
  1557. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1558. }
  1559. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1560. {
  1561. last = Last;
  1562. which = 1;
  1563. c=cid;
  1564. slider = NULL;
  1565. gar = Owner;
  1566. SDL_Surface *hhlp = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1567. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1568. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1569. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1570. SDL_FreeSurface(hhlp);
  1571. pos.x = screen->w/2 - bitmap->w/2;
  1572. pos.y = screen->h/2 - bitmap->h/2;
  1573. pos.w = bitmap->w;
  1574. pos.h = bitmap->h;
  1575. ok = new CAdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1576. cancel = new CAdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1577. int sliderPositions = max - (last>=0) - (last==2);
  1578. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),0,sliderPositions,val,true);
  1579. a1 = max-val;
  1580. a2 = val;
  1581. animLeft = new CCreaturePic(pos.x+20, pos.y+54, CGI->creh->creatures[cid], true, false);
  1582. animRight = new CCreaturePic(pos.x+177, pos.y+54, CGI->creh->creatures[cid], true, false);
  1583. std::string title = CGI->generaltexth->allTexts[256];
  1584. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid]->namePl);
  1585. printAtMiddle(title,150,34,FONT_BIG,Colors::Jasmine,bitmap);
  1586. }
  1587. CSplitWindow::~CSplitWindow() //d-tor
  1588. {
  1589. SDL_FreeSurface(bitmap);
  1590. delete ok;
  1591. delete cancel;
  1592. delete slider;
  1593. delete animLeft;
  1594. delete animRight;
  1595. }
  1596. void CSplitWindow::activate()
  1597. {
  1598. activateLClick();
  1599. activateKeys();
  1600. ok->activate();
  1601. cancel->activate();
  1602. slider->activate();
  1603. }
  1604. void CSplitWindow::deactivate()
  1605. {
  1606. deactivateLClick();
  1607. deactivateKeys();
  1608. ok->deactivate();
  1609. cancel->deactivate();
  1610. slider->deactivate();
  1611. }
  1612. void CSplitWindow::split()
  1613. {
  1614. gar->splitStacks(a2);
  1615. close();
  1616. }
  1617. void CSplitWindow::close()
  1618. {
  1619. GH.popIntTotally(this);
  1620. }
  1621. void CSplitWindow::sliderMoved(int to)
  1622. {
  1623. int all = a1+a2;
  1624. a2 = to + (last==1 || last==2);
  1625. if(slider)
  1626. a1 = all - a2;
  1627. }
  1628. void CSplitWindow::show(SDL_Surface * to)
  1629. {
  1630. blitAt(bitmap,pos.x,pos.y,to);
  1631. ok->showAll(to);
  1632. cancel->showAll(to);
  1633. slider->showAll(to);
  1634. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,FONT_BIG,Colors::Cornsilk,to);
  1635. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,FONT_BIG,Colors::Cornsilk,to);
  1636. animLeft->show(to);
  1637. animRight->show(to);
  1638. }
  1639. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1640. {
  1641. SDLKey k = key.keysym.sym;
  1642. if (CGuiHandler::isNumKey(k)) //convert numpad number to normal digit
  1643. k = CGuiHandler::numToDigit(k);
  1644. if(key.state != SDL_PRESSED)
  1645. return;
  1646. int &cur = (which ? a2 : a1),
  1647. &sec = (which ? a1 : a2),
  1648. ncur = cur;
  1649. if (k == SDLK_BACKSPACE)
  1650. {
  1651. ncur /= 10;
  1652. }
  1653. else if(k == SDLK_TAB)
  1654. {
  1655. which = !which;
  1656. }
  1657. else if (k == SDLK_LEFT)
  1658. {
  1659. ncur--;
  1660. }
  1661. else if (k == SDLK_RIGHT)
  1662. {
  1663. ncur++;
  1664. }
  1665. else
  1666. {
  1667. int number = k - SDLK_0;
  1668. if (number < 0 || number > 9) //not a number pressed
  1669. {
  1670. return;
  1671. }
  1672. ncur = cur*10 + number;
  1673. }
  1674. int delta = ncur - cur;
  1675. if(delta > sec)
  1676. {
  1677. cur += sec;
  1678. sec = 0;
  1679. }
  1680. slider->moveTo(which ? ncur : a1+a2-ncur);
  1681. }
  1682. void CSplitWindow::clickLeft(tribool down, bool previousState)
  1683. {
  1684. if(down)
  1685. {
  1686. Point click(GH.current->motion.x,GH.current->motion.y);
  1687. click = click - pos.topLeft();
  1688. if(Rect(19,216,105,40).isIn(click)) //left picture
  1689. which = 0;
  1690. else if(Rect(175,216,105,40).isIn(click)) //right picture
  1691. which = 1;
  1692. }
  1693. }
  1694. void CLevelWindow::close()
  1695. {
  1696. for(int i=0;i<comps.size();i++)
  1697. {
  1698. if(comps[i]->selected)
  1699. {
  1700. cb(i);
  1701. break;
  1702. }
  1703. }
  1704. GH.popIntTotally(this);
  1705. LOCPLINT->showingDialog->setn(false);
  1706. }
  1707. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  1708. {
  1709. LOCPLINT->showingDialog->setn(true);
  1710. heroPortrait = hero->portrait;
  1711. cb = callback;
  1712. for(int i=0;i<skills.size();i++)
  1713. {
  1714. comps.push_back(new CSelectableComponent(CComponent::secskill44, skills[i],
  1715. hero->getSecSkillLevel( static_cast<CGHeroInstance::SecondarySkill>(skills[i]) )+1,
  1716. boost::bind(&CLevelWindow::selectionChanged,this,i)));
  1717. comps.back()->assignedKeys.insert(SDLK_1 + i);
  1718. }
  1719. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  1720. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1721. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1722. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1723. SDL_FreeSurface(hhlp);
  1724. pos.x = screen->w/2 - bitmap->w/2;
  1725. pos.y = screen->h/2 - bitmap->h/2;
  1726. pos.w = bitmap->w;
  1727. pos.h = bitmap->h;
  1728. ok = new CAdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  1729. //draw window
  1730. char buf[100], buf2[100];
  1731. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  1732. sprintf(buf,buf2,hero->name.c_str());
  1733. printAtMiddle(buf,192,33,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1734. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  1735. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  1736. printAtMiddle(buf,192,162,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1737. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  1738. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1739. const Font *f = graphics->fonts[FONT_MEDIUM];
  1740. std::string text = CGI->generaltexth->allTexts[4];
  1741. int fontWidth = f->getWidth(text.c_str())/2;
  1742. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  1743. for(int i=0;i<comps.size();i++)
  1744. {
  1745. comps[i]->pos.x = curx+pos.x;
  1746. comps[i]->pos.y = 326+pos.y;
  1747. if( i < (comps.size()-1) )
  1748. {
  1749. curx += 44+21; //skill width + margin to "or"
  1750. printAtMiddle(text ,curx,346,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1751. curx += fontWidth+15;
  1752. }
  1753. }
  1754. if(comps.size())
  1755. {
  1756. comps[0]->select(true);
  1757. }
  1758. }
  1759. void CLevelWindow::selectionChanged(unsigned to)
  1760. {
  1761. if(ok->isBlocked())
  1762. ok->block(false);
  1763. for(int i=0;i<comps.size();i++)
  1764. if(i==to)
  1765. comps[i]->select(true);
  1766. else
  1767. comps[i]->select(false);
  1768. }
  1769. CLevelWindow::~CLevelWindow()
  1770. {
  1771. delete ok;
  1772. for(int i=0;i<comps.size();i++)
  1773. delete comps[i];
  1774. SDL_FreeSurface(bitmap);
  1775. }
  1776. void CLevelWindow::activate()
  1777. {
  1778. ok->activate();
  1779. for(int i=0;i<comps.size();i++)
  1780. comps[i]->activate();
  1781. }
  1782. void CLevelWindow::deactivate()
  1783. {
  1784. ok->deactivate();
  1785. for(int i=0;i<comps.size();i++)
  1786. comps[i]->deactivate();
  1787. }
  1788. void CLevelWindow::show(SDL_Surface * to)
  1789. {
  1790. blitAt(bitmap,pos.x,pos.y,to);
  1791. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  1792. ok->showAll(to);
  1793. for(int i=0;i<comps.size();i++)
  1794. comps[i]->show(to);
  1795. }
  1796. void CMinorResDataBar::show(SDL_Surface * to)
  1797. {
  1798. blitAt(bg,pos.x,pos.y,to);
  1799. char buf[30];
  1800. for (int i=0;i<7;i++)
  1801. {
  1802. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  1803. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,Colors::Cornsilk,to);
  1804. }
  1805. std::vector<std::string> temp;
  1806. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  1807. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  1808. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  1809. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  1810. CGI->generaltexth->allTexts[62]
  1811. +": %s, "
  1812. + CGI->generaltexth->allTexts[63]
  1813. + ": %s, "
  1814. + CGI->generaltexth->allTexts[64]
  1815. + ": %s",temp)
  1816. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,Colors::Cornsilk,to);
  1817. }
  1818. void CMinorResDataBar::showAll(SDL_Surface * to)
  1819. {
  1820. show(to);
  1821. }
  1822. CMinorResDataBar::CMinorResDataBar()
  1823. {
  1824. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  1825. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1826. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1827. pos.x = 7;
  1828. pos.y = 575;
  1829. pos.w = bg->w;
  1830. pos.h = bg->h;
  1831. }
  1832. CMinorResDataBar::~CMinorResDataBar()
  1833. {
  1834. SDL_FreeSurface(bg);
  1835. }
  1836. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  1837. parent(_parent),
  1838. index(_id)
  1839. {
  1840. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1841. border = new CPicture("TPGATES");
  1842. pos = border->pos;
  1843. used |= LCLICK;
  1844. type |= REDRAW_PARENT;
  1845. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::Cornsilk, _text);
  1846. select(index == parent->selected);
  1847. }
  1848. void CObjectListWindow::CItem::select(bool on)
  1849. {
  1850. if (on)
  1851. border->recActions = 255;
  1852. else
  1853. border->recActions = ~(UPDATE | SHOWALL);
  1854. redraw();
  1855. }
  1856. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  1857. {
  1858. if( previousState && !down)
  1859. parent->changeSelection(index);
  1860. }
  1861. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1862. boost::function<void(int)> Callback):
  1863. onSelect(Callback)
  1864. {
  1865. items.reserve(_items.size());
  1866. BOOST_FOREACH(int id, _items)
  1867. {
  1868. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->hoverName));
  1869. }
  1870. init(titlePic, _title, _descr);
  1871. }
  1872. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1873. boost::function<void(int)> Callback):
  1874. onSelect(Callback)
  1875. {
  1876. items.reserve(_items.size());
  1877. for (size_t i=0; i<_items.size(); i++)
  1878. items.push_back(std::make_pair(int(i), _items[i]));
  1879. init(titlePic, _title, _descr);
  1880. }
  1881. void CObjectListWindow::init(CPicture * titlePic, std::string _title, std::string _descr)
  1882. {
  1883. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1884. bg = new CPicture("TPGATE.pcx");//x=0, y=0
  1885. bg->colorizeAndConvert(LOCPLINT->playerID);
  1886. pos = bg->center();
  1887. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::Jasmine, _title);
  1888. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::Cornsilk, _descr);
  1889. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  1890. ok->block(true);
  1891. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  1892. if (titlePic)
  1893. {
  1894. titleImage = titlePic;
  1895. addChild(titleImage);
  1896. titleImage->recActions = defActions;
  1897. titleImage->pos.x =bg->pos.w/2 + pos.x - titleImage->pos.w/2;
  1898. titleImage->pos.y =75 + pos.y - titleImage->pos.h/2;
  1899. }
  1900. list = new CListBox(boost::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  1901. Point(15, 152), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1902. list->type |= REDRAW_PARENT;
  1903. }
  1904. CIntObject * CObjectListWindow::genItem(size_t index)
  1905. {
  1906. if (index < items.size())
  1907. return new CItem(this, index, items[index].second);
  1908. return NULL;
  1909. }
  1910. void CObjectListWindow::elementSelected()
  1911. {
  1912. boost::function<void(int)> toCall = onSelect;//save
  1913. int where = items[selected].first; //required variables
  1914. GH.popIntTotally(this);//then destroy window
  1915. toCall(where);//and send selected object
  1916. }
  1917. void CObjectListWindow::changeSelection(size_t which)
  1918. {
  1919. ok->block(false);
  1920. if (selected == which)
  1921. return;
  1922. std::list< CIntObject * > elements = list->getItems();
  1923. BOOST_FOREACH(CIntObject * element, elements)
  1924. {
  1925. CItem *item;
  1926. if ( (item = dynamic_cast<CItem*>(element)) )
  1927. if (item->index == selected)
  1928. item->select(false);
  1929. if (item->index == which)
  1930. item->select(true);
  1931. }
  1932. selected = which;
  1933. }
  1934. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  1935. {
  1936. if(key.state != SDL_PRESSED)
  1937. return;
  1938. int sel = selected;
  1939. switch(key.keysym.sym)
  1940. {
  1941. break; case SDLK_UP:
  1942. sel -=1;
  1943. break; case SDLK_DOWN:
  1944. sel +=1;
  1945. break; case SDLK_PAGEUP:
  1946. sel -=9;
  1947. break; case SDLK_PAGEDOWN:
  1948. sel +=9;
  1949. break; case SDLK_HOME:
  1950. sel = 0;
  1951. break; case SDLK_END:
  1952. sel = items.size();
  1953. break; default:
  1954. return;
  1955. }
  1956. vstd::abetween(sel, 0, items.size()-1);
  1957. list->scrollTo(sel);
  1958. changeSelection(sel);
  1959. }
  1960. CTradeWindow::CTradeableItem::CTradeableItem( EType Type, int ID, bool Left, int Serial)
  1961. {
  1962. serial = Serial;
  1963. left = Left;
  1964. type = Type;
  1965. id = ID;
  1966. used = LCLICK | HOVER | RCLICK;
  1967. downSelection = false;
  1968. hlp = NULL;
  1969. }
  1970. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  1971. {
  1972. Point posToBitmap;
  1973. Point posToSubCenter;
  1974. switch(type)
  1975. {
  1976. case RESOURCE:
  1977. posToBitmap = Point(19,9);
  1978. posToSubCenter = Point(36, 59);
  1979. break;
  1980. case CREATURE_PLACEHOLDER:
  1981. case CREATURE:
  1982. posToSubCenter = Point(29, 76);
  1983. if(downSelection)
  1984. posToSubCenter.y += 5;
  1985. break;
  1986. case PLAYER:
  1987. posToSubCenter = Point(31, 76);
  1988. break;
  1989. case ARTIFACT_PLACEHOLDER:
  1990. case ARTIFACT_INSTANCE:
  1991. posToSubCenter = Point(19, 55);
  1992. if(downSelection)
  1993. posToSubCenter.y += 8;
  1994. break;
  1995. case ARTIFACT_TYPE:
  1996. posToSubCenter = Point(19, 58);
  1997. break;
  1998. }
  1999. if(SDL_Surface *hlp = getSurface())
  2000. blitAt(hlp, pos + posToBitmap, to);
  2001. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::Cornsilk, to);
  2002. }
  2003. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2004. {
  2005. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  2006. assert(mw);
  2007. if(down)
  2008. {
  2009. if(type == ARTIFACT_PLACEHOLDER)
  2010. {
  2011. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  2012. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  2013. {
  2014. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  2015. }
  2016. else if(const CArtifactInstance *art = getArtInstance())
  2017. {
  2018. aw->arts->commonInfo->src.AOH = aw->arts;
  2019. aw->arts->commonInfo->src.art = art;
  2020. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  2021. aw->arts->markPossibleSlots(art);
  2022. //aw->arts->commonInfo->dst.AOH = aw->arts;
  2023. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[art->artType->id].bitmap);
  2024. aw->arts->artifactsOnAltar.erase(art);
  2025. id = -1;
  2026. subtitle = "";
  2027. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  2028. }
  2029. aw->calcTotalExp();
  2030. return;
  2031. }
  2032. if(left)
  2033. {
  2034. if(mw->hLeft != this)
  2035. mw->hLeft = this;
  2036. else
  2037. return;
  2038. }
  2039. else
  2040. {
  2041. if(mw->hRight != this)
  2042. mw->hRight = this;
  2043. else
  2044. return;
  2045. }
  2046. mw->selectionChanged(left);
  2047. }
  2048. }
  2049. SDL_Surface * CTradeWindow::CTradeableItem::getSurface()
  2050. {
  2051. switch(type)
  2052. {
  2053. case RESOURCE:
  2054. return graphics->resources32->ourImages[id].bitmap;
  2055. case PLAYER:
  2056. return graphics->flags->ourImages[id].bitmap;
  2057. case ARTIFACT_TYPE:
  2058. case ARTIFACT_PLACEHOLDER:
  2059. case ARTIFACT_INSTANCE:
  2060. return id >= 0 ? graphics->artDefs->ourImages[id].bitmap : NULL;
  2061. case CREATURE:
  2062. return graphics->bigImgs[id];
  2063. default:
  2064. return NULL;
  2065. }
  2066. }
  2067. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  2068. {
  2069. Rect oldPos = pos;
  2070. std::string oldSub = subtitle;
  2071. downSelection = true;
  2072. pos = dstPos;
  2073. subtitle = customSub;
  2074. showAll(to);
  2075. downSelection = false;
  2076. pos = oldPos;
  2077. subtitle = oldSub;
  2078. }
  2079. void CTradeWindow::CTradeableItem::hover(bool on)
  2080. {
  2081. if(!on)
  2082. {
  2083. GH.statusbar->clear();
  2084. return;
  2085. }
  2086. switch(type)
  2087. {
  2088. case CREATURE:
  2089. case CREATURE_PLACEHOLDER:
  2090. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2091. break;
  2092. case ARTIFACT_PLACEHOLDER:
  2093. if(id < 0)
  2094. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  2095. else
  2096. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  2097. break;
  2098. }
  2099. }
  2100. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  2101. {
  2102. if(down)
  2103. {
  2104. switch(type)
  2105. {
  2106. case CREATURE:
  2107. case CREATURE_PLACEHOLDER:
  2108. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2109. break;
  2110. case ARTIFACT_TYPE:
  2111. case ARTIFACT_PLACEHOLDER:
  2112. if(id >= 0)
  2113. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  2114. break;
  2115. }
  2116. }
  2117. }
  2118. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  2119. {
  2120. switch(type)
  2121. {
  2122. case PLAYER:
  2123. return CGI->generaltexth->capColors[id];
  2124. case RESOURCE:
  2125. return CGI->generaltexth->restypes[id];
  2126. case CREATURE:
  2127. if(number == 1)
  2128. return CGI->creh->creatures[id]->nameSing;
  2129. else
  2130. return CGI->creh->creatures[id]->namePl;
  2131. case ARTIFACT_TYPE:
  2132. case ARTIFACT_INSTANCE:
  2133. return CGI->arth->artifacts[id]->Name();
  2134. }
  2135. assert(0);
  2136. return "";
  2137. }
  2138. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  2139. {
  2140. switch(type)
  2141. {
  2142. case ARTIFACT_PLACEHOLDER:
  2143. case ARTIFACT_INSTANCE:
  2144. return (const CArtifactInstance *)hlp;
  2145. default:
  2146. return NULL;
  2147. }
  2148. }
  2149. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  2150. {
  2151. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  2152. hlp = art;
  2153. if(art)
  2154. id = art->artType->id;
  2155. else
  2156. id = -1;
  2157. }
  2158. CTradeWindow::CTradeWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2159. : market(Market), hero(Hero), arts(NULL), hLeft(NULL), hRight(NULL), readyToTrade(false)
  2160. {
  2161. type |= BLOCK_ADV_HOTKEYS;
  2162. mode = Mode;
  2163. initTypes();
  2164. }
  2165. void CTradeWindow::initTypes()
  2166. {
  2167. switch(mode)
  2168. {
  2169. case EMarketMode::RESOURCE_RESOURCE:
  2170. itemsType[1] = RESOURCE;
  2171. itemsType[0] = RESOURCE;
  2172. break;
  2173. case EMarketMode::RESOURCE_PLAYER:
  2174. itemsType[1] = RESOURCE;
  2175. itemsType[0] = PLAYER;
  2176. break;
  2177. case EMarketMode::CREATURE_RESOURCE:
  2178. itemsType[1] = CREATURE;
  2179. itemsType[0] = RESOURCE;
  2180. break;
  2181. case EMarketMode::RESOURCE_ARTIFACT:
  2182. itemsType[1] = RESOURCE;
  2183. itemsType[0] = ARTIFACT_TYPE;
  2184. break;
  2185. case EMarketMode::ARTIFACT_RESOURCE:
  2186. itemsType[1] = ARTIFACT_INSTANCE;
  2187. itemsType[0] = RESOURCE;
  2188. break;
  2189. case EMarketMode::CREATURE_EXP:
  2190. itemsType[1] = CREATURE;
  2191. itemsType[0] = CREATURE_PLACEHOLDER;
  2192. break;
  2193. case EMarketMode::ARTIFACT_EXP:
  2194. itemsType[1] = ARTIFACT_TYPE;
  2195. itemsType[0] = ARTIFACT_PLACEHOLDER;
  2196. break;
  2197. }
  2198. }
  2199. void CTradeWindow::initItems(bool Left)
  2200. {
  2201. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  2202. {
  2203. int xOffset = 0, yOffset = 0;
  2204. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2205. {
  2206. xOffset = -361;
  2207. yOffset = +46;
  2208. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], -1, 1, 0);
  2209. hlp->recActions &= ~(UPDATE | SHOWALL);
  2210. hlp->pos += Rect(137, 469, 42, 42);
  2211. items[Left].push_back(hlp);
  2212. }
  2213. else //ARTIFACT_EXP
  2214. {
  2215. xOffset = -363;
  2216. yOffset = -12;
  2217. }
  2218. BLOCK_CAPTURING;
  2219. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  2220. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  2221. arts->commonInfo->participants.insert(arts);
  2222. arts->recActions = 255;
  2223. arts->setHero(hero);
  2224. arts->allowedAssembling = false;
  2225. addChild(arts);
  2226. artSets.push_back(arts);
  2227. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2228. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  2229. return;
  2230. }
  2231. std::vector<int> *ids = getItemsIds(Left);
  2232. std::vector<Rect> pos;
  2233. int amount = -1;
  2234. getPositionsFor(pos, Left, itemsType[Left]);
  2235. if(Left || !ids)
  2236. amount = 7;
  2237. else
  2238. amount = ids->size();
  2239. if(ids)
  2240. vstd::amin(amount, ids->size());
  2241. for(int j=0; j<amount; j++)
  2242. {
  2243. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  2244. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  2245. continue;
  2246. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], id, Left, j);
  2247. hlp->pos = pos[j] + hlp->pos;
  2248. items[Left].push_back(hlp);
  2249. }
  2250. initSubs(Left);
  2251. }
  2252. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  2253. {
  2254. std::vector<int> *ids = NULL;
  2255. if(mode == EMarketMode::ARTIFACT_EXP)
  2256. return new std::vector<int>(22, -1);
  2257. if(Left)
  2258. {
  2259. switch(itemsType[1])
  2260. {
  2261. case CREATURE:
  2262. ids = new std::vector<int>;
  2263. for(int i = 0; i < 7; i++)
  2264. {
  2265. if(const CCreature *c = hero->getCreature(i))
  2266. ids->push_back(c->idNumber);
  2267. else
  2268. ids->push_back(-1);
  2269. }
  2270. break;
  2271. }
  2272. }
  2273. else
  2274. {
  2275. switch(itemsType[0])
  2276. {
  2277. case PLAYER:
  2278. ids = new std::vector<int>;
  2279. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  2280. if(i != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  2281. ids->push_back(i);
  2282. break;
  2283. case ARTIFACT_TYPE:
  2284. ids = new std::vector<int>(market->availableItemsIds(mode));
  2285. break;
  2286. }
  2287. }
  2288. return ids;
  2289. }
  2290. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  2291. {
  2292. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  2293. {
  2294. //22 boxes, 5 in row, last row: two boxes centered
  2295. int h, w, x, y, dx, dy;
  2296. h = w = 44;
  2297. x = 317;
  2298. y = 53;
  2299. dx = 54;
  2300. dy = 70;
  2301. for (int i = 0; i < 4 ; i++)
  2302. for (int j = 0; j < 5 ; j++)
  2303. poss += Rect(x + dx*j, y + dy*i, w, h);
  2304. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  2305. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  2306. }
  2307. else
  2308. {
  2309. //seven boxes:
  2310. // X X X
  2311. // X X X
  2312. // X
  2313. int h, w, x, y, dx, dy;
  2314. int leftToRightOffset;
  2315. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  2316. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  2317. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  2318. genRect(h, w, x + dx, y + 2*dy);
  2319. if(!Left)
  2320. {
  2321. BOOST_FOREACH(Rect &r, poss)
  2322. r.x += leftToRightOffset;
  2323. }
  2324. }
  2325. }
  2326. void CTradeWindow::initSubs(bool Left)
  2327. {
  2328. BOOST_FOREACH(CTradeableItem *t, items[Left])
  2329. {
  2330. if(Left)
  2331. {
  2332. switch(itemsType[1])
  2333. {
  2334. case CREATURE:
  2335. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(t->serial));
  2336. break;
  2337. case RESOURCE:
  2338. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(t->serial));
  2339. break;
  2340. }
  2341. }
  2342. else //right side
  2343. {
  2344. if(itemsType[0] == PLAYER)
  2345. {
  2346. t->subtitle = CGI->generaltexth->capColors[t->id];
  2347. }
  2348. else if(hLeft)//artifact, creature
  2349. {
  2350. int h1, h2; //hlp variables for getting offer
  2351. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  2352. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  2353. {
  2354. std::ostringstream oss;
  2355. oss << h2;
  2356. if(h1!=1)
  2357. oss << "/" << h1;
  2358. t->subtitle = oss.str();
  2359. }
  2360. else
  2361. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  2362. }
  2363. else
  2364. t->subtitle = "";
  2365. }
  2366. }
  2367. }
  2368. void CTradeWindow::showAll(SDL_Surface * to)
  2369. {
  2370. CIntObject::showAll(to);
  2371. if(hRight)
  2372. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2373. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  2374. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2375. if(readyToTrade)
  2376. {
  2377. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2378. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2379. }
  2380. }
  2381. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2382. {
  2383. BOOST_FOREACH(CTradeableItem *t, toRemove)
  2384. removeItem(t);
  2385. }
  2386. void CTradeWindow::removeItem(CTradeableItem * t)
  2387. {
  2388. if(active)
  2389. t->deactivate();
  2390. items[t->left] -= t;
  2391. delChild(t);
  2392. if(hRight == t)
  2393. {
  2394. hRight = NULL;
  2395. selectionChanged(false);
  2396. }
  2397. }
  2398. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2399. {
  2400. BOOST_FOREACH(CTradeableItem *t, items[1])
  2401. if(!hero->getStackCount(t->serial))
  2402. toRemove.insert(t);
  2403. }
  2404. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  2405. {
  2406. const IMarket *m = market;
  2407. const CGHeroInstance *h = hero;
  2408. CTradeWindow *nwindow = NULL;
  2409. GH.popIntTotally(this);
  2410. switch(Mode)
  2411. {
  2412. case EMarketMode::CREATURE_EXP:
  2413. case EMarketMode::ARTIFACT_EXP:
  2414. nwindow = new CAltarWindow(m, h, Mode);
  2415. break;
  2416. default:
  2417. nwindow = new CMarketplaceWindow(m, h, Mode);
  2418. break;
  2419. }
  2420. GH.pushInt(nwindow);
  2421. }
  2422. void CTradeWindow::artifactSelected(CArtPlace *slot)
  2423. {
  2424. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  2425. items[1][0]->setArtInstance(slot->ourArt);
  2426. if(slot->ourArt)
  2427. hLeft = items[1][0];
  2428. else
  2429. hLeft = NULL;
  2430. selectionChanged(true);
  2431. }
  2432. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2433. : CTradeWindow(Market, Hero, Mode)
  2434. {
  2435. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2436. madeTransaction = false;
  2437. std::string bgName;
  2438. bool sliderNeeded = true;
  2439. switch(Mode)
  2440. {
  2441. case EMarketMode::RESOURCE_RESOURCE:
  2442. bgName = "TPMRKRES.bmp";
  2443. break;
  2444. case EMarketMode::RESOURCE_PLAYER:
  2445. bgName = "TPMRKPTS.bmp";
  2446. break;
  2447. case EMarketMode::CREATURE_RESOURCE:
  2448. bgName = "TPMRKCRS.bmp";
  2449. break;
  2450. case EMarketMode::RESOURCE_ARTIFACT:
  2451. bgName = "TPMRKABS.bmp";
  2452. sliderNeeded = false;
  2453. break;
  2454. case EMarketMode::ARTIFACT_RESOURCE:
  2455. bgName = "TPMRKASS.bmp";
  2456. sliderNeeded = false;
  2457. break;
  2458. }
  2459. bg = new CPicture(bgName);
  2460. bg->colorizeAndConvert(LOCPLINT->playerID);
  2461. pos = bg->center();
  2462. new CGStatusBar(302, 576);
  2463. if(market->o->ID == 7) //black market
  2464. {
  2465. printAtMiddle(CGI->generaltexth->allTexts[349],300,27,FONT_BIG,Colors::Jasmine,*bg); //title
  2466. }
  2467. else if(market->o->ID == 99 || market->o->ID == 221) //trading post
  2468. {
  2469. printAtMiddle(CGI->generaltexth->allTexts[159],300,27,FONT_BIG,Colors::Jasmine,*bg); //title
  2470. }
  2471. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2472. {
  2473. if(market->o->ID == GameConstants::TOWNI_TYPE)
  2474. printAtMiddle(CGI->buildh->buildings[6][21]->Name(), 300, 27, FONT_BIG, Colors::Jasmine, *bg); //title
  2475. else
  2476. printAtMiddle(market->o->getHoverText(), 300, 27, FONT_BIG, Colors::Jasmine, *bg); //title
  2477. }
  2478. else if(mode == EMarketMode::RESOURCE_ARTIFACT || mode == EMarketMode::ARTIFACT_RESOURCE)
  2479. {
  2480. const std::string &title = market->o->ID == GameConstants::TOWNI_TYPE
  2481. ? CGI->buildh->buildings[market->o->subID][17]->Name()
  2482. : market->o->getHoverText();
  2483. printAtMiddle(title, 300, 27, FONT_BIG, Colors::Jasmine, *bg); //title
  2484. }
  2485. else
  2486. {
  2487. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,Colors::Jasmine,*bg); //marketplace
  2488. }
  2489. initItems(false);
  2490. initItems(true);
  2491. ok = new CAdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2492. ok->assignedKeys.insert(SDLK_ESCAPE);
  2493. deal = new CAdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2494. deal->block(true);
  2495. //slider and buttons must be created after bg
  2496. if(sliderNeeded)
  2497. {
  2498. slider = new CSlider(231,490,137,0,0,0);
  2499. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2500. max = new CAdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2501. max->block(true);
  2502. }
  2503. else
  2504. {
  2505. slider = NULL;
  2506. max = NULL;
  2507. deal->moveBy(Point(-30, 0));
  2508. }
  2509. Rect traderTextRect;
  2510. //left side
  2511. switch(Mode)
  2512. {
  2513. case EMarketMode::RESOURCE_RESOURCE:
  2514. case EMarketMode::RESOURCE_PLAYER:
  2515. case EMarketMode::RESOURCE_ARTIFACT:
  2516. printAtMiddle(CGI->generaltexth->allTexts[270],154,148,FONT_SMALL,Colors::Cornsilk,*bg); //kingdom res.
  2517. break;
  2518. case EMarketMode::CREATURE_RESOURCE:
  2519. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 152, 102, FONT_SMALL, Colors::Cornsilk, *bg); //%s's Creatures
  2520. break;
  2521. case EMarketMode::ARTIFACT_RESOURCE:
  2522. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->name), 152, 57, FONT_SMALL, Colors::Cornsilk, *bg); //%s's Artifacts
  2523. break;
  2524. }
  2525. //right side
  2526. switch(Mode)
  2527. {
  2528. case EMarketMode::RESOURCE_RESOURCE:
  2529. case EMarketMode::CREATURE_RESOURCE:
  2530. case EMarketMode::RESOURCE_ARTIFACT:
  2531. case EMarketMode::ARTIFACT_RESOURCE:
  2532. printAtMiddle(CGI->generaltexth->allTexts[168],445,148,FONT_SMALL,Colors::Cornsilk,*bg); //available for trade
  2533. traderTextRect = Rect(316, 48, 260, 75);
  2534. break;
  2535. case EMarketMode::RESOURCE_PLAYER:
  2536. printAtMiddle(CGI->generaltexth->allTexts[169],445,55,FONT_SMALL,Colors::Cornsilk,*bg); //players
  2537. traderTextRect = Rect(28, 48, 260, 75);
  2538. break;
  2539. }
  2540. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2541. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  2542. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  2543. new CAdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2544. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  2545. new CAdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  2546. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  2547. new CAdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  2548. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  2549. new CAdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  2550. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  2551. new CAdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"); //was y=450, changed to not overlap res-art in some conditions
  2552. updateTraderText();
  2553. }
  2554. CMarketplaceWindow::~CMarketplaceWindow()
  2555. {
  2556. hLeft = hRight = NULL;
  2557. for(int i=0;i<items[1].size();i++)
  2558. delChild(items[1][i]);
  2559. for(int i=0;i<items[0].size();i++)
  2560. delChild(items[0][i]);
  2561. items[1].clear();
  2562. items[0].clear();
  2563. delChild(bg);
  2564. bg = NULL;
  2565. }
  2566. void CMarketplaceWindow::setMax()
  2567. {
  2568. slider->moveToMax();
  2569. }
  2570. void CMarketplaceWindow::makeDeal()
  2571. {
  2572. int sliderValue = 0;
  2573. if(slider)
  2574. sliderValue = slider->value;
  2575. else
  2576. sliderValue = !deal->isBlocked(); //should always be 1
  2577. if(!sliderValue)
  2578. return;
  2579. int leftIdToSend = -1;
  2580. if(mode == EMarketMode::CREATURE_RESOURCE)
  2581. leftIdToSend = hLeft->serial;
  2582. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2583. leftIdToSend = hLeft->getArtInstance()->id;
  2584. else
  2585. leftIdToSend = hLeft->id;
  2586. if(slider)
  2587. {
  2588. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2589. slider->moveTo(0);
  2590. }
  2591. else
  2592. {
  2593. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2594. }
  2595. madeTransaction = true;
  2596. hLeft = NULL;
  2597. hRight = NULL;
  2598. selectionChanged(true);
  2599. }
  2600. void CMarketplaceWindow::sliderMoved( int to )
  2601. {
  2602. redraw();
  2603. }
  2604. void CMarketplaceWindow::selectionChanged(bool side)
  2605. {
  2606. readyToTrade = hLeft && hRight;
  2607. if(mode == EMarketMode::RESOURCE_RESOURCE)
  2608. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  2609. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  2610. arts->unmarkSlots(false);
  2611. if(readyToTrade)
  2612. {
  2613. int soldItemId = hLeft->id;
  2614. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  2615. if(slider)
  2616. {
  2617. int newAmount = -1;
  2618. if(itemsType[1] == RESOURCE)
  2619. newAmount = LOCPLINT->cb->getResourceAmount(soldItemId);
  2620. else if(itemsType[1] == CREATURE)
  2621. newAmount = hero->getStackCount(hLeft->serial) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2622. else
  2623. assert(0);
  2624. slider->setAmount(newAmount / r1);
  2625. slider->moveTo(0);
  2626. max->block(false);
  2627. deal->block(false);
  2628. }
  2629. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  2630. {
  2631. deal->block(LOCPLINT->cb->getResourceAmount(soldItemId) < r1);
  2632. }
  2633. else
  2634. deal->block(false);
  2635. }
  2636. else
  2637. {
  2638. if(slider)
  2639. {
  2640. max->block(true);
  2641. slider->setAmount(0);
  2642. slider->moveTo(0);
  2643. }
  2644. deal->block(true);
  2645. }
  2646. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2647. initSubs(false);
  2648. updateTraderText();
  2649. redraw();
  2650. }
  2651. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  2652. {
  2653. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  2654. }
  2655. void CMarketplaceWindow::garrisonChanged()
  2656. {
  2657. if(mode != EMarketMode::CREATURE_RESOURCE)
  2658. return;
  2659. std::set<CTradeableItem *> toRemove;
  2660. getEmptySlots(toRemove);
  2661. removeItems(toRemove);
  2662. initSubs(true);
  2663. }
  2664. void CMarketplaceWindow::artifactsChanged(bool Left)
  2665. {
  2666. assert(!Left);
  2667. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  2668. return;
  2669. std::vector<int> available = market->availableItemsIds(mode);
  2670. std::set<CTradeableItem *> toRemove;
  2671. BOOST_FOREACH(CTradeableItem *t, items[0])
  2672. if(!vstd::contains(available, t->id))
  2673. toRemove.insert(t);
  2674. removeItems(toRemove);
  2675. redraw();
  2676. }
  2677. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  2678. {
  2679. if(Left)
  2680. {
  2681. switch(itemsType[1])
  2682. {
  2683. case RESOURCE:
  2684. case CREATURE:
  2685. {
  2686. int val = slider
  2687. ? slider->value * r1
  2688. : (((deal->isBlocked())) ? 0 : r1);
  2689. return boost::lexical_cast<std::string>(val);
  2690. }
  2691. case ARTIFACT_INSTANCE:
  2692. return ((deal->isBlocked()) ? "0" : "1");
  2693. }
  2694. }
  2695. else
  2696. {
  2697. switch(itemsType[0])
  2698. {
  2699. case RESOURCE:
  2700. if(slider)
  2701. return boost::lexical_cast<std::string>( slider->value * r2 );
  2702. else
  2703. return boost::lexical_cast<std::string>(r2);
  2704. case ARTIFACT_TYPE:
  2705. return ((deal->isBlocked()) ? "0" : "1");
  2706. case PLAYER:
  2707. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  2708. }
  2709. }
  2710. return "???";
  2711. }
  2712. Point CMarketplaceWindow::selectionOffset(bool Left) const
  2713. {
  2714. if(Left)
  2715. {
  2716. switch(itemsType[1])
  2717. {
  2718. case RESOURCE:
  2719. return Point(122, 446);
  2720. case CREATURE:
  2721. return Point(128, 450);
  2722. case ARTIFACT_INSTANCE:
  2723. return Point(134, 466);
  2724. }
  2725. }
  2726. else
  2727. {
  2728. switch(itemsType[0])
  2729. {
  2730. case RESOURCE:
  2731. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2732. return Point(410, 469);
  2733. else
  2734. return Point(410, 446);
  2735. case ARTIFACT_TYPE:
  2736. return Point(425, 447);
  2737. case PLAYER:
  2738. return Point(417, 451);
  2739. }
  2740. }
  2741. assert(0);
  2742. return Point(0,0);
  2743. }
  2744. void CMarketplaceWindow::resourceChanged(int type, int val)
  2745. {
  2746. initSubs(true);
  2747. }
  2748. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2749. {
  2750. switch(type)
  2751. {
  2752. case RESOURCE:
  2753. dx = 82;
  2754. dy = 79;
  2755. x = 39;
  2756. y = 180;
  2757. h = 66;
  2758. w = 74;
  2759. break;
  2760. case PLAYER:
  2761. dx = 83;
  2762. dy = 118;
  2763. h = 64;
  2764. w = 58;
  2765. x = 44;
  2766. y = 83;
  2767. assert(Right);
  2768. break;
  2769. case CREATURE://45,123
  2770. x = 45;
  2771. y = 123;
  2772. w = 58;
  2773. h = 64;
  2774. dx = 83;
  2775. dy = 98;
  2776. assert(!Right);
  2777. break;
  2778. case ARTIFACT_TYPE://45,123
  2779. x = 340-289;
  2780. y = 180;
  2781. w = 44;
  2782. h = 44;
  2783. dx = 83;
  2784. dy = 79;
  2785. break;
  2786. }
  2787. leftToRightOffset = 289;
  2788. }
  2789. void CMarketplaceWindow::updateTraderText()
  2790. {
  2791. if(readyToTrade)
  2792. {
  2793. if(mode == EMarketMode::RESOURCE_PLAYER)
  2794. {
  2795. //I can give %s to the %s player.
  2796. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  2797. }
  2798. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  2799. {
  2800. //I can offer you the %s for %d %s of %s.
  2801. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2802. }
  2803. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  2804. {
  2805. //I can offer you %d %s of %s for %d %s of %s.
  2806. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2807. }
  2808. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2809. {
  2810. //I can offer you %d %s of %s for %d %s.
  2811. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  2812. }
  2813. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2814. {
  2815. //I can offer you %d %s of %s for your %s.
  2816. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  2817. }
  2818. return;
  2819. }
  2820. int gnrtxtnr = -1;
  2821. if(madeTransaction)
  2822. {
  2823. if(mode == EMarketMode::RESOURCE_PLAYER)
  2824. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  2825. else
  2826. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  2827. }
  2828. else
  2829. {
  2830. if(mode == EMarketMode::RESOURCE_PLAYER)
  2831. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  2832. else
  2833. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  2834. }
  2835. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  2836. }
  2837. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= NULL*/, EMarketMode::EMarketMode Mode)
  2838. :CTradeWindow(Market, Hero, Mode)
  2839. {
  2840. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2841. bg = new CPicture(Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp");
  2842. bg->colorizeAndConvert(LOCPLINT->playerID);
  2843. pos = bg->center();
  2844. if(Mode == EMarketMode::CREATURE_EXP)
  2845. {
  2846. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 155, 30, FONT_SMALL, Colors::Jasmine, *bg); //%s's Creatures
  2847. printAtMiddle(CGI->generaltexth->allTexts[479], 450, 30, FONT_SMALL, Colors::Jasmine, *bg); //Altar of Sacrifice
  2848. printAtMiddleWB(CGI->generaltexth->allTexts[480], 450, 70, FONT_SMALL, 45, Colors::Jasmine, *bg); //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  2849. slider = new CSlider(231,481,137,0,0,0);
  2850. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  2851. max = new CAdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  2852. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  2853. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  2854. sacrificeBackpack = NULL;
  2855. initItems(true);
  2856. mimicCres();
  2857. }
  2858. else
  2859. {
  2860. printAtMiddle(CGI->generaltexth->allTexts[477], 450, 34, FONT_SMALL, Colors::Jasmine, *bg); //Sacrifice artifacts for experience
  2861. printAtMiddle(CGI->generaltexth->allTexts[478], 302, 423, FONT_SMALL, Colors::Jasmine, *bg); //%s's Creatures
  2862. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[571],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  2863. sacrificeAll->block(!hero->artifactsInBackpack.size() && !hero->artifactsWorn.size());
  2864. sacrificeBackpack = new CAdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  2865. sacrificeBackpack->block(!hero->artifactsInBackpack.size());
  2866. slider = NULL;
  2867. max = NULL;
  2868. initItems(true);
  2869. initItems(false);
  2870. }
  2871. printAtMiddleWB(CGI->generaltexth->allTexts[475], 72, 437, FONT_SMALL, 17, Colors::Jasmine, *bg); //Experience needed to reach next level
  2872. printAtMiddleWB(CGI->generaltexth->allTexts[476], 72, 505, FONT_SMALL, 17, Colors::Jasmine, *bg); //Total experience on the Altar
  2873. new CGStatusBar(302, 576);
  2874. ok = new CAdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2875. ok->assignedKeys.insert(SDLK_ESCAPE);
  2876. deal = new CAdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  2877. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  2878. new CAdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  2879. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  2880. new CAdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  2881. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  2882. getExpValues();
  2883. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  2884. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  2885. setExpToLevel();
  2886. calcTotalExp();
  2887. blockTrade();
  2888. }
  2889. CAltarWindow::~CAltarWindow()
  2890. {
  2891. }
  2892. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2893. {
  2894. leftToRightOffset = 289;
  2895. x = 45;
  2896. y = 110;
  2897. w = 58;
  2898. h = 64;
  2899. dx = 83;
  2900. dy = 98;
  2901. }
  2902. void CAltarWindow::sliderMoved(int to)
  2903. {
  2904. sacrificedUnits[hLeft->serial] = to;
  2905. updateRight(hRight);
  2906. deal->block(!to);
  2907. calcTotalExp();
  2908. redraw();
  2909. }
  2910. void CAltarWindow::makeDeal()
  2911. {
  2912. if(mode == EMarketMode::CREATURE_EXP)
  2913. {
  2914. blockTrade();
  2915. slider->value = 0;
  2916. std::vector<int> toSacrifice = sacrificedUnits;
  2917. for (int i = 0; i < toSacrifice.size(); i++)
  2918. {
  2919. if(toSacrifice[i])
  2920. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  2921. }
  2922. BOOST_FOREACH(int& val, sacrificedUnits)
  2923. val = 0;
  2924. BOOST_FOREACH(CTradeableItem *t, items[0])
  2925. {
  2926. t->type = CREATURE_PLACEHOLDER;
  2927. t->subtitle = "";
  2928. }
  2929. }
  2930. else
  2931. {
  2932. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar) //sacrifice each artifact on the list
  2933. {
  2934. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  2935. }
  2936. arts->artifactsOnAltar.clear();
  2937. BOOST_FOREACH(CTradeableItem *t, items[0])
  2938. {
  2939. t->id = -1;
  2940. t->subtitle = "";
  2941. }
  2942. arts->commonInfo->reset();
  2943. //arts->scrollBackpack(0);
  2944. deal->block(true);
  2945. }
  2946. calcTotalExp();
  2947. }
  2948. void CAltarWindow::SacrificeAll()
  2949. {
  2950. if(mode == EMarketMode::CREATURE_EXP)
  2951. {
  2952. bool movedAnything = false;
  2953. BOOST_FOREACH(CTradeableItem *t, items[1])
  2954. sacrificedUnits[t->serial] = hero->getStackCount(t->serial);
  2955. sacrificedUnits[items[1].front()->serial]--;
  2956. BOOST_FOREACH(CTradeableItem *t, items[0])
  2957. {
  2958. updateRight(t);
  2959. if(t->type == CREATURE)
  2960. movedAnything = true;
  2961. }
  2962. deal->block(!movedAnything);
  2963. calcTotalExp();
  2964. }
  2965. else
  2966. {
  2967. for(std::map<ui16, ArtSlotInfo>::const_iterator i = hero->artifactsWorn.begin(); i != hero->artifactsWorn.end(); i++)
  2968. {
  2969. if(i->second.artifact->artType->id != 145) //ignore locks from assembled artifacts
  2970. moveFromSlotToAltar(i->first, NULL, i->second.artifact);
  2971. }
  2972. SacrificeBackpack();
  2973. }
  2974. redraw();
  2975. }
  2976. void CAltarWindow::selectionChanged(bool side)
  2977. {
  2978. if(mode != EMarketMode::CREATURE_EXP)
  2979. return;
  2980. CTradeableItem *&selected = side ? hLeft : hRight;
  2981. CTradeableItem *&theOther = side ? hRight : hLeft;
  2982. theOther = *std::find_if(items[!side].begin(), items[!side].end(), boost::bind(&CTradeableItem::serial, _1) == selected->serial);
  2983. int stackCount = 0;
  2984. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  2985. if(hero->getStackCount(i) > sacrificedUnits[i])
  2986. stackCount++;
  2987. slider->setAmount(hero->getStackCount(hLeft->serial) - (stackCount == 1));
  2988. slider->block(!slider->amount);
  2989. slider->value = sacrificedUnits[hLeft->serial];
  2990. max->block(!slider->amount);
  2991. readyToTrade = true;
  2992. redraw();
  2993. }
  2994. void CAltarWindow::mimicCres()
  2995. {
  2996. std::vector<Rect> positions;
  2997. getPositionsFor(positions, false, CREATURE);
  2998. BOOST_FOREACH(CTradeableItem *t, items[1])
  2999. {
  3000. CTradeableItem *hlp = new CTradeableItem(CREATURE_PLACEHOLDER, t->id, false, t->serial);
  3001. hlp->pos = positions[t->serial] + hlp->pos;
  3002. items[0].push_back(hlp);
  3003. }
  3004. }
  3005. Point CAltarWindow::selectionOffset(bool Left) const
  3006. {
  3007. if(Left)
  3008. return Point(150, 421);
  3009. else
  3010. return Point(396, 421);
  3011. }
  3012. std::string CAltarWindow::selectionSubtitle(bool Left) const
  3013. {
  3014. if(Left && slider && hLeft)
  3015. return boost::lexical_cast<std::string>(slider->value);
  3016. else if(!Left && hRight)
  3017. return hRight->subtitle;
  3018. else
  3019. return "";
  3020. }
  3021. void CAltarWindow::artifactsChanged(bool left)
  3022. {
  3023. }
  3024. void CAltarWindow::garrisonChanged()
  3025. {
  3026. if(mode != EMarketMode::CREATURE_EXP)
  3027. return;
  3028. std::set<CTradeableItem *> empty;
  3029. getEmptySlots(empty);
  3030. BOOST_FOREACH(CTradeableItem *t, empty)
  3031. {
  3032. removeItem(*std::find_if(items[0].begin(), items[0].end(), boost::bind(&CTradeableItem::serial, _1) == t->serial));
  3033. removeItem(t);
  3034. }
  3035. initSubs(true);
  3036. getExpValues();
  3037. }
  3038. void CAltarWindow::getExpValues()
  3039. {
  3040. int dump;
  3041. BOOST_FOREACH(CTradeableItem *t, items[1])
  3042. if(t->id >= 0)
  3043. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  3044. }
  3045. void CAltarWindow::calcTotalExp()
  3046. {
  3047. int val = 0;
  3048. if(mode == EMarketMode::CREATURE_EXP)
  3049. {
  3050. for (int i = 0; i < sacrificedUnits.size(); i++)
  3051. {
  3052. val += expPerUnit[i] * sacrificedUnits[i];
  3053. }
  3054. }
  3055. else
  3056. {
  3057. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar)
  3058. {
  3059. int dmp, valOfArt;
  3060. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  3061. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  3062. }
  3063. }
  3064. val *=(100+hero->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0;
  3065. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  3066. }
  3067. void CAltarWindow::setExpToLevel()
  3068. {
  3069. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  3070. }
  3071. void CAltarWindow::blockTrade()
  3072. {
  3073. hLeft = hRight = NULL;
  3074. readyToTrade = false;
  3075. if(slider)
  3076. {
  3077. slider->block(true);
  3078. max->block(true);
  3079. }
  3080. deal->block(true);
  3081. }
  3082. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  3083. {
  3084. int val = sacrificedUnits[toUpdate->serial];
  3085. toUpdate->type = val ? CREATURE : CREATURE_PLACEHOLDER;
  3086. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  3087. }
  3088. int CAltarWindow::firstFreeSlot()
  3089. {
  3090. int ret = -1;
  3091. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  3092. return ret < items[0].size() ? ret : -1;
  3093. }
  3094. void CAltarWindow::SacrificeBackpack()
  3095. {
  3096. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  3097. for (int i = 0; i < hero->artifactsInBackpack.size(); i++)
  3098. {
  3099. if(vstd::contains(toOmmit, hero->artifactsInBackpack[i].artifact))
  3100. {
  3101. toOmmit -= hero->artifactsInBackpack[i].artifact;
  3102. continue;
  3103. }
  3104. putOnAltar(NULL, hero->artifactsInBackpack[i].artifact);
  3105. }
  3106. arts->scrollBackpack(0);
  3107. calcTotalExp();
  3108. }
  3109. void CAltarWindow::artifactPicked()
  3110. {
  3111. redraw();
  3112. }
  3113. void CAltarWindow::showAll(SDL_Surface * to)
  3114. {
  3115. CTradeWindow::showAll(to);
  3116. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  3117. {
  3118. blitAtLoc(graphics->artDefs->ourImages[arts->commonInfo->src.art->artType->id].bitmap, 281, 442, to);
  3119. int dmp, val;
  3120. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  3121. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::Cornsilk, to);
  3122. }
  3123. }
  3124. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  3125. {
  3126. int artID = art->artType->id;
  3127. if(artID != 1 && artID < 7) //special art
  3128. {
  3129. tlog2 << "Cannot put special artifact on altar!\n";
  3130. return false;
  3131. }
  3132. if(!altarSlot)
  3133. {
  3134. int slotIndex = firstFreeSlot();
  3135. if(slotIndex < 0)
  3136. {
  3137. tlog2 << "No free slots on altar!\n";
  3138. return false;
  3139. }
  3140. altarSlot = items[0][slotIndex];
  3141. }
  3142. int dmp, val;
  3143. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  3144. arts->artifactsOnAltar.insert(art);
  3145. altarSlot->setArtInstance(art);
  3146. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  3147. deal->block(false);
  3148. return true;
  3149. }
  3150. void CAltarWindow::moveFromSlotToAltar(int slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  3151. {
  3152. int freeBackpackSlot = hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START;
  3153. if(arts->commonInfo->src.art)
  3154. {
  3155. arts->commonInfo->dst.slotID = freeBackpackSlot;
  3156. arts->commonInfo->dst.AOH = arts;
  3157. }
  3158. if(putOnAltar(altarSlot, art))
  3159. {
  3160. if(slotID < GameConstants::BACKPACK_START)
  3161. LOCPLINT->cb->swapArtifacts(hero, slotID, hero, freeBackpackSlot);
  3162. else
  3163. {
  3164. arts->commonInfo->src.clear();
  3165. arts->commonInfo->dst.clear();
  3166. CCS->curh->dragAndDropCursor(NULL);
  3167. arts->unmarkSlots(false);
  3168. }
  3169. }
  3170. }
  3171. void CSystemOptionsWindow::setMusicVolume( int newVolume )
  3172. {
  3173. Settings volume = settings.write["general"]["music"];
  3174. volume->Float() = newVolume;
  3175. }
  3176. void CSystemOptionsWindow::setSoundVolume( int newVolume )
  3177. {
  3178. Settings volume = settings.write["general"]["sound"];
  3179. volume->Float() = newVolume;
  3180. }
  3181. void CSystemOptionsWindow::setHeroMoveSpeed( int newSpeed )
  3182. {
  3183. Settings speed = settings.write["adventure"]["heroSpeed"];
  3184. speed->Float() = newSpeed;
  3185. }
  3186. void CSystemOptionsWindow::setMapScrollingSpeed( int newSpeed )
  3187. {
  3188. Settings speed = settings.write["adventure"]["scrollSpeed"];
  3189. speed->Float() = newSpeed;
  3190. }
  3191. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &Pos, CPlayerInterface * owner)
  3192. {
  3193. //TODO: translation and\or config file
  3194. static const std::string fsLabel = "Fullscreen";
  3195. static const std::string fsHelp = "{Fullscreen}\n\n If selected, VCMI will run in fullscreen mode, othervice VCMI will run in window";
  3196. static const std::string cwLabel = "Classic creature window";
  3197. static const std::string cwHelp = "{Classic creature window}\n\n Enable original Heroes 3 creature window instead of new window from VCMI";
  3198. static const std::string rsLabel = "Select resolution";
  3199. static const std::string rsHelp = "{Select resolution}\n\n Change in-game screen resolution. Will only affect adventure map. Game restart required to apply new resolution.";
  3200. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3201. pos = Pos;
  3202. bg = new CPicture("SysOpBck");
  3203. bg->colorize(LOCPLINT->playerID);
  3204. //window title
  3205. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[568]);
  3206. //left window section
  3207. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::Jasmine);
  3208. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  3209. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  3210. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  3211. leftGroup->add(122, 262, rsLabel); //CGI->generaltexth->allTexts[20]
  3212. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  3213. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  3214. //right section
  3215. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::Cornsilk);
  3216. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  3217. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  3218. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  3219. rightGroup->add(282, 153, CGI->generaltexth->allTexts[575]);
  3220. rightGroup->add(282, 185, cwLabel); //CGI->generaltexth->allTexts[576]
  3221. rightGroup->add(282, 217, fsLabel); //CGI->generaltexth->allTexts[577]
  3222. //setting up buttons
  3223. // load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::loadf, this), pos.x+246, pos.y+298, "SOLOAD.DEF", SDLK_l);
  3224. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second,
  3225. boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  3226. save->swappedImages = true;
  3227. save->update();
  3228. // restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), pos.x+346, pos.y+357, "SORSTRT", SDLK_r);
  3229. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second,
  3230. boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  3231. mainMenu->swappedImages = true;
  3232. mainMenu->update();
  3233. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second,
  3234. boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  3235. quitGame->swappedImages = true;
  3236. quitGame->update();
  3237. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second,
  3238. boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  3239. backToMap->swappedImages = true;
  3240. backToMap->update();
  3241. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3242. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3243. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  3244. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  3245. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  3246. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  3247. heroMoveSpeed->select(settings["adventure"]["heroSpeed"].Float(), 1);
  3248. heroMoveSpeed->onChange = boost::bind(&CSystemOptionsWindow::setHeroMoveSpeed, this, _1);
  3249. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3250. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  3251. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  3252. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  3253. mapScrollSpeed->select(settings["adventure"]["scrollSpeed"].Float(), 1);
  3254. mapScrollSpeed->onChange = boost::bind(&CSystemOptionsWindow::setMapScrollingSpeed, this, _1);
  3255. musicVolume = new CHighlightableButtonsGroup(0, true);
  3256. for(int i=0; i<10; ++i)
  3257. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  3258. musicVolume->select(CCS->musich->getVolume(), 1);
  3259. musicVolume->onChange = boost::bind(&CSystemOptionsWindow::setMusicVolume, this, _1);
  3260. effectsVolume = new CHighlightableButtonsGroup(0, true);
  3261. for(int i=0; i<10; ++i)
  3262. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  3263. effectsVolume->select(CCS->soundh->getVolume(), 1);
  3264. effectsVolume->onChange = boost::bind(&CSystemOptionsWindow::setSoundVolume, this, _1);
  3265. showReminder = new CHighlightableButton(
  3266. boost::bind(&CSystemOptionsWindow::toggleReminder, this, true), boost::bind(&CSystemOptionsWindow::toggleReminder, this, false),
  3267. std::map<int,std::string>(), CGI->generaltexth->zelp[361].second, false, "sysopchk.def", NULL, 246, 87, false);
  3268. newCreatureWin = new CHighlightableButton(
  3269. boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, true), boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, false),
  3270. std::map<int,std::string>(), cwHelp, false, "sysopchk.def", NULL, 246, 183, false);
  3271. fullscreen = new CHighlightableButton(
  3272. boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, true), boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, false),
  3273. std::map<int,std::string>(), fsHelp, false, "sysopchk.def", NULL, 246, 215, false);
  3274. showReminder->select(settings["adventure"]["heroReminder"].Bool());
  3275. newCreatureWin->select(settings["general"]["classicCreatureWindow"].Bool());
  3276. fullscreen->select(settings["video"]["fullscreen"].Bool());
  3277. gameResButton = new CAdventureMapButton("", rsHelp, boost::bind(&CSystemOptionsWindow::selectGameRes, this, false), 28, 275,"SYSOB12", SDLK_g);
  3278. }
  3279. void CSystemOptionsWindow::selectGameRes(bool pregame)
  3280. {
  3281. assert(pregame == false);//TODO
  3282. //TODO: translation and\or config file
  3283. static const std::string rsLabel = "Select resolution";
  3284. static const std::string rsHelp = "Change in-game screen resolution.";
  3285. std::vector<std::string> items;
  3286. BOOST_FOREACH( config::CConfigHandler::GuiOptionsMap::value_type& value, conf.guiOptions)
  3287. {
  3288. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  3289. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  3290. items.push_back(resX + 'x' + resY);
  3291. }
  3292. GH.pushInt(new CObjectListWindow(items, NULL, rsLabel, rsHelp,
  3293. boost::bind(&CSystemOptionsWindow::setGameRes, this, pregame, _1)));
  3294. }
  3295. void CSystemOptionsWindow::setGameRes(bool pregame, int index)
  3296. {
  3297. config::CConfigHandler::GuiOptionsMap::const_iterator iter = conf.guiOptions.begin();
  3298. while (index--)
  3299. iter++;
  3300. Settings gameRes = settings.write["video"]["gameRes"];
  3301. gameRes["width"].Float() = iter->first.first;
  3302. gameRes["height"].Float() = iter->first.second;
  3303. }
  3304. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  3305. {
  3306. GH.popIntTotally(this);
  3307. SDL_Event event;
  3308. event.type = type;
  3309. event.user.code = usercode; // not necessarily used
  3310. SDL_PushEvent(&event);
  3311. }
  3312. void CSystemOptionsWindow::toggleReminder(bool on)
  3313. {
  3314. Settings heroReminder = settings.write["adventure"]["heroReminder"];
  3315. heroReminder->Bool() = on;
  3316. }
  3317. void CSystemOptionsWindow::toggleCreatureWin(bool on)
  3318. {
  3319. Settings classicCreatureWindow = settings.write["general"]["classicCreatureWindow"];
  3320. classicCreatureWindow->Bool() = on;
  3321. }
  3322. void CSystemOptionsWindow::toggleFullscreen(bool on)
  3323. {
  3324. Settings fullscreen = settings.write["video"]["fullscreen"];
  3325. fullscreen->Bool() = on;
  3326. }
  3327. void CSystemOptionsWindow::bquitf()
  3328. {
  3329. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<CComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  3330. }
  3331. void CSystemOptionsWindow::breturnf()
  3332. {
  3333. GH.popIntTotally(this);
  3334. }
  3335. void CSystemOptionsWindow::bmainmenuf()
  3336. {
  3337. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<CComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, 2), 0, false);
  3338. }
  3339. void CSystemOptionsWindow::bsavef()
  3340. {
  3341. GH.popIntTotally(this);
  3342. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  3343. }
  3344. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj)
  3345. : tavernObj(TavernObj)
  3346. {
  3347. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3348. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3349. assert(h.size() == 2);
  3350. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3351. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3352. if(h[0])
  3353. selected = 0;
  3354. else
  3355. selected = -1;
  3356. oldSelected = -1;
  3357. bg = new CPicture("TPTAVERN.bmp");
  3358. bg->colorizeAndConvert(LOCPLINT->playerID);
  3359. pos = center(bg->pos);
  3360. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,FONT_BIG,Colors::Jasmine,*bg);
  3361. printAtMiddle("2500",320,328,FONT_SMALL,Colors::Cornsilk,*bg);
  3362. // printAtMiddle(CGI->generaltexth->jktexts[38],146,283,FONT_BIG,Colors::Jasmine,bg); //what is this???
  3363. printAtMiddleWB(LOCPLINT->cb->getTavernGossip(tavernObj), 200, 220, FONT_SMALL, 50, Colors::Cornsilk, *bg);
  3364. bar = new CGStatusBar(8, 478, "APHLFTRT.bmp", 380);
  3365. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3366. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3367. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3368. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  3369. {
  3370. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3371. recruit->block(2);
  3372. }
  3373. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3374. {
  3375. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3376. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3377. recruit->block(2);
  3378. }
  3379. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3380. {
  3381. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3382. recruit->block(2);
  3383. }
  3384. else
  3385. {
  3386. if(!h[0])
  3387. recruit->block(1);
  3388. }
  3389. CCS->videoh->open(VIDEO_TAVERN);
  3390. }
  3391. void CTavernWindow::recruitb()
  3392. {
  3393. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3394. const CGObjectInstance *obj = tavernObj;
  3395. close();
  3396. LOCPLINT->cb->recruitHero(obj, toBuy);
  3397. }
  3398. void CTavernWindow::thievesguildb()
  3399. {
  3400. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3401. }
  3402. CTavernWindow::~CTavernWindow()
  3403. {
  3404. CCS->videoh->close();
  3405. }
  3406. void CTavernWindow::close()
  3407. {
  3408. GH.popIntTotally(this);
  3409. }
  3410. void CTavernWindow::show(SDL_Surface * to)
  3411. {
  3412. CIntObject::show(to);
  3413. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3414. if(selected >= 0)
  3415. {
  3416. HeroPortrait *sel = selected ? h2 : h1;
  3417. if (selected != oldSelected && !recruit->isBlocked())
  3418. {
  3419. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3420. oldSelected = selected;
  3421. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3422. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3423. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3424. }
  3425. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,Colors::Cornsilk,to);
  3426. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3427. }
  3428. }
  3429. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3430. {
  3431. if(previousState && !down && h)
  3432. *_sel = _id;
  3433. }
  3434. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3435. {
  3436. if(down && h)
  3437. {
  3438. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3439. }
  3440. }
  3441. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3442. : h(H), _sel(&sel), _id(id)
  3443. {
  3444. used = LCLICK | RCLICK | HOVER;
  3445. h = H;
  3446. pos.x = x;
  3447. pos.y = y;
  3448. pos.w = 58;
  3449. pos.h = 64;
  3450. if(H)
  3451. {
  3452. hoverName = CGI->generaltexth->tavernInfo[4];
  3453. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3454. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3455. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3456. if(vstd::contains(h->artifactsWorn,i))
  3457. artifs--;
  3458. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3459. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3460. descr[sizeof(descr)-1] = '\0';
  3461. }
  3462. }
  3463. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  3464. {
  3465. blitAt(graphics->portraitLarge[h->subID],pos,to);
  3466. }
  3467. void CTavernWindow::HeroPortrait::hover( bool on )
  3468. {
  3469. //Hoverable::hover(on);
  3470. if(on)
  3471. GH.statusbar->print(hoverName);
  3472. else
  3473. GH.statusbar->clear();
  3474. }
  3475. void CInGameConsole::activate()
  3476. {
  3477. activateKeys();
  3478. }
  3479. void CInGameConsole::deactivate()
  3480. {
  3481. deactivateKeys();
  3482. }
  3483. void CInGameConsole::show(SDL_Surface * to)
  3484. {
  3485. int number = 0;
  3486. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3487. texts_mx.lock();
  3488. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3489. {
  3490. SDL_Color green = {0,0xff,0,0};
  3491. Point leftBottomCorner(0, screen->h);
  3492. if(LOCPLINT->battleInt)
  3493. {
  3494. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3495. }
  3496. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3497. if(SDL_GetTicks() - it->second > defaultTimeout)
  3498. {
  3499. toDel.push_back(it);
  3500. }
  3501. }
  3502. for(int it=0; it<toDel.size(); ++it)
  3503. {
  3504. texts.erase(toDel[it]);
  3505. }
  3506. texts_mx.unlock();
  3507. }
  3508. void CInGameConsole::print(const std::string &txt)
  3509. {
  3510. texts_mx.lock();
  3511. int lineLen = conf.go()->ac.outputLineLength;
  3512. if(txt.size() < lineLen)
  3513. {
  3514. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3515. if(texts.size() > maxDisplayedTexts)
  3516. {
  3517. texts.pop_front();
  3518. }
  3519. }
  3520. else
  3521. {
  3522. assert(lineLen);
  3523. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3524. {
  3525. std::string part = txt.substr(g * lineLen, lineLen);
  3526. if(part.size() == 0)
  3527. break;
  3528. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3529. if(texts.size() > maxDisplayedTexts)
  3530. {
  3531. texts.pop_front();
  3532. }
  3533. }
  3534. }
  3535. texts_mx.unlock();
  3536. }
  3537. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3538. {
  3539. if(key.type != SDL_KEYDOWN) return;
  3540. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3541. switch(key.keysym.sym)
  3542. {
  3543. case SDLK_TAB:
  3544. case SDLK_ESCAPE:
  3545. {
  3546. if(captureAllKeys)
  3547. {
  3548. captureAllKeys = false;
  3549. endEnteringText(false);
  3550. }
  3551. else if(SDLK_TAB)
  3552. {
  3553. captureAllKeys = true;
  3554. startEnteringText();
  3555. }
  3556. break;
  3557. }
  3558. case SDLK_RETURN: //enter key
  3559. {
  3560. if(enteredText.size() > 0 && captureAllKeys)
  3561. {
  3562. captureAllKeys = false;
  3563. endEnteringText(true);
  3564. }
  3565. break;
  3566. }
  3567. case SDLK_BACKSPACE:
  3568. {
  3569. if(enteredText.size() > 1)
  3570. {
  3571. enteredText.resize(enteredText.size()-1);
  3572. enteredText[enteredText.size()-1] = '_';
  3573. refreshEnteredText();
  3574. }
  3575. break;
  3576. }
  3577. case SDLK_UP: //up arrow
  3578. {
  3579. if(previouslyEntered.size() == 0)
  3580. break;
  3581. if(prevEntDisp == -1)
  3582. {
  3583. prevEntDisp = previouslyEntered.size() - 1;
  3584. enteredText = previouslyEntered[prevEntDisp] + "_";
  3585. refreshEnteredText();
  3586. }
  3587. else if( prevEntDisp > 0)
  3588. {
  3589. --prevEntDisp;
  3590. enteredText = previouslyEntered[prevEntDisp] + "_";
  3591. refreshEnteredText();
  3592. }
  3593. break;
  3594. }
  3595. case SDLK_DOWN: //down arrow
  3596. {
  3597. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3598. {
  3599. ++prevEntDisp;
  3600. enteredText = previouslyEntered[prevEntDisp] + "_";
  3601. refreshEnteredText();
  3602. }
  3603. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3604. {
  3605. prevEntDisp = -1;
  3606. enteredText = "_";
  3607. refreshEnteredText();
  3608. }
  3609. break;
  3610. }
  3611. default:
  3612. {
  3613. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3614. {
  3615. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3616. {
  3617. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3618. enteredText += "_";
  3619. refreshEnteredText();
  3620. }
  3621. }
  3622. break;
  3623. }
  3624. }
  3625. }
  3626. void CInGameConsole::startEnteringText()
  3627. {
  3628. enteredText = "_";
  3629. if(GH.topInt() == adventureInt)
  3630. {
  3631. GH.statusbar->print(enteredText);
  3632. }
  3633. else if(LOCPLINT->battleInt)
  3634. {
  3635. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3636. }
  3637. }
  3638. void CInGameConsole::endEnteringText(bool printEnteredText)
  3639. {
  3640. prevEntDisp = -1;
  3641. if(printEnteredText)
  3642. {
  3643. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3644. LOCPLINT->cb->sendMessage(txt);
  3645. previouslyEntered.push_back(txt);
  3646. print(txt);
  3647. }
  3648. enteredText = "";
  3649. if(GH.topInt() == adventureInt)
  3650. {
  3651. GH.statusbar->clear();
  3652. }
  3653. else if(LOCPLINT->battleInt)
  3654. {
  3655. LOCPLINT->battleInt->console->ingcAlter = "";
  3656. }
  3657. }
  3658. void CInGameConsole::refreshEnteredText()
  3659. {
  3660. if(GH.topInt() == adventureInt)
  3661. {
  3662. GH.statusbar->print(enteredText);
  3663. }
  3664. else if(LOCPLINT->battleInt)
  3665. {
  3666. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3667. }
  3668. }
  3669. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3670. {
  3671. }
  3672. void CGarrisonWindow::close()
  3673. {
  3674. GH.popIntTotally(this);
  3675. }
  3676. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3677. {
  3678. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3679. bg = new CPicture("GARRISON.bmp");
  3680. bg->colorizeAndConvert(LOCPLINT->playerID);
  3681. pos = bg->center();
  3682. garr = new CGarrisonInt(92, 127, 4, Point(0,96), bg->bg, Point(93,127), up, down, removableUnits);
  3683. {
  3684. CAdventureMapButton *split = new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3685. removeChild(split);
  3686. garr->addSplitBtn(split);
  3687. }
  3688. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3689. std::string titleText;
  3690. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  3691. titleText = CGI->generaltexth->allTexts[709];
  3692. else
  3693. {
  3694. titleText = CGI->generaltexth->allTexts[35];
  3695. boost::algorithm::replace_first(titleText, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  3696. }
  3697. title = new CLabel(275, 30, FONT_BIG, CENTER, Colors::Jasmine, titleText);
  3698. }
  3699. CGarrisonWindow::~CGarrisonWindow()
  3700. {
  3701. }
  3702. void CGarrisonWindow::showAll(SDL_Surface * to)
  3703. {
  3704. CIntObject::showAll(to);
  3705. blitAtLoc(graphics->flags->ourImages[garr->armedObjs[1]->getOwner()].bitmap,28,124,to);
  3706. blitAtLoc(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait],29,222,to);
  3707. }
  3708. IShowActivatable::IShowActivatable()
  3709. {
  3710. type = 0;
  3711. }
  3712. CGarrisonHolder::CGarrisonHolder()
  3713. {
  3714. type |= WITH_GARRISON;
  3715. }
  3716. void CWindowWithGarrison::updateGarrisons()
  3717. {
  3718. garr->recreateSlots();
  3719. }
  3720. CArtPlace::CArtPlace(const CArtifactInstance* Art)
  3721. :picked(false), marked(false), locked(false), ourArt(Art)
  3722. {
  3723. }
  3724. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  3725. picked(false), marked(false), locked(false), ourArt(Art)
  3726. {
  3727. pos += position;
  3728. pos.w = pos.h = 44;
  3729. }
  3730. void CArtPlace::activate()
  3731. {
  3732. if(!active)
  3733. {
  3734. LRClickableAreaWTextComp::activate();
  3735. }
  3736. }
  3737. void CArtPlace::clickLeft(tribool down, bool previousState)
  3738. {
  3739. //LRClickableAreaWTextComp::clickLeft(down);
  3740. bool inBackpack = slotID >= GameConstants::BACKPACK_START,
  3741. srcInBackpack = ourOwner->commonInfo->src.slotID >= GameConstants::BACKPACK_START,
  3742. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  3743. if(ourOwner->highlightModeCallback && ourArt)
  3744. {
  3745. if(down)
  3746. {
  3747. if(ourArt->artType->id < 7) //War Machine or Spellbook
  3748. {
  3749. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  3750. }
  3751. else
  3752. {
  3753. ourOwner->unmarkSlots(false);
  3754. marked = true;
  3755. ourOwner->highlightModeCallback(this);
  3756. }
  3757. }
  3758. return;
  3759. }
  3760. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3761. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  3762. {
  3763. if(ourArt->artType->id == 0)
  3764. {
  3765. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  3766. GH.pushInt(spellWindow);
  3767. }
  3768. }
  3769. if (!down && previousState)
  3770. {
  3771. if(ourArt && ourArt->artType->id == 0) //spellbook
  3772. return; //this is handled separately
  3773. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  3774. {
  3775. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3776. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  3777. {
  3778. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  3779. {
  3780. std::vector<CComponent *> catapult(1, new CComponent(CComponent::artifact, 3, 0));
  3781. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3782. return;
  3783. }
  3784. select();
  3785. }
  3786. }
  3787. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  3788. {
  3789. deselect();
  3790. }
  3791. else //perform artifact transition
  3792. {
  3793. if(inBackpack) // Backpack destination.
  3794. {
  3795. if(srcInBackpack && slotID == ourOwner->commonInfo->src.slotID + 1) //next slot (our is not visible, so visually same as "old" place) to the art -> make nothing, return artifact to slot
  3796. {
  3797. deselect();
  3798. }
  3799. else
  3800. {
  3801. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  3802. switch(cur->id)
  3803. {
  3804. case 3:
  3805. //should not happen, catapult cannot be selected
  3806. assert(cur->id != 3);
  3807. break;
  3808. case 4: case 5: case 6: //war machines cannot go to backpack
  3809. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  3810. break;
  3811. default:
  3812. setMeAsDest();
  3813. vstd::amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  3814. if(srcInBackpack && srcInSameHero)
  3815. {
  3816. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with vstd::amin above it will guarantee that dest is at most the last artifact
  3817. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  3818. ourOwner->commonInfo->dst.slotID--;
  3819. }
  3820. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  3821. deselect();
  3822. else
  3823. ourOwner->realizeCurrentTransaction();
  3824. break;
  3825. }
  3826. }
  3827. }
  3828. //check if swap is possible
  3829. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  3830. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  3831. {
  3832. setMeAsDest();
  3833. //
  3834. // // Special case when the dest artifact can't be fit into the src slot.
  3835. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  3836. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  3837. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  3838. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  3839. // {
  3840. // // Put dest artifact into owner's backpack.
  3841. // ourOwner->commonInfo->src.AOH = ourOwner;
  3842. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  3843. // }
  3844. ourOwner->realizeCurrentTransaction();
  3845. }
  3846. }
  3847. }
  3848. }
  3849. void CArtPlace::clickRight(tribool down, bool previousState)
  3850. {
  3851. if(down && ourArt && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  3852. {
  3853. if (slotID < 19)
  3854. {
  3855. if(ourOwner->allowedAssembling)
  3856. {
  3857. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  3858. // If the artifact can be assembled, display dialog.
  3859. BOOST_FOREACH(const CArtifact *combination, assemblyPossibilities)
  3860. {
  3861. LOCPLINT->showArtifactAssemblyDialog(
  3862. ourArt->artType->id,
  3863. combination->id,
  3864. true,
  3865. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, true, combination->id),
  3866. 0);
  3867. if(assemblyPossibilities.size() > 2)
  3868. {
  3869. tlog3 << "More than one possibility of assembling... taking only first\n";
  3870. break;
  3871. }
  3872. return;
  3873. }
  3874. // Otherwise if the artifact can be diasassembled, display dialog.
  3875. if(ourArt->canBeDisassembled())
  3876. {
  3877. LOCPLINT->showArtifactAssemblyDialog(
  3878. ourArt->artType->id,
  3879. 0,
  3880. false,
  3881. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, false, 0),
  3882. 0);
  3883. return;
  3884. }
  3885. }
  3886. }
  3887. // Lastly just show the artifact description.
  3888. LRClickableAreaWTextComp::clickRight(down, previousState);
  3889. }
  3890. }
  3891. /**
  3892. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3893. */
  3894. void CArtPlace::select ()
  3895. {
  3896. if (locked)
  3897. return;
  3898. picked = true;
  3899. if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear
  3900. {
  3901. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3902. {
  3903. CArtPlace *ap = ourOwner->getArtPlace(i);
  3904. ap->picked = ourArt->isPart(ap->ourArt);
  3905. }
  3906. }
  3907. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  3908. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->artType->id].bitmap);
  3909. ourOwner->commonInfo->src.setTo(this, false);
  3910. ourOwner->markPossibleSlots(ourArt);
  3911. if(slotID >= GameConstants::BACKPACK_START)
  3912. ourOwner->scrollBackpack(0); //will update slots
  3913. ourOwner->updateParentWindow();
  3914. ourOwner->safeRedraw();
  3915. }
  3916. /**
  3917. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  3918. */
  3919. void CArtPlace::deselect ()
  3920. {
  3921. picked = false;
  3922. if(ourArt && ourArt->canBeDisassembled()) //combined art returned to its slot -> restore locks
  3923. {
  3924. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3925. ourOwner->getArtPlace(i)->picked = false;
  3926. }
  3927. CCS->curh->dragAndDropCursor(NULL);
  3928. ourOwner->unmarkSlots();
  3929. ourOwner->commonInfo->src.clear();
  3930. if(slotID >= GameConstants::BACKPACK_START)
  3931. ourOwner->scrollBackpack(0); //will update slots
  3932. ourOwner->updateParentWindow();
  3933. ourOwner->safeRedraw();
  3934. }
  3935. void CArtPlace::deactivate()
  3936. {
  3937. if(active)
  3938. {
  3939. LRClickableAreaWTextComp::deactivate();
  3940. }
  3941. }
  3942. void CArtPlace::showAll(SDL_Surface * to)
  3943. {
  3944. if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
  3945. {
  3946. int graphic = locked ? 145 : ourArt->artType->id;
  3947. blitAt(graphics->artDefs->ourImages[graphic].bitmap, pos.x, pos.y, to);
  3948. }
  3949. if(marked && active)
  3950. {
  3951. // Draw vertical bars.
  3952. for (int i = 0; i < pos.h; ++i)
  3953. {
  3954. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3955. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3956. }
  3957. // Draw horizontal bars.
  3958. for (int i = 0; i < pos.w; ++i)
  3959. {
  3960. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3961. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3962. }
  3963. }
  3964. }
  3965. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  3966. {
  3967. // You can place 'no artifact' anywhere.
  3968. if(!art)
  3969. return true;
  3970. // Anything can but War Machines can be placed in backpack.
  3971. if (slotID >= 19)
  3972. return !CGI->arth->isBigArtifact(art->id);
  3973. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  3974. }
  3975. CArtPlace::~CArtPlace()
  3976. {
  3977. deactivate();
  3978. }
  3979. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  3980. {
  3981. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  3982. }
  3983. void CArtPlace::setArtifact(const CArtifactInstance *art)
  3984. {
  3985. baseType = -1; //by default we don't store any component
  3986. ourArt = art;
  3987. if(!art)
  3988. {
  3989. text = std::string();
  3990. hoverText = CGI->generaltexth->allTexts[507];
  3991. }
  3992. else
  3993. {
  3994. text = ourArt->artType->Description();
  3995. if(art->artType->id == 1) //spell scroll
  3996. {
  3997. // we expect scroll description to be like this: This scroll contains the [spell name] spell which is added into your spell book for as long as you carry the scroll.
  3998. // so we want to replace text in [...] with a spell name
  3999. // however other language versions don't have name placeholder at all, so we have to be careful
  4000. int spellID = art->getGivenSpellID();
  4001. int nameStart = text.find_first_of('[');
  4002. int nameEnd = text.find_first_of(']', nameStart);
  4003. if(spellID >= 0)
  4004. {
  4005. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  4006. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  4007. //add spell component info (used to provide a pic in r-click popup)
  4008. baseType = CComponent::spell;
  4009. type = spellID;
  4010. bonusValue = 0;
  4011. }
  4012. }
  4013. if (locked) // Locks should appear as empty.
  4014. hoverText = CGI->generaltexth->allTexts[507];
  4015. else
  4016. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  4017. }
  4018. }
  4019. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  4020. {
  4021. if((!down) && previousState)
  4022. {
  4023. std::vector<CComponent*> comp(1, createComponent());
  4024. LOCPLINT->showInfoDialog(text, comp);
  4025. }
  4026. }
  4027. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  4028. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  4029. {
  4030. }
  4031. CComponent * LRClickableAreaWTextComp::createComponent() const
  4032. {
  4033. if(baseType >= 0)
  4034. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  4035. else
  4036. return NULL;
  4037. }
  4038. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  4039. {
  4040. if(down)
  4041. {
  4042. if(CComponent *comp = createComponent())
  4043. {
  4044. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  4045. return;
  4046. }
  4047. }
  4048. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  4049. }
  4050. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  4051. {
  4052. used = LCLICK | RCLICK | HOVER;
  4053. pos.x += x; pos.w = 58;
  4054. pos.y += y; pos.h = 64;
  4055. }
  4056. void CHeroArea::clickLeft(tribool down, bool previousState)
  4057. {
  4058. if((!down) && previousState && hero)
  4059. LOCPLINT->openHeroWindow(hero);
  4060. }
  4061. void CHeroArea::clickRight(tribool down, bool previousState)
  4062. {
  4063. if((!down) && previousState && hero)
  4064. LOCPLINT->openHeroWindow(hero);
  4065. }
  4066. void CHeroArea::hover(bool on)
  4067. {
  4068. if (on && hero)
  4069. GH.statusbar->print(hero->hoverName);
  4070. else
  4071. GH.statusbar->clear();
  4072. }
  4073. void CHeroArea::showAll(SDL_Surface * to)
  4074. {
  4075. if (hero)
  4076. blitAtLoc(graphics->portraitLarge[hero->portrait],0,0,to);
  4077. }
  4078. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  4079. {
  4080. if((!down) && previousState && town)
  4081. {
  4082. LOCPLINT->openTownWindow(town);
  4083. if ( type == 2 )
  4084. LOCPLINT->castleInt->builds->buildingClicked(10);
  4085. else if ( type == 3 && town->fortLevel() )
  4086. LOCPLINT->castleInt->builds->buildingClicked(7);
  4087. }
  4088. }
  4089. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  4090. {
  4091. if((!down) && previousState && town)
  4092. LOCPLINT->openTownWindow(town);//TODO: popup?
  4093. }
  4094. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  4095. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  4096. {
  4097. }
  4098. void CArtifactsOfHero::SCommonPart::reset()
  4099. {
  4100. src.clear();
  4101. dst.clear();
  4102. CCS->curh->dragAndDropCursor(NULL);
  4103. }
  4104. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  4105. {
  4106. // // An update is made, rather than initialization.
  4107. // if (curHero && curHero->id == hero->id)
  4108. // {
  4109. // if(curHero != hero)
  4110. // {
  4111. // //delete curHero;
  4112. // curHero = hero; //was: creating a copy
  4113. // }
  4114. //
  4115. // // Compensate backpack pos if an artifact was insertad before it.
  4116. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  4117. // && commonInfo->dst.slotID - 19 < backpackPos)
  4118. // {
  4119. // backpackPos++;
  4120. // }
  4121. //
  4122. // if (updateState && commonInfo->srcAOH == this)
  4123. // {
  4124. // // A swap was made, make the replaced artifact the current selected.
  4125. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  4126. // {
  4127. // // // Temporarily remove artifact from hero.
  4128. // // if (commonInfo->srcSlotID < 19)
  4129. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  4130. // // else
  4131. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  4132. //
  4133. // updateParentWindow(); //TODO: evil! but does the thing
  4134. //
  4135. // // Source <- Dest
  4136. // commonInfo->srcArtifact = commonInfo->destArtifact;
  4137. //
  4138. // // Reset destination parameters.
  4139. // commonInfo->dst.clear();
  4140. //
  4141. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  4142. // markPossibleSlots(commonInfo->srcArtifact);
  4143. // }
  4144. // else if (commonInfo->destAOH != NULL)
  4145. // {
  4146. // // Reset all parameters.
  4147. // commonInfo->reset();
  4148. // unmarkSlots();
  4149. // }
  4150. // }
  4151. // }
  4152. // else
  4153. // {
  4154. // commonInfo->reset();
  4155. // }
  4156. //
  4157. // if(hero != curHero)
  4158. // {
  4159. // // delete curHero;
  4160. // // curHero = new CGHeroInstance(*hero);
  4161. // curHero = hero; //was: creating a copy
  4162. // }
  4163. curHero = hero;
  4164. if (curHero->artifactsInBackpack.size() > 0)
  4165. backpackPos %= curHero->artifactsInBackpack.size();
  4166. else
  4167. backpackPos = 0;
  4168. // Fill the slots for worn artifacts and backpack.
  4169. for (int g = 0; g < artWorn.size() ; g++)
  4170. setSlotData(artWorn[g], g);
  4171. scrollBackpack(0);
  4172. }
  4173. void CArtifactsOfHero::dispose()
  4174. {
  4175. //vstd::clear_pointer(curHero);
  4176. //unmarkSlots(false);
  4177. CCS->curh->dragAndDropCursor(NULL);
  4178. }
  4179. void CArtifactsOfHero::scrollBackpack(int dir)
  4180. {
  4181. int artsInBackpack = curHero->artifactsInBackpack.size();
  4182. backpackPos += dir;
  4183. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  4184. backpackPos += artsInBackpack;
  4185. if(artsInBackpack)
  4186. backpackPos %= artsInBackpack;
  4187. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  4188. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  4189. toOmit.insert(commonInfo->src.art);
  4190. int omitedSoFar = 0;
  4191. //set new data
  4192. size_t s = 0;
  4193. for( ; s < artsInBackpack; ++s)
  4194. {
  4195. if (s < artsInBackpack)
  4196. {
  4197. int slotID = 19 + (s + backpackPos)%artsInBackpack;
  4198. const CArtifactInstance *art = curHero->getArt(slotID);
  4199. assert(art);
  4200. if(!vstd::contains(toOmit, art))
  4201. {
  4202. if(s - omitedSoFar < backpack.size())
  4203. setSlotData(backpack[s-omitedSoFar], slotID);
  4204. }
  4205. else
  4206. {
  4207. toOmit -= art;
  4208. omitedSoFar++;
  4209. continue;
  4210. }
  4211. }
  4212. }
  4213. for( ; s - omitedSoFar < backpack.size(); s++)
  4214. eraseSlotData(backpack[s-omitedSoFar], 19 + s);
  4215. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  4216. if(highlightModeCallback)
  4217. {
  4218. for(int i = 0; i < backpack.size(); i++)
  4219. {
  4220. if(backpack[i]->marked)
  4221. {
  4222. highlightModeCallback(backpack[i]);
  4223. break;
  4224. }
  4225. }
  4226. }
  4227. //blocking scrolling if there is not enough artifacts to scroll
  4228. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  4229. leftArtRoll->block(!scrollingPossible);
  4230. rightArtRoll->block(!scrollingPossible);
  4231. safeRedraw();
  4232. }
  4233. /**
  4234. * Marks possible slots where a given artifact can be placed, except backpack.
  4235. *
  4236. * @param art Artifact checked against.
  4237. */
  4238. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  4239. {
  4240. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4241. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  4242. place->marked = art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true);
  4243. safeRedraw();
  4244. }
  4245. /**
  4246. * Unamarks all slots.
  4247. */
  4248. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  4249. {
  4250. if(commonInfo)
  4251. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4252. aoh->unmarkLocalSlots(false);
  4253. else
  4254. unmarkLocalSlots(false);\
  4255. if(withRedraw)
  4256. safeRedraw();
  4257. }
  4258. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw /*= true*/)
  4259. {
  4260. BOOST_FOREACH(CArtPlace *place, artWorn)
  4261. place->marked = false;
  4262. BOOST_FOREACH(CArtPlace *place, backpack)
  4263. place->marked = false;
  4264. if(withRedraw)
  4265. safeRedraw();
  4266. }
  4267. /**
  4268. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  4269. */
  4270. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, int slotID)
  4271. {
  4272. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  4273. {
  4274. return;
  4275. }
  4276. artPlace->picked = false;
  4277. artPlace->slotID = slotID;
  4278. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  4279. {
  4280. artPlace->setArtifact(asi->artifact);
  4281. artPlace->locked = asi->locked;
  4282. }
  4283. else
  4284. artPlace->setArtifact(NULL);
  4285. }
  4286. /**
  4287. * Makes given artifact slot appear as empty with a certain slot ID.
  4288. */
  4289. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, int slotID)
  4290. {
  4291. artPlace->picked = false;
  4292. artPlace->slotID = slotID;
  4293. artPlace->setArtifact(NULL);
  4294. }
  4295. CArtifactsOfHero::CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  4296. CAdventureMapButton *leftScroll, CAdventureMapButton *rightScroll, bool createCommonPart):
  4297. curHero(NULL),
  4298. artWorn(ArtWorn), backpack(Backpack),
  4299. backpackPos(0), commonInfo(NULL), updateState(false),
  4300. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  4301. allowedAssembling(true), highlightModeCallback(0)
  4302. {
  4303. if(createCommonPart)
  4304. {
  4305. commonInfo = new CArtifactsOfHero::SCommonPart;
  4306. commonInfo->participants.insert(this);
  4307. }
  4308. // Init slots for worn artifacts.
  4309. for (size_t g = 0; g < artWorn.size() ; g++)
  4310. {
  4311. artWorn[g]->ourOwner = this;
  4312. eraseSlotData(artWorn[g], g);
  4313. }
  4314. // Init slots for the backpack.
  4315. for(size_t s=0; s<backpack.size(); ++s)
  4316. {
  4317. backpack[s]->ourOwner = this;
  4318. eraseSlotData(backpack[s], 19 + s);
  4319. }
  4320. leftArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1);
  4321. rightArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1);
  4322. }
  4323. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  4324. : curHero(NULL), backpackPos(0), commonInfo(NULL), updateState(false), allowedAssembling(true), highlightModeCallback(0)
  4325. {
  4326. if(createCommonPart)
  4327. {
  4328. commonInfo = new CArtifactsOfHero::SCommonPart;
  4329. commonInfo->participants.insert(this);
  4330. }
  4331. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4332. pos += position;
  4333. artWorn.resize(19);
  4334. std::vector<Rect> slotPos;
  4335. slotPos += genRect(44,44,509,30), genRect(44,44,567,240), genRect(44,44,509,80),
  4336. genRect(44,44,383,68), genRect(44,44,564,183), genRect(44,44,509,130),
  4337. genRect(44,44,431,68), genRect(44,44,610,183), genRect(44,44,515,295),
  4338. genRect(44,44,383,143), genRect(44,44,399,194), genRect(44,44,415,245),
  4339. genRect(44,44,431,296), genRect(44,44,564,30), genRect(44,44,610,30),
  4340. genRect(44,44,610,76), genRect(44,44,610,122), genRect(44,44,610,310),
  4341. genRect(44,44,381,296);
  4342. // Create slots for worn artifacts.
  4343. for (size_t g = 0; g < 19 ; g++)
  4344. {
  4345. artWorn[g] = new CArtPlace(NULL);
  4346. artWorn[g]->pos = slotPos[g] + pos;
  4347. artWorn[g]->ourOwner = this;
  4348. eraseSlotData(artWorn[g], g);
  4349. }
  4350. // Create slots for the backpack.
  4351. for(size_t s=0; s<5; ++s)
  4352. {
  4353. CArtPlace * add = new CArtPlace(NULL);
  4354. add->ourOwner = this;
  4355. add->pos.x = pos.x + 403 + 46*s;
  4356. add->pos.y = pos.y + 365;
  4357. add->pos.h = add->pos.w = 44;
  4358. eraseSlotData(add, 19 + s);
  4359. backpack.push_back(add);
  4360. }
  4361. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  4362. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  4363. }
  4364. CArtifactsOfHero::~CArtifactsOfHero()
  4365. {
  4366. dispose();
  4367. }
  4368. void CArtifactsOfHero::updateParentWindow()
  4369. {
  4370. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  4371. {
  4372. if(updateState)
  4373. chw->curHero = curHero;
  4374. else
  4375. chw->update(curHero, true);
  4376. }
  4377. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  4378. {
  4379. //use our copy of hero to draw window
  4380. if(cew->heroInst[0]->id == curHero->id)
  4381. cew->heroInst[0] = curHero;
  4382. else
  4383. cew->heroInst[1] = curHero;
  4384. if(!updateState)
  4385. {
  4386. cew->deactivate();
  4387. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  4388. // {
  4389. // if(cew->heroInst[g] == curHero)
  4390. // {
  4391. // cew->artifs[g]->setHero(curHero);
  4392. // }
  4393. // }
  4394. cew->prepareBackground();
  4395. cew->redraw();
  4396. cew->activate();
  4397. }
  4398. }
  4399. }
  4400. void CArtifactsOfHero::safeRedraw()
  4401. {
  4402. if (active)
  4403. {
  4404. if(parent)
  4405. parent->redraw();
  4406. else
  4407. redraw();
  4408. }
  4409. }
  4410. void CArtifactsOfHero::realizeCurrentTransaction()
  4411. {
  4412. assert(commonInfo->src.AOH);
  4413. assert(commonInfo->dst.AOH);
  4414. LOCPLINT->cb->swapArtifacts(commonInfo->src.AOH->curHero, commonInfo->src.slotID,
  4415. commonInfo->dst.AOH->curHero, commonInfo->dst.slotID);
  4416. }
  4417. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4418. {
  4419. if(src.hero == curHero && src.slot >= GameConstants::BACKPACK_START)
  4420. updateSlot(src.slot);
  4421. if(dst.hero == curHero && dst.slot >= GameConstants::BACKPACK_START)
  4422. updateSlot(dst.slot);
  4423. if(src.hero == curHero || dst.hero == curHero) //we need to update all slots, artifact might be combined and affect more slots
  4424. updateWornSlots(false);
  4425. if (src.hero != curHero && dst.hero != curHero)
  4426. return;
  4427. if(commonInfo->src == src) //artifact was taken from us
  4428. {
  4429. assert(commonInfo->dst == dst || dst.slot == dst.hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  4430. commonInfo->reset();
  4431. unmarkSlots();
  4432. }
  4433. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4434. {
  4435. assert(dst.slot >= GameConstants::BACKPACK_START);
  4436. commonInfo->reset();
  4437. CArtPlace *ap = NULL;
  4438. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4439. {
  4440. if(aoh->curHero == dst.hero)
  4441. {
  4442. commonInfo->src.AOH = aoh;
  4443. if((ap = aoh->getArtPlace(dst.slot)))
  4444. break;
  4445. }
  4446. }
  4447. if(ap)
  4448. {
  4449. ap->select();
  4450. }
  4451. else
  4452. {
  4453. commonInfo->src.art = dst.getArt();
  4454. commonInfo->src.slotID = dst.slot;
  4455. assert(commonInfo->src.AOH);
  4456. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[dst.getArt()->artType->id].bitmap);
  4457. markPossibleSlots(dst.getArt());
  4458. }
  4459. }
  4460. else if(src.slot >= GameConstants::BACKPACK_START &&
  4461. src.slot < commonInfo->src.slotID &&
  4462. src.hero == commonInfo->src.AOH->curHero) //artifact taken from before currently picked one
  4463. {
  4464. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4465. commonInfo->src.slotID--;
  4466. assert(commonInfo->src.valid());
  4467. }
  4468. else
  4469. {
  4470. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  4471. // however after first movement we pick the art from backpack and the second movement coming when
  4472. // we have a different artifact may look surprising... but it's valid.
  4473. //tlog1 << "Unexpected artifact movement...\n";
  4474. }
  4475. updateParentWindow();
  4476. int shift = 0;
  4477. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4478. // shift++;
  4479. //
  4480. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  4481. shift++;
  4482. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  4483. shift--;
  4484. if( (src.hero == curHero && src.slot >= GameConstants::BACKPACK_START)
  4485. || (dst.hero == curHero && dst.slot >= GameConstants::BACKPACK_START) )
  4486. scrollBackpack(shift); //update backpack slots
  4487. }
  4488. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  4489. {
  4490. if(al.hero == curHero)
  4491. {
  4492. if(al.slot < GameConstants::BACKPACK_START)
  4493. updateWornSlots(0);
  4494. else
  4495. scrollBackpack(0); //update backpack slots
  4496. }
  4497. }
  4498. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4499. {
  4500. if(slot < GameConstants::BACKPACK_START)
  4501. {
  4502. return artWorn[slot];
  4503. }
  4504. else
  4505. {
  4506. BOOST_FOREACH(CArtPlace *ap, backpack)
  4507. if(ap->slotID == slot)
  4508. return ap;
  4509. }
  4510. return NULL;
  4511. }
  4512. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4513. {
  4514. if(al.hero == curHero)
  4515. updateWornSlots();
  4516. }
  4517. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4518. {
  4519. if(al.hero == curHero)
  4520. updateWornSlots();
  4521. }
  4522. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4523. {
  4524. for(int i = 0; i < artWorn.size(); i++)
  4525. updateSlot(i);
  4526. if(redrawParent)
  4527. updateParentWindow();
  4528. }
  4529. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4530. {
  4531. return curHero;
  4532. }
  4533. void CArtifactsOfHero::updateSlot(int slotID)
  4534. {
  4535. setSlotData(getArtPlace(slotID), slotID);
  4536. }
  4537. void CExchangeWindow::close()
  4538. {
  4539. GH.popIntTotally(this);
  4540. }
  4541. void CExchangeWindow::showAll(SDL_Surface * to)
  4542. {
  4543. blitAt(bg, pos, to);
  4544. CIntObject::showAll(to);
  4545. //printing border around window
  4546. if(screen->w != 800 || screen->h !=600)
  4547. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4548. }
  4549. void CExchangeWindow::questlog(int whichHero)
  4550. {
  4551. CCS->curh->dragAndDropCursor(NULL);
  4552. }
  4553. void CExchangeWindow::prepareBackground()
  4554. {
  4555. if(bg)
  4556. SDL_FreeSurface(bg);
  4557. SDL_Surface * bgtemp; //loaded as 8bpp surface
  4558. bgtemp = BitmapHandler::loadBitmap("TRADE2.BMP");
  4559. graphics->blueToPlayersAdv(bgtemp, heroInst[0]->tempOwner);
  4560. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  4561. SDL_FreeSurface(bgtemp);
  4562. //printing heroes' names and levels
  4563. std::ostringstream os, os2;
  4564. os<<heroInst[0]->name<<", Level "<<heroInst[0]->level<<" "<<heroInst[0]->type->heroClass->name;
  4565. CSDL_Ext::printAtMiddle(os.str(), 147, 25, FONT_SMALL, Colors::Cornsilk, bg);
  4566. os2<<heroInst[1]->name<<", Level "<<heroInst[1]->level<<" "<<heroInst[1]->type->heroClass->name;
  4567. CSDL_Ext::printAtMiddle(os2.str(), 653, 25, FONT_SMALL, Colors::Cornsilk, bg);
  4568. //printing primary skills
  4569. CDefHandler * skilldef = CDefHandler::giveDef("PSKIL32.DEF");
  4570. for(int g=0; g<4; ++g)
  4571. {
  4572. //graphics
  4573. blitAt(skilldef->ourImages[g].bitmap, genRect(32, 32, 385, 19 + 36 * g), bg);
  4574. }
  4575. CDefHandler * un32 = CDefHandler::giveDef("UN32.DEF");
  4576. //heroes related thing
  4577. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4578. {
  4579. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  4580. //printing primary skills' amounts
  4581. for(int m=0; m<4; ++m)
  4582. {
  4583. std::ostringstream primarySkill;
  4584. primarySkill << heroWArt.getPrimSkillLevel(m);
  4585. CSDL_Ext::printAtMiddle(primarySkill.str(), 352 + 93 * b, 35 + 36 * m, FONT_SMALL, Colors::Cornsilk, bg);
  4586. }
  4587. //printing secondary skills
  4588. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4589. {
  4590. blitAt(graphics->abils32->ourImages[heroInst[b]->secSkills[m].first * 3 + heroInst[b]->secSkills[m].second + 2].bitmap, genRect(32, 32, 32 + 36 * m + 454 * b, 88), bg);
  4591. }
  4592. //hero's specialty
  4593. blitAt(un32->ourImages[heroInst[b]->subID].bitmap, 67 + 490*b, 45, bg);
  4594. //experience
  4595. blitAt(skilldef->ourImages[4].bitmap, 103 + 490*b, 45, bg);
  4596. printAtMiddle( makeNumberShort(heroInst[b]->exp), 119 + 490*b, 71, FONT_SMALL, Colors::Cornsilk, bg );
  4597. //mana points
  4598. blitAt(skilldef->ourImages[5].bitmap, 139 + 490*b, 45, bg);
  4599. printAtMiddle( makeNumberShort(heroInst[b]->mana), 155 + 490*b, 71, FONT_SMALL, Colors::Cornsilk, bg );
  4600. //setting morale
  4601. blitAt(graphics->morale30->ourImages[heroWArt.MoraleVal()+3].bitmap, 177 + 490*b, 45, bg);
  4602. //setting luck
  4603. blitAt(graphics->luck30->ourImages[heroWArt.LuckVal()+3].bitmap, 213 + 490*b, 45, bg);
  4604. }
  4605. //printing portraits
  4606. blitAt(graphics->portraitLarge[heroInst[0]->portrait], 257, 13, bg);
  4607. blitAt(graphics->portraitLarge[heroInst[1]->portrait], 485, 13, bg);
  4608. delete un32;
  4609. delete skilldef;
  4610. }
  4611. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) : bg(NULL)
  4612. {
  4613. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4614. char bufor[400];
  4615. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  4616. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  4617. prepareBackground();
  4618. pos.x = screen->w/2 - bg->w/2;
  4619. pos.y = screen->h/2 - bg->h/2;
  4620. pos.w = screen->w;
  4621. pos.h = screen->h;
  4622. artifs[0] = new CArtifactsOfHero(Point(pos.x + -334, pos.y + 150));
  4623. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4624. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4625. artifs[0]->setHero(heroInst[0]);
  4626. artifs[1] = new CArtifactsOfHero(Point(pos.x + 96, pos.y + 150));
  4627. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4628. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4629. artifs[1]->setHero(heroInst[1]);
  4630. artSets.push_back(artifs[0]);
  4631. artSets.push_back(artifs[1]);
  4632. //primary skills
  4633. for(int g=0; g<4; ++g)
  4634. {
  4635. //primary skill's clickable areas
  4636. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4637. primSkillAreas[g]->pos = genRect(32, 140, pos.x+329, pos.y + 19 + 36 * g);
  4638. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4639. primSkillAreas[g]->type = g;
  4640. primSkillAreas[g]->bonusValue = -1;
  4641. primSkillAreas[g]->baseType = 0;
  4642. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4643. primSkillAreas[g]->hoverText = std::string(bufor);
  4644. }
  4645. //heroes related thing
  4646. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4647. {
  4648. //secondary skill's clickable areas
  4649. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4650. {
  4651. int skill = heroInst[b]->secSkills[g].first,
  4652. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4653. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4654. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4655. secSkillAreas[b][g]->baseType = 1;
  4656. secSkillAreas[b][g]->type = skill;
  4657. secSkillAreas[b][g]->bonusValue = level;
  4658. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4659. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4660. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4661. }
  4662. portrait[b] = new CHeroArea(pos.x + 257 + 228*b, pos.y + 13, heroInst[b]);
  4663. speciality[b] = new LRClickableAreaWText();
  4664. speciality[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4665. speciality[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4666. speciality[b]->text = CGI->generaltexth->hTxts[heroInst[b]->subID].longBonus;
  4667. experience[b] = new LRClickableAreaWText();
  4668. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4669. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4670. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4671. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4672. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4673. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4674. spellPoints[b] = new LRClickableAreaWText();
  4675. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4676. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4677. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4678. spellPoints[b]->text = std::string(bufor);
  4679. //setting morale
  4680. morale[b] = new MoraleLuckBox(true, genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45));
  4681. morale[b]->set(heroInst[b]);
  4682. //setting luck
  4683. luck[b] = new MoraleLuckBox(false, genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45));
  4684. luck[b]->set(heroInst[b]);
  4685. }
  4686. //buttons
  4687. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  4688. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  4689. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  4690. //statusbar
  4691. //FIXME - this image is a bit bigger than required - part of background should be used instead
  4692. ourBar = new CGStatusBar(pos.x + 3, pos.y + 577, "KSTATBAR");
  4693. //garrison interface
  4694. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  4695. {
  4696. BLOCK_CAPTURING;
  4697. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  4698. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  4699. }
  4700. }
  4701. CExchangeWindow::~CExchangeWindow() //d-tor
  4702. {
  4703. SDL_FreeSurface(bg);
  4704. delete artifs[0]->commonInfo;
  4705. artifs[0]->commonInfo = NULL;
  4706. artifs[1]->commonInfo = NULL;
  4707. }
  4708. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy)
  4709. {
  4710. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4711. bg = new CPicture("TPSHIP");
  4712. bg->colorize(LOCPLINT->playerID);
  4713. pos = bg->center();
  4714. bgWater = new CPicture("TPSHIPBK", 100, 69);
  4715. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  4716. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  4717. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  4718. bgShip->center(waterCenter);
  4719. // Create resource icons and costs.
  4720. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  4721. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  4722. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::Cornsilk, goldValue);
  4723. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::Cornsilk, woodValue);
  4724. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  4725. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  4726. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 224, 312, "ICANCEL", SDLK_RETURN);
  4727. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 42, 312, "IBUY30", SDLK_RETURN);
  4728. build->callback += onBuy;
  4729. for(size_t i = 0; i < cost.size(); i++)
  4730. {
  4731. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4732. {
  4733. build->block(true);
  4734. break;
  4735. }
  4736. }
  4737. statusBar = new CGStatusBar(bg->pos.w/2, bg->pos.h-16);
  4738. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->jktexts[13]);
  4739. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::Cornsilk, CGI->generaltexth->jktexts[14]);
  4740. }
  4741. CPuzzleWindow::CPuzzleWindow(const int3 &grailPos, double discoveredRatio)
  4742. :animCount(0)
  4743. {
  4744. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  4745. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  4746. //make transparency black
  4747. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  4748. //the rest
  4749. background = SDL_ConvertSurface(back, screen->format, back->flags);
  4750. SDL_FreeSurface(back);
  4751. pos = genRect(background->h, background->w, (screen->w - background->w) / 2, (screen->h - background->h) / 2);
  4752. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  4753. quitb->assignedKeys.insert(SDLK_ESCAPE);
  4754. quitb->borderColor = Colors::MetallicGold;
  4755. quitb->borderEnabled = true;
  4756. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  4757. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  4758. //printing necessary things to background
  4759. int3 moveInt = int3(8, 9, 0);
  4760. Rect mapRect = genRect(544, 591, 8, 8);
  4761. CGI->mh->terrainRect
  4762. (grailPos - moveInt, adventureInt->anim,
  4763. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  4764. background, &mapRect, 0, 0, true, moveInt);
  4765. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  4766. std::vector<SPuzzleInfo> puzzlesToPrint;
  4767. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  4768. {
  4769. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered > PUZZLES_PER_FACTION * discoveredRatio)
  4770. {
  4771. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  4772. }
  4773. else
  4774. {
  4775. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  4776. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  4777. }
  4778. }
  4779. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  4780. {
  4781. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  4782. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  4783. SDL_FreeSurface(puzzle);
  4784. }
  4785. CCS->soundh->playSound(soundBase::OBELISK);
  4786. }
  4787. CPuzzleWindow::~CPuzzleWindow()
  4788. {
  4789. delete quitb;
  4790. delete resdatabar;
  4791. SDL_FreeSurface(background);
  4792. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  4793. SDL_FreeSurface( puzzlesToPullBack[g].first );
  4794. }
  4795. void CPuzzleWindow::activate()
  4796. {
  4797. quitb->activate();
  4798. }
  4799. void CPuzzleWindow::deactivate()
  4800. {
  4801. quitb->deactivate();
  4802. }
  4803. void CPuzzleWindow::show(SDL_Surface * to)
  4804. {
  4805. blitAt(background, pos.x, pos.y, to);
  4806. quitb->showAll(to);
  4807. resdatabar->draw(to);
  4808. //blitting disappearing puzzles
  4809. for(int g=0; g<2; ++g)
  4810. if(animCount != 255)
  4811. ++animCount;
  4812. if(animCount != 255)
  4813. {
  4814. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  4815. {
  4816. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  4817. yPos = puzzlesToPullBack[b].second->y + pos.y;
  4818. SDL_Surface *from = puzzlesToPullBack[b].first;
  4819. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  4820. blitAt(from, xPos, yPos, to);
  4821. }
  4822. }
  4823. //disappearing puzzles blitted
  4824. //printing border around window
  4825. if(screen->w != 800 || screen->h !=600)
  4826. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4827. }
  4828. void CTransformerWindow::CItem::showAll(SDL_Surface * to)
  4829. {
  4830. SDL_Surface * backgr = graphics->bigImgs[parent->army->getCreature(id)->idNumber];
  4831. blitAt(backgr, pos.x, pos.y, to);
  4832. printAtMiddle(boost::lexical_cast<std::string>(size),pos.x+28, pos.y+76,FONT_SMALL,Colors::Cornsilk,to);//stack size
  4833. }
  4834. void CTransformerWindow::CItem::move()
  4835. {
  4836. if (left)
  4837. pos.x += 289;
  4838. else
  4839. pos.x -= 289;
  4840. left = !left;
  4841. }
  4842. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  4843. {
  4844. if(previousState && (!down))
  4845. {
  4846. move();
  4847. parent->showAll(screen2);
  4848. }
  4849. }
  4850. CTransformerWindow::CItem::CItem(CTransformerWindow * _parent, int _size, int _id):
  4851. id(_id), size(_size), parent(_parent)
  4852. {
  4853. used = LCLICK;
  4854. left = true;
  4855. pos.w = 58;
  4856. pos.h = 64;
  4857. pos.x += 45 + (id%3)*83 + id/6*83;
  4858. pos.y += 109 + (id/3)*98;
  4859. }
  4860. CTransformerWindow::CItem::~CItem()
  4861. {
  4862. }
  4863. void CTransformerWindow::showAll(SDL_Surface * to)
  4864. {
  4865. CIntObject::showAll(to);
  4866. printAtMiddleLoc( CGI->generaltexth->allTexts[485], 153, 29,FONT_SMALL, Colors::Jasmine,to);//holding area
  4867. printAtMiddleLoc( CGI->generaltexth->allTexts[486], 153+295, 29,FONT_SMALL, Colors::Jasmine,to);//transformer
  4868. printAtMiddleWBLoc(CGI->generaltexth->allTexts[487], 153, 75,FONT_MEDIUM, 32,Colors::Jasmine,to);//move creatures to create skeletons
  4869. printAtMiddleWBLoc(CGI->generaltexth->allTexts[488], 153+295, 75,FONT_MEDIUM, 32,Colors::Jasmine,to);//creatures here will become skeletons
  4870. }
  4871. void CTransformerWindow::makeDeal()
  4872. {
  4873. for (int i=0; i<items.size(); i++)
  4874. if (!items[i]->left)
  4875. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, items[i]->id, 0, 0, hero);
  4876. }
  4877. void CTransformerWindow::addAll()
  4878. {
  4879. for (int i=0; i<items.size(); i++)
  4880. if (items[i]->left)
  4881. items[i]->move();
  4882. showAll(screen2);
  4883. }
  4884. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):hero(_hero),town(_town)
  4885. {
  4886. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4887. bg = new CPicture ("SKTRNBK.PCX");
  4888. bg->colorizeAndConvert(LOCPLINT->playerID);
  4889. pos = center(bg->pos);
  4890. if (hero)
  4891. army = hero;
  4892. else
  4893. army = town;
  4894. for (int i=0; i<7; i++ )
  4895. if ( army->getCreature(i) )
  4896. items.push_back(new CItem(this, army->getStackCount(i), i));
  4897. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  4898. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  4899. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CGuiHandler::popIntTotally,&GH, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  4900. bar = new CGStatusBar(304, 469);
  4901. }
  4902. CTransformerWindow::~CTransformerWindow()
  4903. {
  4904. }
  4905. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  4906. {
  4907. if(previousState && (!down))
  4908. {
  4909. if ( state() != 2 )
  4910. return;
  4911. CUnivConfirmWindow *win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(6) >= 2000);
  4912. GH.pushInt(win);
  4913. }
  4914. }
  4915. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  4916. {
  4917. if(down)
  4918. {
  4919. CInfoPopup *message = new CInfoPopup();
  4920. message->free = true;
  4921. message->bitmap = CMessage::drawBoxTextBitmapSub
  4922. (LOCPLINT->playerID,
  4923. CGI->generaltexth->skillInfoTexts[ID][0],
  4924. graphics->abils82->ourImages[ID*3+3].bitmap,
  4925. CGI->generaltexth->skillName[ID]);
  4926. message->pos.x = screen->w/2 - message->bitmap->w/2;
  4927. message->pos.y = screen->h/2 - message->bitmap->h/2;
  4928. GH.pushInt(message);
  4929. }
  4930. }
  4931. void CUniversityWindow::CItem::hover(bool on)
  4932. {
  4933. if (on)
  4934. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  4935. else
  4936. GH.statusbar->clear();
  4937. }
  4938. int CUniversityWindow::CItem::state()
  4939. {
  4940. if (parent->hero->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ID)))//hero know this skill
  4941. return 1;
  4942. if (parent->hero->secSkills.size() >= GameConstants::SKILL_PER_HERO)//can't learn more skills
  4943. return 0;
  4944. if (parent->hero->type->heroClass->proSec[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  4945. return 0;
  4946. /* if (LOCPLINT->cb->getResourceAmount(6) < 2000 )//no gold - allowed in H3, confirm button is blocked instead
  4947. return 0;*/
  4948. return 2;
  4949. }
  4950. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  4951. {
  4952. CPicture * bar;
  4953. switch (state())
  4954. {
  4955. case 0: bar = parent->red;
  4956. break;
  4957. case 1: bar = parent->yellow;
  4958. break;
  4959. case 2: bar = parent->green;
  4960. break;
  4961. default:bar = NULL;
  4962. break;
  4963. }
  4964. assert(bar);
  4965. blitAtLoc(bar->bg, -28, -22, to);
  4966. blitAtLoc(bar->bg, -28, 48, to);
  4967. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::Cornsilk,to);//Name
  4968. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::Cornsilk,to);//Level(always basic)
  4969. CPicture::showAll(to);
  4970. }
  4971. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  4972. CPicture (graphics->abils44->ourImages[_ID*3+3].bitmap,X,Y,false),ID(_ID), parent(_parent)
  4973. {
  4974. used = LCLICK | RCLICK | HOVER;
  4975. }
  4976. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):hero(_hero), market(_market)
  4977. {
  4978. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4979. bg = new CPicture("UNIVERS1.PCX");
  4980. bg->colorizeAndConvert(LOCPLINT->playerID);
  4981. green = new CPicture("UNIVGREN.PCX");
  4982. yellow = new CPicture("UNIVGOLD.PCX");//bars
  4983. red = new CPicture("UNIVRED.PCX");
  4984. green->recActions =
  4985. yellow->recActions =
  4986. red->recActions = DISPOSE;
  4987. if ( market->o->ID == 104 ) // this is adventure map university
  4988. {
  4989. SDL_Surface * titleImage = BitmapHandler::loadBitmap("UNIVBLDG.PCX");
  4990. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  4991. SDL_FreeSurface(titleImage);
  4992. }
  4993. else if (LOCPLINT->castleInt && LOCPLINT->castleInt->town->subID == 8)// this is town university
  4994. {
  4995. SDL_Surface * titleImage = LOCPLINT->castleInt->bicons->ourImages[21].bitmap;
  4996. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  4997. }
  4998. else
  4999. tlog0<<"Error: Image for university was not found!\n";//This should not happen
  5000. printAtMiddleWBLoc(CGI->generaltexth->allTexts[603], 232, 153, FONT_SMALL, 70,Colors::Cornsilk,bg->bg);//Clerk speech
  5001. printAtMiddleLoc (CGI->generaltexth->allTexts[602], 231, 26 , FONT_MEDIUM ,Colors::Jasmine,bg->bg);//University
  5002. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  5003. if (list.size()!=4)
  5004. tlog0<<"\t\tIncorrect size of available items vector!\n";
  5005. for (int i=0; i<list.size(); i++)//prepare clickable items
  5006. items.push_back(new CItem(this, list[i], pos.x+54+i*104, pos.y+234));
  5007. pos = center(bg->pos);
  5008. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  5009. boost::bind(&CGuiHandler::popIntTotally,&GH, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  5010. bar = new CGStatusBar(232, 371);
  5011. }
  5012. CUniversityWindow::~CUniversityWindow()
  5013. {
  5014. }
  5015. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):parent(PARENT)
  5016. {
  5017. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5018. bg = new CPicture("UNIVERS2.PCX");
  5019. bg->colorizeAndConvert(LOCPLINT->playerID);
  5020. std::string text = CGI->generaltexth->allTexts[608];
  5021. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  5022. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  5023. boost::replace_first(text, "%d", "2000");
  5024. printAtMiddleWBLoc(text, 230, 163, FONT_SMALL, 65,Colors::Cornsilk,bg->bg);//Clerk speech
  5025. printAtMiddleLoc (CGI->generaltexth-> skillName[SKILL], 230, 37, FONT_SMALL, Colors::Cornsilk,bg->bg);//Skill name
  5026. printAtMiddleLoc (CGI->generaltexth->levels[1], 230, 107, FONT_SMALL, Colors::Cornsilk,bg->bg);//Skill level
  5027. printAtMiddleLoc ("2000", 230, 267, FONT_SMALL, Colors::Cornsilk,bg->bg);//Cost
  5028. blitAtLoc(graphics->abils44->ourImages[SKILL*3+3].bitmap, 211, 51, bg->bg);//skill
  5029. blitAtLoc(graphics->resources32->ourImages[6].bitmap, 210, 210, bg->bg);//gold
  5030. pos = center(bg->pos);
  5031. std::string hoverText = CGI->generaltexth->allTexts[609];
  5032. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  5033. text = CGI->generaltexth->zelp[633].second;
  5034. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  5035. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  5036. boost::replace_first(text, "%d", "2000");
  5037. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  5038. 148,299,"IBY6432.DEF",SDLK_RETURN);
  5039. confirm->block(!available);
  5040. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CGuiHandler::popIntTotally, &GH, this),
  5041. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  5042. bar = new CGStatusBar(232, 371);
  5043. }
  5044. void CUnivConfirmWindow::makeDeal(int skill)
  5045. {
  5046. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  5047. GH.popIntTotally(this);
  5048. }
  5049. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  5050. fort(object),hero(visitor)
  5051. {
  5052. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5053. slotsCount=7;
  5054. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  5055. bg = new CPicture("APHLFTBK.PCX");
  5056. bg->colorizeAndConvert(LOCPLINT->playerID);
  5057. printAtMiddleLoc (fort->hoverName, 325, 32, FONT_BIG, Colors::Jasmine, bg->bg);//Hill Fort
  5058. pos = center(bg->pos);
  5059. heroPic = new CHeroArea(30, 60, hero);
  5060. currState.resize(slotsCount+1);
  5061. costs.resize(slotsCount);
  5062. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  5063. std::vector<std::string> files;
  5064. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  5065. for (int i=0; i<slotsCount; i++)
  5066. {
  5067. currState[i] = getState(i);
  5068. upgrade[i] = new CAdventureMapButton(getTextForSlot(i),"",boost::bind(&CHillFortWindow::makeDeal, this, i),
  5069. 107+i*76, 171, "", SDLK_1+i, &files);
  5070. upgrade[i]->block(currState[i] == -1);
  5071. }
  5072. files.clear();
  5073. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  5074. currState[slotsCount] = getState(slotsCount);
  5075. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, slotsCount),
  5076. 30, 231, "", SDLK_0, &files);
  5077. quit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  5078. bar = new CGStatusBar(327, 332);
  5079. garr = new CGarrisonInt(108, 60, 18, Point(),bg->bg,Point(108,60),hero,NULL);
  5080. updateGarrisons();
  5081. }
  5082. CHillFortWindow::~CHillFortWindow()
  5083. {
  5084. }
  5085. void CHillFortWindow::activate()
  5086. {
  5087. CIntObject::activate();
  5088. GH.statusbar = bar;
  5089. }
  5090. void CHillFortWindow::updateGarrisons()
  5091. {
  5092. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  5093. totalSumm[i]=0;
  5094. for (int i=0; i<slotsCount; i++)
  5095. {
  5096. costs[i].clear();
  5097. int newState = getState(i);
  5098. if (newState != -1)
  5099. {
  5100. UpgradeInfo info;
  5101. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  5102. if (info.newID.size())//we have upgrades here - update costs
  5103. {
  5104. costs[i] = info.cost[0] * hero->getStackCount(i);
  5105. totalSumm += costs[i];
  5106. }
  5107. }
  5108. currState[i] = newState;
  5109. upgrade[i]->setIndex(newState);
  5110. upgrade[i]->block(currState[i] == -1);
  5111. upgrade[i]->hoverTexts[0] = getTextForSlot(i);
  5112. }
  5113. int newState = getState(slotsCount);
  5114. currState[slotsCount] = newState;
  5115. upgradeAll->setIndex(newState);
  5116. garr->recreateSlots();
  5117. }
  5118. void CHillFortWindow::makeDeal(int slot)
  5119. {
  5120. int offset = (slot == slotsCount)?2:0;
  5121. switch (currState[slot])
  5122. {
  5123. case 0:
  5124. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  5125. std::vector<CComponent*>(), soundBase::sound_todo);
  5126. break;
  5127. case 1:
  5128. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  5129. std::vector<CComponent*>(), soundBase::sound_todo);
  5130. break;
  5131. case 2:
  5132. for (int i=0; i<slotsCount; i++)
  5133. if ( slot ==i || ( slot == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  5134. {
  5135. UpgradeInfo info;
  5136. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  5137. LOCPLINT->cb->upgradeCreature(hero, i, info.newID[0]);
  5138. }
  5139. break;
  5140. }
  5141. }
  5142. void CHillFortWindow::showAll (SDL_Surface *to)
  5143. {
  5144. CIntObject::showAll(to);
  5145. for ( int i=0; i<slotsCount; i++)
  5146. {
  5147. if ( currState[i] == 0 || currState[i] == 2 )
  5148. {
  5149. if ( costs[i].size() )//we have several elements
  5150. {
  5151. int curY = 128;//reverse iterator is used to display gold as first element
  5152. for(int j = costs[i].size()-1; j >= 0; j--)
  5153. {
  5154. int val = costs[i][j];
  5155. if(!val) continue;
  5156. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  5157. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::Cornsilk, to);
  5158. curY += 20;
  5159. }
  5160. }
  5161. else//free upgrade - print gold image and "Free" text
  5162. {
  5163. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  5164. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::Cornsilk, to);
  5165. }
  5166. }
  5167. }
  5168. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  5169. {
  5170. if (totalSumm[i])//this resource is used - display it
  5171. {
  5172. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  5173. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::Cornsilk, to);
  5174. }
  5175. }
  5176. }
  5177. std::string CHillFortWindow::getTextForSlot(int slot)
  5178. {
  5179. if ( !hero->getCreature(slot) )//we dont have creature here
  5180. return "";
  5181. std::string str = CGI->generaltexth->allTexts[318];
  5182. int amount = hero->getStackCount(slot);
  5183. if ( amount == 1 )
  5184. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  5185. else
  5186. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  5187. return str;
  5188. }
  5189. int CHillFortWindow::getState(int slot)
  5190. {
  5191. TResources myRes = LOCPLINT->cb->getResourceAmount();
  5192. if ( slot == slotsCount )//"Upgrade all" slot
  5193. {
  5194. bool allUpgraded = true;//All creatures are upgraded?
  5195. for (int i=0; i<slotsCount; i++)
  5196. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  5197. if (allUpgraded)
  5198. return 1;
  5199. if(!totalSumm.canBeAfforded(myRes))
  5200. return 0;
  5201. return 2;
  5202. }
  5203. if (hero->slotEmpty(slot))//no creature here
  5204. return -1;
  5205. UpgradeInfo info;
  5206. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  5207. if (!info.newID.size())//already upgraded
  5208. return 1;
  5209. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  5210. return 0;
  5211. return 2;//can upgrade
  5212. }
  5213. void CThievesGuildWindow::activate()
  5214. {
  5215. CIntObject::activate();
  5216. GH.statusbar = statusBar;
  5217. }
  5218. void CThievesGuildWindow::show(SDL_Surface * to)
  5219. {
  5220. blitAt(background, pos.x, pos.y, to);
  5221. statusBar->show(to);
  5222. exitb->showAll(to);
  5223. resdatabar->show(to);
  5224. //showing border around window
  5225. if(screen->w != 800 || screen->h !=600)
  5226. {
  5227. CMessage::drawBorder(LOCPLINT->playerID, to, pos.w + 28, pos.h + 28, pos.x-14, pos.y-15);
  5228. }
  5229. }
  5230. void CThievesGuildWindow::bexitf()
  5231. {
  5232. GH.popIntTotally(this);
  5233. }
  5234. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner)
  5235. :owner(_owner)
  5236. {
  5237. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5238. SThievesGuildInfo tgi; //info to be displayed
  5239. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  5240. pos = center(Rect(0,0,800,600));// Rect( (conf.cc.resx - 800) / 2, (conf.cc.resy - 600) / 2, 800, 600 );
  5241. //loading backround and converting to more bpp form
  5242. SDL_Surface * bg = background = BitmapHandler::loadBitmap("TpRank.bmp", false);
  5243. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  5244. background = newSurface(bg->w, bg->h);
  5245. blitAt(bg, 0, 0, background);
  5246. SDL_FreeSurface(bg);
  5247. exitb = new CAdventureMapButton (std::string(), std::string(), boost::bind(&CThievesGuildWindow::bexitf,this), 748, 556, "TPMAGE1.def", SDLK_RETURN);
  5248. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  5249. resdatabar = new CMinorResDataBar();
  5250. resdatabar->pos.x += pos.x;
  5251. resdatabar->pos.y += pos.y;
  5252. static std::vector< std::list< ui8 > > SThievesGuildInfo::* fields[] = { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  5253. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks, &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army,
  5254. &SThievesGuildInfo::income};
  5255. //printing texts & descriptions to background
  5256. for(int g=0; g<12; ++g)
  5257. {
  5258. int y;
  5259. if(g == 9) //best hero
  5260. {
  5261. y = 400;
  5262. }
  5263. else if(g == 10) //personality
  5264. {
  5265. y = 460;
  5266. }
  5267. else if(g == 11) //best monster
  5268. {
  5269. y = 510;
  5270. }
  5271. else
  5272. {
  5273. y = 52 + 32*g;
  5274. }
  5275. std::string text = CGI->generaltexth->jktexts[24+g];
  5276. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  5277. printAtMiddle(text, 135, y, FONT_MEDIUM, Colors::Jasmine, background);
  5278. }
  5279. CDefHandler * strips = CDefHandler::giveDef("PRSTRIPS.DEF");
  5280. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  5281. for(int g=0; g<tgi.playerColors.size(); ++g)
  5282. {
  5283. if(g > 0)
  5284. {
  5285. blitAt(strips->ourImages[g-1].bitmap, 250 + 66*g, 7, background);
  5286. }
  5287. printAtMiddle(CGI->generaltexth->jktexts[16+g], 283 + 66*g, 24, FONT_BIG, Colors::Jasmine, background);
  5288. SDL_Surface * box = BitmapHandler::loadBitmap(colorToBox[tgi.playerColors[g]]);
  5289. blitAt(box, 253 + 66*g, 334, background);
  5290. SDL_FreeSurface(box);
  5291. }
  5292. delete strips;
  5293. CDefHandler * flagPictures = CDefHandler::giveDef("itgflags.def");
  5294. //printing flags
  5295. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  5296. {
  5297. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  5298. {
  5299. std::list<ui8> players = (tgi .* fields[g])[b]; //get players with this place in this line
  5300. //std::sort(players.begin(), players.end());
  5301. int counter = 0;
  5302. for(std::list<ui8>::const_iterator it = players.begin(); it != players.end(); ++it)
  5303. {
  5304. int xpos = 259 + 66 * b + 12 * (counter % 4) + 6 * (counter / 4);
  5305. int ypos = 41 + 32 * g + 4 * (counter / 4);
  5306. blitAt(flagPictures->ourImages[*it].bitmap, xpos, ypos, background);
  5307. counter++;
  5308. }
  5309. }
  5310. }
  5311. delete flagPictures;
  5312. flagPictures = NULL;
  5313. //printing best hero
  5314. int counter = 0;
  5315. for(std::map<ui8, InfoAboutHero>::const_iterator it = tgi.colorToBestHero.begin(); it != tgi.colorToBestHero.end(); ++it)
  5316. {
  5317. if(it->second.portrait >= 0)
  5318. blitAt(graphics->portraitSmall[it->second.portrait], 260 + 66 * counter, 360, background);
  5319. counter++;
  5320. //printing stats
  5321. if(it->second.details)
  5322. {
  5323. printAtWB(CGI->generaltexth->allTexts[184], 191 + 66*counter, 396, FONT_TINY, 10, Colors::Cornsilk, background);
  5324. for (int i=0; i<it->second.details->primskills.size(); ++i)
  5325. {
  5326. std::ostringstream skill;
  5327. skill << it->second.details->primskills[i];
  5328. printTo(skill.str(), 244 + 66 * counter, 407 + 11*i, FONT_TINY, Colors::Cornsilk, background);
  5329. }
  5330. }
  5331. }
  5332. //printing best creature
  5333. counter = 0;
  5334. for(std::map<ui8, si32>::const_iterator it = tgi.bestCreature.begin(); it != tgi.bestCreature.end(); ++it)
  5335. {
  5336. if(it->second >= 0)
  5337. blitAt(graphics->bigImgs[it->second], 255 + 66 * counter, 479, background);
  5338. counter++;
  5339. }
  5340. //printing personality
  5341. counter = 0;
  5342. for(std::map<ui8, si8>::const_iterator it = tgi.personality.begin(); it != tgi.personality.end(); ++it)
  5343. {
  5344. int toPrint = 0;
  5345. if(it->second == -1)
  5346. {
  5347. toPrint = 172;
  5348. }
  5349. else
  5350. {
  5351. toPrint = 168 + it->second;
  5352. }
  5353. printAtMiddle(CGI->generaltexth->arraytxt[toPrint], 283 + 66*counter, 459, FONT_SMALL, Colors::Cornsilk, background);
  5354. counter++;
  5355. }
  5356. }
  5357. CThievesGuildWindow::~CThievesGuildWindow()
  5358. {
  5359. SDL_FreeSurface(background);
  5360. // delete exitb;
  5361. // delete statusBar;
  5362. // delete resdatabar;
  5363. }
  5364. void MoraleLuckBox::set(const IBonusBearer *node)
  5365. {
  5366. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  5367. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  5368. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  5369. const int componentType[] = {CComponent::luck, CComponent::morale};
  5370. const int hoverTextBase[] = {7, 4};
  5371. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  5372. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  5373. int mrlt = -9;
  5374. TModDescr mrl;
  5375. if (node)
  5376. {
  5377. node->getModifiersWDescr(mrl, bonusType[morale]);
  5378. bonusValue = (node->*getValue[morale])();
  5379. }
  5380. else
  5381. bonusValue = 0;
  5382. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  5383. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  5384. baseType = componentType[morale];
  5385. text = CGI->generaltexth->arraytxt[textId[morale]];
  5386. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  5387. if (!mrl.size())
  5388. text += CGI->generaltexth->arraytxt[noneTxtId];
  5389. else
  5390. {
  5391. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  5392. {
  5393. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  5394. }
  5395. else
  5396. {
  5397. for(int it=0; it < mrl.size(); it++)
  5398. text += "\n" + mrl[it].second;
  5399. }
  5400. }
  5401. }
  5402. void MoraleLuckBox::showAll(SDL_Surface * to)
  5403. {
  5404. CDefEssential *def;
  5405. if (small)
  5406. def = morale ? graphics->morale30 : graphics->luck30;
  5407. else
  5408. def = morale ? graphics->morale42 : graphics->luck42;
  5409. SDL_Surface *img = def->ourImages[bonusValue + 3].bitmap;
  5410. blitAt(img, Rect(img).centerIn(pos), to); //put img in the center of our pos
  5411. }
  5412. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small)
  5413. :morale(Morale),
  5414. small(Small)
  5415. {
  5416. bonusValue = 0;
  5417. pos = r + pos;
  5418. }
  5419. MoraleLuckBox::~MoraleLuckBox()
  5420. {
  5421. }
  5422. CArtifactHolder::CArtifactHolder()
  5423. {
  5424. type |= WITH_ARTIFACTS;
  5425. }
  5426. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  5427. {
  5428. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5429. aoh->artifactRemoved(artLoc);
  5430. }
  5431. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  5432. {
  5433. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5434. aoh->artifactMoved(artLoc, destLoc);
  5435. }
  5436. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  5437. {
  5438. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5439. aoh->artifactDisassembled(artLoc);
  5440. }
  5441. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  5442. {
  5443. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5444. aoh->artifactAssembled(artLoc);
  5445. }
  5446. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  5447. {
  5448. slotID = -1;
  5449. AOH = NULL;
  5450. art = NULL;
  5451. }
  5452. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  5453. {
  5454. clear();
  5455. }
  5456. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  5457. {
  5458. slotID = place->slotID;
  5459. AOH = place->ourOwner;
  5460. if(slotID >= 19 && dontTakeBackpack)
  5461. art = NULL;
  5462. else
  5463. art = place->ourArt;
  5464. }
  5465. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  5466. {
  5467. if(!AOH)
  5468. return false;
  5469. bool ret = al.hero == AOH->curHero && al.slot == slotID;
  5470. //assert(al.getArt() == art);
  5471. return ret;
  5472. }
  5473. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  5474. {
  5475. assert(AOH && art);
  5476. return art == AOH->curHero->getArt(slotID);
  5477. }
  5478. void CRClickPopup::clickRight(tribool down, bool previousState)
  5479. {
  5480. if(down)
  5481. return;
  5482. close();
  5483. }
  5484. void CRClickPopup::activate()
  5485. {
  5486. activateRClick();
  5487. }
  5488. void CRClickPopup::deactivate()
  5489. {
  5490. deactivateRClick();
  5491. }
  5492. void CRClickPopup::close()
  5493. {
  5494. GH.popIntTotally(this);
  5495. }
  5496. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  5497. {
  5498. int player = LOCPLINT ? LOCPLINT->playerID : 1; //if no player, then use blue
  5499. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  5500. temp->center(Point(GH.current->motion)); //center on mouse
  5501. temp->fitToScreen(10);
  5502. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  5503. GH.pushInt(rcpi);
  5504. }
  5505. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  5506. {
  5507. SDL_Surface *iWin = LOCPLINT->infoWin(obj); //try get custom infowindow for this obj
  5508. if(iWin)
  5509. GH.pushInt(new CInfoPopup(iWin, p, alignment, true));
  5510. else
  5511. CRClickPopup::createAndPush(obj->getHoverText());
  5512. }
  5513. CRClickPopup::CRClickPopup()
  5514. {
  5515. }
  5516. CRClickPopup::~CRClickPopup()
  5517. {
  5518. }
  5519. void CRClickPopupInt::show(SDL_Surface * to)
  5520. {
  5521. inner->show(to);
  5522. }
  5523. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  5524. {
  5525. CCS->curh->hide();
  5526. inner = our;
  5527. delInner = deleteInt;
  5528. }
  5529. CRClickPopupInt::~CRClickPopupInt()
  5530. {
  5531. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  5532. // //TODO? it might be better to rewrite hero window so it will bee newed/deleted on opening / closing (not effort-worthy now, but on some day...?)
  5533. // if(LOCPLINT && inner == adventureInt->heroWindow)
  5534. // adventureInt->heroWindow->dispose();
  5535. if(delInner)
  5536. delete inner;
  5537. CCS->curh->show();
  5538. }
  5539. void CRClickPopupInt::showAll(SDL_Surface * to)
  5540. {
  5541. inner->showAll(to);
  5542. }