GUIClasses.cpp 184 KB

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