GUIClasses.cpp 180 KB

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