GUIClasses.cpp 180 KB

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