GUIClasses.cpp 181 KB

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