GUIClasses.cpp 198 KB

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