GUIClasses.cpp 183 KB

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