GUIClasses.cpp 197 KB

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