GUIClasses.cpp 183 KB

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