GUIClasses.cpp 181 KB

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