GUIClasses.cpp 181 KB

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