GUIClasses.cpp 181 KB

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