GUIClasses.cpp 182 KB

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