GUIClasses.cpp 183 KB

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