GUIClasses.cpp 183 KB

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