GUIClasses.cpp 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209
  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 = pos + creatures[i].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. drawBorder(*bitmap,creatures[j].pos,int3(255,0,0));
  1633. else
  1634. drawBorder(*bitmap,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. bool enough = true;
  1872. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  1873. {
  1874. BLOCK_CAPTURING;
  1875. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*st.count)
  1876. enough = false;
  1877. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*st.count));
  1878. }
  1879. if(enough)
  1880. {
  1881. CFunctionList<void()> fs;
  1882. fs += Upg;
  1883. fs += boost::bind(&CCreInfoWindow::close,this);
  1884. CFunctionList<void()> cfl;
  1885. cfl = boost::bind(&CPlayerInterface::showYesNoDialog, LOCPLINT, CGI->generaltexth->allTexts[207], boost::ref(upgResCost), fs, 0, false);
  1886. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,76,237,"IVIEWCR.DEF",SDLK_u);
  1887. }
  1888. else
  1889. {
  1890. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),76,237,"IVIEWCR.DEF");
  1891. upgrade->callback.funcs.clear();
  1892. upgrade->setOffset(2);
  1893. }
  1894. }
  1895. if(Dsm)
  1896. {
  1897. CFunctionList<void()> fs[2];
  1898. //on dismiss confirmed
  1899. fs[0] += Dsm; //dismiss
  1900. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  1901. CFunctionList<void()> cfl;
  1902. cfl = boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false);
  1903. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,21,237,"IVIEWCR2.DEF",SDLK_d);
  1904. }
  1905. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),216,237,"IOKAY.DEF",SDLK_RETURN);
  1906. }
  1907. }
  1908. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount)
  1909. : type(Type), dismiss(0), upgrade(0), ok(0)
  1910. {
  1911. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1912. const CCreature *cre = CGI->creh->creatures[Cid];
  1913. init(cre, NULL, NULL, creatureCount);
  1914. }
  1915. CCreInfoWindow::CCreInfoWindow(const CStack &st, int Type /*= 0*/)
  1916. : type(Type), dismiss(0), upgrade(0), ok(0)
  1917. {
  1918. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1919. init(st.getCreature(), &st, st.getMyHero(), st.count);
  1920. }
  1921. void CCreInfoWindow::printLine(int nr, const std::string &text, int baseVal, int val/*=-1*/, bool range/*=false*/)
  1922. {
  1923. printAt(text, 155, 48 + nr*19, FONT_SMALL, zwykly, *bitmap);
  1924. std::string hlp;
  1925. if(range && baseVal != val)
  1926. hlp = boost::str(boost::format("%d - %d") % baseVal % val);
  1927. else if(baseVal != val && val>=0)
  1928. hlp = boost::str(boost::format("%d (%d)") % baseVal % val);
  1929. else
  1930. hlp = boost::lexical_cast<std::string>(baseVal);
  1931. printTo(hlp, 276, 61 + nr*19, FONT_SMALL, zwykly, *bitmap);
  1932. }
  1933. //void CCreInfoWindow::init(const CCreature *cre, const CStackInstance *stack, int creatureCount)
  1934. void CCreInfoWindow::init(const CCreature *cre, const CBonusSystemNode *stackNode, const CGHeroInstance *heroOwner, int creatureCount)
  1935. {
  1936. c = cre;
  1937. if(!stackNode) stackNode = c;
  1938. bitmap = new CPicture("CRSTKPU.bmp");
  1939. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  1940. pos = bitmap->center();
  1941. anim = new CCreaturePic(21, 48, c);
  1942. count = boost::lexical_cast<std::string>(creatureCount);
  1943. printAtMiddle(c->namePl,149,30,FONT_SMALL,tytulowy,*bitmap); //creature name
  1944. printLine(0, CGI->generaltexth->primarySkillNames[0], cre->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK));
  1945. printLine(1, CGI->generaltexth->primarySkillNames[1], cre->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE), stackNode->valOfBonuses(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE));
  1946. //if(c->shots)
  1947. // printLine(2, CGI->generaltexth->allTexts[198], c->shots);
  1948. if(stackNode->valOfBonuses(Bonus::SHOTS))
  1949. printLine(2, CGI->generaltexth->allTexts[198], stackNode->valOfBonuses(Bonus::SHOTS));
  1950. //TODO
  1951. int dmgMultiply = 1;
  1952. if(heroOwner && stackNode->hasBonusOfType(Bonus::SIEGE_WEAPON))
  1953. dmgMultiply += heroOwner->Attack();
  1954. printLine(3, CGI->generaltexth->allTexts[199], stackNode->getMinDamage() * dmgMultiply, stackNode->getMaxDamage() * dmgMultiply, true);
  1955. printLine(4, CGI->generaltexth->allTexts[388], cre->valOfBonuses(Bonus::STACK_HEALTH), stackNode->valOfBonuses(Bonus::STACK_HEALTH));
  1956. printLine(6, CGI->generaltexth->zelp[441].first, cre->valOfBonuses(Bonus::STACKS_SPEED), stackNode->valOfBonuses(Bonus::STACKS_SPEED));
  1957. //setting morale
  1958. morale = new MoraleLuckBox(true, genRect(42, 42, 24, 189));
  1959. morale->set(stackNode);
  1960. //setting luck
  1961. luck = new MoraleLuckBox(false, genRect(42, 42, 77, 189));
  1962. luck->set(stackNode);
  1963. //luck and morale
  1964. int luck = 3, morale = 3;
  1965. if(stackNode)
  1966. {
  1967. //add modifiers
  1968. luck += stackNode->LuckVal();
  1969. morale += stackNode->MoraleVal();
  1970. }
  1971. blitAt(graphics->morale42->ourImages[morale].bitmap, 24, 189, *bitmap);
  1972. blitAt(graphics->luck42->ourImages[luck].bitmap, 77, 189, *bitmap);
  1973. if(!type)
  1974. {
  1975. printAtWB(c->abilityText,17,231,FONT_SMALL,35,zwykly,*bitmap);
  1976. }
  1977. //if we are displying window fo r stack in battle, there are several more things that we need to display
  1978. if(const CStack *battleStack = dynamic_cast<const CStack*>(stackNode))
  1979. {
  1980. //spell effects
  1981. int printed=0; //how many effect pics have been printed
  1982. std::vector<si32> spells = battleStack->activeSpells();
  1983. BOOST_FOREACH(si32 effect, spells)
  1984. {
  1985. blitAt(graphics->spellEffectsPics->ourImages[effect + 1].bitmap, 127 + 52 * printed, 186, *bitmap);
  1986. ++printed;
  1987. if(printed >= 3) //we can fit only 3 effects
  1988. break;
  1989. }
  1990. //print current health
  1991. printLine(5, CGI->generaltexth->allTexts[200], battleStack->firstHPleft);
  1992. }
  1993. }
  1994. CCreInfoWindow::~CCreInfoWindow()
  1995. {
  1996. for(int i=0; i<upgResCost.size();i++)
  1997. delete upgResCost[i];
  1998. }
  1999. void CCreInfoWindow::activate()
  2000. {
  2001. CIntObject::activate();
  2002. if(!type)
  2003. activateRClick();
  2004. }
  2005. void CCreInfoWindow::close()
  2006. {
  2007. GH.popIntTotally(this);
  2008. }
  2009. void CCreInfoWindow::clickRight(tribool down, bool previousState)
  2010. {
  2011. if(down)
  2012. return;
  2013. close();
  2014. }
  2015. void CCreInfoWindow::dismissF()
  2016. {
  2017. dsm();
  2018. close();
  2019. }
  2020. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  2021. {
  2022. }
  2023. void CCreInfoWindow::deactivate()
  2024. {
  2025. if(!type)
  2026. deactivateRClick();
  2027. CIntObject::deactivate();
  2028. }
  2029. void CLevelWindow::close()
  2030. {
  2031. for(int i=0;i<comps.size();i++)
  2032. {
  2033. if(comps[i]->selected)
  2034. {
  2035. cb(i);
  2036. break;
  2037. }
  2038. }
  2039. GH.popIntTotally(this);
  2040. LOCPLINT->showingDialog->setn(false);
  2041. }
  2042. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  2043. {
  2044. LOCPLINT->showingDialog->setn(true);
  2045. heroPortrait = hero->portrait;
  2046. cb = callback;
  2047. for(int i=0;i<skills.size();i++)
  2048. {
  2049. comps.push_back(new CSelectableComponent(SComponent::secskill44, skills[i],
  2050. hero->getSecSkillLevel( static_cast<CGHeroInstance::SecondarySkill>(skills[i]) )+1,
  2051. boost::bind(&CLevelWindow::selectionChanged,this,i)));
  2052. comps.back()->assignedKeys.insert(SDLK_1 + i);
  2053. }
  2054. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  2055. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2056. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  2057. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2058. SDL_FreeSurface(hhlp);
  2059. pos.x = screen->w/2 - bitmap->w/2;
  2060. pos.y = screen->h/2 - bitmap->h/2;
  2061. pos.w = bitmap->w;
  2062. pos.h = bitmap->h;
  2063. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  2064. //draw window
  2065. char buf[100], buf2[100];
  2066. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  2067. sprintf(buf,buf2,hero->name.c_str());
  2068. printAtMiddle(buf,192,33,FONT_MEDIUM,zwykly,bitmap);
  2069. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  2070. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  2071. printAtMiddle(buf,192,162,FONT_MEDIUM,zwykly,bitmap);
  2072. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  2073. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,zwykly,bitmap);
  2074. const Font *f = graphics->fonts[FONT_MEDIUM];
  2075. std::string text = CGI->generaltexth->allTexts[4];
  2076. int fontWidth = f->getWidth(text.c_str())/2;
  2077. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  2078. for(int i=0;i<comps.size();i++)
  2079. {
  2080. comps[i]->pos.x = curx+pos.x;
  2081. comps[i]->pos.y = 326+pos.y;
  2082. if( i < (comps.size()-1) )
  2083. {
  2084. curx += 44+21; //skill width + margin to "or"
  2085. printAtMiddle(text ,curx,346,FONT_MEDIUM,zwykly,bitmap);
  2086. curx += fontWidth+15;
  2087. }
  2088. }
  2089. if(comps.size())
  2090. {
  2091. comps[0]->select(true);
  2092. }
  2093. }
  2094. void CLevelWindow::selectionChanged(unsigned to)
  2095. {
  2096. if(ok->isBlocked())
  2097. ok->block(false);
  2098. for(int i=0;i<comps.size();i++)
  2099. if(i==to)
  2100. comps[i]->select(true);
  2101. else
  2102. comps[i]->select(false);
  2103. }
  2104. CLevelWindow::~CLevelWindow()
  2105. {
  2106. delete ok;
  2107. for(int i=0;i<comps.size();i++)
  2108. delete comps[i];
  2109. SDL_FreeSurface(bitmap);
  2110. }
  2111. void CLevelWindow::activate()
  2112. {
  2113. ok->activate();
  2114. for(int i=0;i<comps.size();i++)
  2115. comps[i]->activate();
  2116. }
  2117. void CLevelWindow::deactivate()
  2118. {
  2119. ok->deactivate();
  2120. for(int i=0;i<comps.size();i++)
  2121. comps[i]->deactivate();
  2122. }
  2123. void CLevelWindow::show(SDL_Surface * to)
  2124. {
  2125. blitAt(bitmap,pos.x,pos.y,to);
  2126. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  2127. ok->showAll(to);
  2128. for(int i=0;i<comps.size();i++)
  2129. comps[i]->show(to);
  2130. }
  2131. void CMinorResDataBar::show(SDL_Surface * to)
  2132. {
  2133. blitAt(bg,pos.x,pos.y,to);
  2134. char buf[30];
  2135. for (int i=0;i<7;i++)
  2136. {
  2137. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  2138. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2139. }
  2140. std::vector<std::string> temp;
  2141. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  2142. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  2143. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  2144. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  2145. CGI->generaltexth->allTexts[62]
  2146. +": %s, "
  2147. + CGI->generaltexth->allTexts[63]
  2148. + ": %s, "
  2149. + CGI->generaltexth->allTexts[64]
  2150. + ": %s",temp)
  2151. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,zwykly,to);
  2152. }
  2153. void CMinorResDataBar::showAll(SDL_Surface * to)
  2154. {
  2155. show(to);
  2156. }
  2157. CMinorResDataBar::CMinorResDataBar()
  2158. {
  2159. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  2160. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2161. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  2162. pos.x = 7;
  2163. pos.y = 575;
  2164. pos.w = bg->w;
  2165. pos.h = bg->h;
  2166. }
  2167. CMinorResDataBar::~CMinorResDataBar()
  2168. {
  2169. SDL_FreeSurface(bg);
  2170. }
  2171. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  2172. boost::function<void(int)> Callback, int initState)
  2173. :title(_title), descr(_descr),items(_items),selected(initState)
  2174. {
  2175. init = false;
  2176. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2177. defActions = ACTIVATE | DEACTIVATE | UPDATE | SHOWALL | DISPOSE | SHARE_POS;
  2178. used = LCLICK | KEYBOARD;
  2179. onSelect = Callback;
  2180. length = 9;
  2181. pos.x = (screen->w-306)/2;
  2182. pos.y = (screen->h-468)/2;
  2183. bg = new CPicture("TPGATE.pcx");//x=0, y=0
  2184. bg->colorizeAndConvert(LOCPLINT->playerID);
  2185. slider = new CSlider(277, 120, 256, boost::bind(&CObjectListWindow::moveList,this, _1), length, items.size(), 0, false, 0);
  2186. ok = new AdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  2187. exit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  2188. pos.w = bg->pos.w;
  2189. pos.h = bg->pos.h;
  2190. titleImage = titlePic;
  2191. titleImage->pos.x =153+pos.x-titleImage->pos.w/2;
  2192. titleImage->pos.y =75 +pos.y-titleImage->pos.h/2;
  2193. for (int i=0; i<length; i++)
  2194. areas.push_back(genRect(22, 260, pos.x+15, pos.y+152+i*25 ));//rects for selecting\printing items
  2195. init = true;
  2196. }
  2197. CObjectListWindow::~CObjectListWindow()
  2198. {
  2199. delete titleImage;
  2200. }
  2201. void CObjectListWindow::elementSelected()
  2202. {
  2203. boost::function<void(int)> toCall = onSelect;//save
  2204. int where = items[selected]; //required variables
  2205. GH.popIntTotally(this);//then destroy window
  2206. toCall(where);//and send selected object
  2207. }
  2208. void CObjectListWindow::moveList(int which)
  2209. {
  2210. if (init)//TODO: is there a way to disable running this when CSlider is created?
  2211. showAll(screen2);
  2212. }
  2213. void CObjectListWindow::clickLeft(tribool down, bool previousState)
  2214. {
  2215. if (previousState && (!down))
  2216. {
  2217. for (int i=0; i<areas.size(); i++)
  2218. if(slider->value+i < items.size() && isItIn(&areas[i],GH.current->motion.x,GH.current->motion.y))
  2219. {//check all areas to find element which was clicked
  2220. selected = i+slider->value;
  2221. showAll(screen2);
  2222. return;
  2223. }
  2224. }
  2225. }
  2226. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  2227. {
  2228. if(key.state != SDL_PRESSED) return;
  2229. int sel = selected;
  2230. switch(key.keysym.sym)
  2231. {
  2232. case SDLK_UP:
  2233. sel -=1;
  2234. break;
  2235. case SDLK_DOWN:
  2236. sel +=1;
  2237. break;
  2238. case SDLK_PAGEUP:
  2239. sel -=length;
  2240. break;
  2241. case SDLK_PAGEDOWN:
  2242. sel +=length;
  2243. break;
  2244. case SDLK_HOME:
  2245. sel = 0;
  2246. break;
  2247. case SDLK_END:
  2248. sel = slider->amount;
  2249. break;
  2250. default:
  2251. return;
  2252. }
  2253. if (sel<-1)//nothing was selected & list was moved up
  2254. return;
  2255. if (sel<0)//start of list reached
  2256. sel = 0;
  2257. if ( sel >= slider->amount )//end of list reached
  2258. sel = slider->amount-1;
  2259. if ( sel >= items.size() )
  2260. sel = items.size()-1;
  2261. if ( sel < slider->value )//need to move list up
  2262. slider->moveTo(sel);
  2263. else
  2264. if ( sel >= slider->value+length )//move to bottom
  2265. slider->moveTo(sel-length+1);
  2266. selected = sel;
  2267. showAll(screen2);
  2268. }
  2269. void CObjectListWindow::show(SDL_Surface * to)
  2270. {
  2271. }
  2272. void CObjectListWindow::showAll(SDL_Surface * to)
  2273. {
  2274. ok->block((selected<0)?2:0);
  2275. CIntObject::showAll(to);
  2276. CSDL_Ext::printAtMiddle(title,pos.x+152,pos.y+27,FONT_BIG,tytulowy,to);//"castle gate"
  2277. CSDL_Ext::printAtMiddle(descr,pos.x+145,pos.y+133,FONT_SMALL,zwykly,to);//"select destination"
  2278. titleImage->showAll(to);
  2279. if ( selected >= slider->value && selected < slider->value+length )//if selected item is visible
  2280. {
  2281. SDL_Rect a = areas[selected-slider->value];
  2282. CSDL_Ext::drawBorder(to, a.x, a.y, a.w, a.h, int3(255, 231, 148));
  2283. 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
  2284. }
  2285. int position = slider->value;
  2286. for ( int i = 0; i<9 && i<items.size()-position; i++)
  2287. CSDL_Ext::printAtMiddle(CGI->mh->map->objects[items[i+position]]->hoverName,pos.x+145,pos.y+163+25*i,
  2288. FONT_SMALL, zwykly, to);//print item names in list
  2289. }
  2290. CTradeWindow::CTradeableItem::CTradeableItem( EType Type, int ID, bool Left, int Serial)
  2291. {
  2292. serial = Serial;
  2293. left = Left;
  2294. type = Type;
  2295. id = ID;
  2296. used = LCLICK | HOVER | RCLICK;
  2297. downSelection = false;
  2298. hlp = NULL;
  2299. }
  2300. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  2301. {
  2302. Point posToBitmap;
  2303. Point posToSubCenter;
  2304. switch(type)
  2305. {
  2306. case RESOURCE:
  2307. posToBitmap = Point(19,9);
  2308. posToSubCenter = Point(36, 59);
  2309. break;
  2310. case CREATURE_PLACEHOLDER:
  2311. case CREATURE:
  2312. posToSubCenter = Point(29, 76);
  2313. if(downSelection)
  2314. posToSubCenter.y += 5;
  2315. break;
  2316. case PLAYER:
  2317. posToSubCenter = Point(31, 76);
  2318. break;
  2319. case ARTIFACT_PLACEHOLDER:
  2320. case ARTIFACT_INSTANCE:
  2321. posToSubCenter = Point(19, 55);
  2322. if(downSelection)
  2323. posToSubCenter.y += 8;
  2324. break;
  2325. case ARTIFACT_TYPE:
  2326. posToSubCenter = Point(19, 58);
  2327. break;
  2328. }
  2329. if(SDL_Surface *hlp = getSurface())
  2330. blitAt(hlp, pos + posToBitmap, to);
  2331. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, zwykly, to);
  2332. }
  2333. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2334. {
  2335. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  2336. assert(mw);
  2337. if(down)
  2338. {
  2339. if(type == ARTIFACT_PLACEHOLDER)
  2340. {
  2341. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  2342. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  2343. {
  2344. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  2345. }
  2346. else if(const CArtifactInstance *art = getArtInstance())
  2347. {
  2348. aw->arts->commonInfo->src.AOH = aw->arts;
  2349. aw->arts->commonInfo->src.art = art;
  2350. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  2351. aw->arts->markPossibleSlots(art);
  2352. //aw->arts->commonInfo->dst.AOH = aw->arts;
  2353. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[art->artType->id].bitmap);
  2354. aw->arts->artifactsOnAltar.erase(art);
  2355. id = -1;
  2356. subtitle = "";
  2357. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  2358. }
  2359. aw->calcTotalExp();
  2360. return;
  2361. }
  2362. if(left)
  2363. {
  2364. if(mw->hLeft != this)
  2365. mw->hLeft = this;
  2366. else
  2367. return;
  2368. }
  2369. else
  2370. {
  2371. if(mw->hRight != this)
  2372. mw->hRight = this;
  2373. else
  2374. return;
  2375. }
  2376. mw->selectionChanged(left);
  2377. }
  2378. }
  2379. SDL_Surface * CTradeWindow::CTradeableItem::getSurface()
  2380. {
  2381. switch(type)
  2382. {
  2383. case RESOURCE:
  2384. return graphics->resources32->ourImages[id].bitmap;
  2385. case PLAYER:
  2386. return graphics->flags->ourImages[id].bitmap;
  2387. case ARTIFACT_TYPE:
  2388. case ARTIFACT_PLACEHOLDER:
  2389. case ARTIFACT_INSTANCE:
  2390. return id >= 0 ? graphics->artDefs->ourImages[id].bitmap : NULL;
  2391. case CREATURE:
  2392. return graphics->bigImgs[id];
  2393. default:
  2394. return NULL;
  2395. }
  2396. }
  2397. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  2398. {
  2399. Rect oldPos = pos;
  2400. std::string oldSub = subtitle;
  2401. downSelection = true;
  2402. pos = dstPos;
  2403. subtitle = customSub;
  2404. showAll(to);
  2405. downSelection = false;
  2406. pos = oldPos;
  2407. subtitle = oldSub;
  2408. }
  2409. void CTradeWindow::CTradeableItem::hover(bool on)
  2410. {
  2411. if(!on)
  2412. {
  2413. GH.statusbar->clear();
  2414. return;
  2415. }
  2416. switch(type)
  2417. {
  2418. case CREATURE:
  2419. case CREATURE_PLACEHOLDER:
  2420. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2421. break;
  2422. case ARTIFACT_PLACEHOLDER:
  2423. if(id < 0)
  2424. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  2425. else
  2426. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  2427. break;
  2428. }
  2429. }
  2430. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  2431. {
  2432. if(down)
  2433. {
  2434. switch(type)
  2435. {
  2436. case CREATURE:
  2437. case CREATURE_PLACEHOLDER:
  2438. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2439. break;
  2440. case ARTIFACT_TYPE:
  2441. case ARTIFACT_PLACEHOLDER:
  2442. if(id >= 0)
  2443. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  2444. break;
  2445. }
  2446. }
  2447. }
  2448. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  2449. {
  2450. switch(type)
  2451. {
  2452. case PLAYER:
  2453. return CGI->generaltexth->capColors[id];
  2454. case RESOURCE:
  2455. return CGI->generaltexth->restypes[id];
  2456. case CREATURE:
  2457. if(number == 1)
  2458. return CGI->creh->creatures[id]->nameSing;
  2459. else
  2460. return CGI->creh->creatures[id]->namePl;
  2461. case ARTIFACT_TYPE:
  2462. case ARTIFACT_INSTANCE:
  2463. return CGI->arth->artifacts[id]->Name();
  2464. }
  2465. assert(0);
  2466. return "";
  2467. }
  2468. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  2469. {
  2470. switch(type)
  2471. {
  2472. case ARTIFACT_PLACEHOLDER:
  2473. case ARTIFACT_INSTANCE:
  2474. return (const CArtifactInstance *)hlp;
  2475. default:
  2476. return NULL;
  2477. }
  2478. }
  2479. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  2480. {
  2481. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  2482. hlp = art;
  2483. if(art)
  2484. id = art->artType->id;
  2485. else
  2486. id = -1;
  2487. }
  2488. CTradeWindow::CTradeWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode Mode)
  2489. : market(Market), hero(Hero), arts(NULL), hLeft(NULL), hRight(NULL), readyToTrade(false)
  2490. {
  2491. type |= BLOCK_ADV_HOTKEYS;
  2492. mode = Mode;
  2493. initTypes();
  2494. }
  2495. void CTradeWindow::initTypes()
  2496. {
  2497. switch(mode)
  2498. {
  2499. case RESOURCE_RESOURCE:
  2500. itemsType[1] = RESOURCE;
  2501. itemsType[0] = RESOURCE;
  2502. break;
  2503. case RESOURCE_PLAYER:
  2504. itemsType[1] = RESOURCE;
  2505. itemsType[0] = PLAYER;
  2506. break;
  2507. case CREATURE_RESOURCE:
  2508. itemsType[1] = CREATURE;
  2509. itemsType[0] = RESOURCE;
  2510. break;
  2511. case RESOURCE_ARTIFACT:
  2512. itemsType[1] = RESOURCE;
  2513. itemsType[0] = ARTIFACT_TYPE;
  2514. break;
  2515. case ARTIFACT_RESOURCE:
  2516. itemsType[1] = ARTIFACT_INSTANCE;
  2517. itemsType[0] = RESOURCE;
  2518. break;
  2519. case CREATURE_EXP:
  2520. itemsType[1] = CREATURE;
  2521. itemsType[0] = CREATURE_PLACEHOLDER;
  2522. break;
  2523. case ARTIFACT_EXP:
  2524. itemsType[1] = ARTIFACT_TYPE;
  2525. itemsType[0] = ARTIFACT_PLACEHOLDER;
  2526. break;
  2527. }
  2528. }
  2529. void CTradeWindow::initItems(bool Left)
  2530. {
  2531. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  2532. {
  2533. int xOffset = 0, yOffset = 0;
  2534. if(mode == ARTIFACT_RESOURCE)
  2535. {
  2536. xOffset = -361;
  2537. yOffset = +46;
  2538. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], -1, 1, 0);
  2539. hlp->recActions &= ~(UPDATE | SHOWALL);
  2540. hlp->pos += Rect(137, 469, 42, 42);
  2541. items[Left].push_back(hlp);
  2542. }
  2543. else //ARTIFACT_EXP
  2544. {
  2545. xOffset = -363;
  2546. yOffset = -12;
  2547. }
  2548. BLOCK_CAPTURING;
  2549. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  2550. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  2551. arts->commonInfo->participants.insert(arts);
  2552. arts->recActions = 255;
  2553. arts->setHero(hero);
  2554. arts->allowedAssembling = false;
  2555. addChild(arts);
  2556. artSets.push_back(arts);
  2557. if(mode == ARTIFACT_RESOURCE)
  2558. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  2559. return;
  2560. }
  2561. std::vector<int> *ids = getItemsIds(Left);
  2562. std::vector<Rect> pos;
  2563. int amount = -1;
  2564. getPositionsFor(pos, Left, itemsType[Left]);
  2565. if(Left || !ids)
  2566. amount = 7;
  2567. else
  2568. amount = ids->size();
  2569. if(ids)
  2570. amin(amount, ids->size());
  2571. for(int j=0; j<amount; j++)
  2572. {
  2573. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  2574. if(id < 0 && mode != ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  2575. continue;
  2576. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], id, Left, j);
  2577. hlp->pos = pos[j] + hlp->pos;
  2578. items[Left].push_back(hlp);
  2579. }
  2580. initSubs(Left);
  2581. }
  2582. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  2583. {
  2584. std::vector<int> *ids = NULL;
  2585. if(mode == ARTIFACT_EXP)
  2586. return new std::vector<int>(22, -1);
  2587. if(Left)
  2588. {
  2589. switch(itemsType[1])
  2590. {
  2591. case CREATURE:
  2592. ids = new std::vector<int>;
  2593. for(int i = 0; i < 7; i++)
  2594. {
  2595. if(const CCreature *c = hero->getCreature(i))
  2596. ids->push_back(c->idNumber);
  2597. else
  2598. ids->push_back(-1);
  2599. }
  2600. break;
  2601. }
  2602. }
  2603. else
  2604. {
  2605. switch(itemsType[0])
  2606. {
  2607. case PLAYER:
  2608. ids = new std::vector<int>;
  2609. for(int i = 0; i < PLAYER_LIMIT; i++)
  2610. if(i != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  2611. ids->push_back(i);
  2612. break;
  2613. case ARTIFACT_TYPE:
  2614. ids = new std::vector<int>(market->availableItemsIds(mode));
  2615. break;
  2616. }
  2617. }
  2618. return ids;
  2619. }
  2620. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  2621. {
  2622. if(mode == ARTIFACT_EXP && !Left)
  2623. {
  2624. //22 boxes, 5 in row, last row: two boxes centered
  2625. int h, w, x, y, dx, dy;
  2626. h = w = 44;
  2627. x = 317;
  2628. y = 53;
  2629. dx = 54;
  2630. dy = 70;
  2631. for (int i = 0; i < 4 ; i++)
  2632. for (int j = 0; j < 5 ; j++)
  2633. poss += Rect(x + dx*j, y + dy*i, w, h);
  2634. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  2635. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  2636. }
  2637. else
  2638. {
  2639. //seven boxes:
  2640. // X X X
  2641. // X X X
  2642. // X
  2643. int h, w, x, y, dx, dy;
  2644. int leftToRightOffset;
  2645. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  2646. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  2647. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  2648. genRect(h, w, x + dx, y + 2*dy);
  2649. if(!Left)
  2650. {
  2651. BOOST_FOREACH(Rect &r, poss)
  2652. r.x += leftToRightOffset;
  2653. }
  2654. }
  2655. }
  2656. void CTradeWindow::initSubs(bool Left)
  2657. {
  2658. BOOST_FOREACH(CTradeableItem *t, items[Left])
  2659. {
  2660. if(Left)
  2661. {
  2662. switch(itemsType[1])
  2663. {
  2664. case CREATURE:
  2665. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(t->serial));
  2666. break;
  2667. case RESOURCE:
  2668. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(t->serial));
  2669. break;
  2670. }
  2671. }
  2672. else //right side
  2673. {
  2674. if(itemsType[0] == PLAYER)
  2675. {
  2676. t->subtitle = CGI->generaltexth->capColors[t->id];
  2677. }
  2678. else if(hLeft)//artifact, creature
  2679. {
  2680. int h1, h2; //hlp variables for getting offer
  2681. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  2682. if(t->id != hLeft->id || mode != RESOURCE_RESOURCE) //don't allow exchanging same resources
  2683. {
  2684. std::ostringstream oss;
  2685. oss << h2;
  2686. if(h1!=1)
  2687. oss << "/" << h1;
  2688. t->subtitle = oss.str();
  2689. }
  2690. else
  2691. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  2692. }
  2693. else
  2694. t->subtitle = "";
  2695. }
  2696. }
  2697. }
  2698. void CTradeWindow::showAll(SDL_Surface * to)
  2699. {
  2700. CIntObject::showAll(to);
  2701. if(hRight)
  2702. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2703. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  2704. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2705. if(readyToTrade)
  2706. {
  2707. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2708. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2709. }
  2710. }
  2711. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2712. {
  2713. BOOST_FOREACH(CTradeableItem *t, toRemove)
  2714. removeItem(t);
  2715. }
  2716. void CTradeWindow::removeItem(CTradeableItem * t)
  2717. {
  2718. if(active)
  2719. t->deactivate();
  2720. items[t->left] -= t;
  2721. delChild(t);
  2722. if(hRight == t)
  2723. {
  2724. hRight = NULL;
  2725. selectionChanged(false);
  2726. }
  2727. }
  2728. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2729. {
  2730. BOOST_FOREACH(CTradeableItem *t, items[1])
  2731. if(!hero->getStackCount(t->serial))
  2732. toRemove.insert(t);
  2733. }
  2734. void CTradeWindow::setMode(EMarketMode Mode)
  2735. {
  2736. const IMarket *m = market;
  2737. const CGHeroInstance *h = hero;
  2738. CTradeWindow *nwindow = NULL;
  2739. GH.popIntTotally(this);
  2740. switch(Mode)
  2741. {
  2742. case CREATURE_EXP:
  2743. case ARTIFACT_EXP:
  2744. nwindow = new CAltarWindow(m, h, Mode);
  2745. break;
  2746. default:
  2747. nwindow = new CMarketplaceWindow(m, h, Mode);
  2748. break;
  2749. }
  2750. GH.pushInt(nwindow);
  2751. }
  2752. void CTradeWindow::artifactSelected(CArtPlace *slot)
  2753. {
  2754. assert(mode == ARTIFACT_RESOURCE);
  2755. items[1][0]->setArtInstance(slot->ourArt);
  2756. if(slot->ourArt)
  2757. hLeft = items[1][0];
  2758. else
  2759. hLeft = NULL;
  2760. selectionChanged(true);
  2761. }
  2762. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode Mode)
  2763. : CTradeWindow(Market, Hero, Mode)
  2764. {
  2765. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2766. madeTransaction = false;
  2767. std::string bgName;
  2768. bool sliderNeeded = true;
  2769. switch(Mode)
  2770. {
  2771. case RESOURCE_RESOURCE:
  2772. bgName = "TPMRKRES.bmp";
  2773. break;
  2774. case RESOURCE_PLAYER:
  2775. bgName = "TPMRKPTS.bmp";
  2776. break;
  2777. case CREATURE_RESOURCE:
  2778. bgName = "TPMRKCRS.bmp";
  2779. break;
  2780. case RESOURCE_ARTIFACT:
  2781. bgName = "TPMRKABS.bmp";
  2782. sliderNeeded = false;
  2783. break;
  2784. case ARTIFACT_RESOURCE:
  2785. bgName = "TPMRKASS.bmp";
  2786. sliderNeeded = false;
  2787. break;
  2788. }
  2789. bg = new CPicture(bgName);
  2790. bg->colorizeAndConvert(LOCPLINT->playerID);
  2791. pos = bg->center();
  2792. new CGStatusBar(302, 576);
  2793. if(market->o->ID == 7) //black market
  2794. {
  2795. printAtMiddle(CGI->generaltexth->allTexts[349],300,27,FONT_BIG,tytulowy,*bg); //title
  2796. }
  2797. else if(market->o->ID == 99 || market->o->ID == 221) //trading post
  2798. {
  2799. printAtMiddle(CGI->generaltexth->allTexts[159],300,27,FONT_BIG,tytulowy,*bg); //title
  2800. }
  2801. else if(mode == CREATURE_RESOURCE)
  2802. {
  2803. if(market->o->ID == TOWNI_TYPE)
  2804. printAtMiddle(CGI->buildh->buildings[6][21]->Name(), 300, 27, FONT_BIG, tytulowy, *bg); //title
  2805. else
  2806. printAtMiddle(market->o->getHoverText(), 300, 27, FONT_BIG, tytulowy, *bg); //title
  2807. }
  2808. else if(mode == RESOURCE_ARTIFACT || mode == ARTIFACT_RESOURCE)
  2809. {
  2810. const std::string &title = market->o->ID == TOWNI_TYPE
  2811. ? CGI->buildh->buildings[market->o->subID][17]->Name()
  2812. : market->o->getHoverText();
  2813. printAtMiddle(title, 300, 27, FONT_BIG, tytulowy, *bg); //title
  2814. }
  2815. else
  2816. {
  2817. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,tytulowy,*bg); //marketplace
  2818. }
  2819. initItems(false);
  2820. initItems(true);
  2821. ok = new AdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2822. ok->assignedKeys.insert(SDLK_ESCAPE);
  2823. deal = new AdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2824. deal->block(true);
  2825. //slider and buttons must be created after bg
  2826. if(sliderNeeded)
  2827. {
  2828. slider = new CSlider(231,490,137,0,0,0);
  2829. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2830. max = new AdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2831. max->block(true);
  2832. }
  2833. else
  2834. {
  2835. slider = NULL;
  2836. max = NULL;
  2837. deal->moveBy(Point(-30, 0));
  2838. }
  2839. Rect traderTextRect;
  2840. //left side
  2841. switch(Mode)
  2842. {
  2843. case RESOURCE_RESOURCE:
  2844. case RESOURCE_PLAYER:
  2845. case RESOURCE_ARTIFACT:
  2846. printAtMiddle(CGI->generaltexth->allTexts[270],154,148,FONT_SMALL,zwykly,*bg); //kingdom res.
  2847. break;
  2848. case CREATURE_RESOURCE:
  2849. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 152, 102, FONT_SMALL, zwykly, *bg); //%s's Creatures
  2850. break;
  2851. case ARTIFACT_RESOURCE:
  2852. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->name), 152, 57, FONT_SMALL, zwykly, *bg); //%s's Artifacts
  2853. break;
  2854. }
  2855. //right side
  2856. switch(Mode)
  2857. {
  2858. case RESOURCE_RESOURCE:
  2859. case CREATURE_RESOURCE:
  2860. case RESOURCE_ARTIFACT:
  2861. case ARTIFACT_RESOURCE:
  2862. printAtMiddle(CGI->generaltexth->allTexts[168],445,148,FONT_SMALL,zwykly,*bg); //available for trade
  2863. traderTextRect = Rect(316, 48, 260, 75);
  2864. break;
  2865. case RESOURCE_PLAYER:
  2866. printAtMiddle(CGI->generaltexth->allTexts[169],445,55,FONT_SMALL,zwykly,*bg); //players
  2867. traderTextRect = Rect(28, 48, 260, 75);
  2868. break;
  2869. }
  2870. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2871. int specialOffset = mode == ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  2872. if(printButtonFor(RESOURCE_PLAYER))
  2873. new AdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2874. if(printButtonFor(RESOURCE_RESOURCE))
  2875. new AdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  2876. if(printButtonFor(CREATURE_RESOURCE))
  2877. 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
  2878. if(printButtonFor(RESOURCE_ARTIFACT))
  2879. new AdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  2880. if(printButtonFor(ARTIFACT_RESOURCE))
  2881. 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
  2882. updateTraderText();
  2883. }
  2884. CMarketplaceWindow::~CMarketplaceWindow()
  2885. {
  2886. hLeft = hRight = NULL;
  2887. for(int i=0;i<items[1].size();i++)
  2888. delChild(items[1][i]);
  2889. for(int i=0;i<items[0].size();i++)
  2890. delChild(items[0][i]);
  2891. items[1].clear();
  2892. items[0].clear();
  2893. delChild(bg);
  2894. bg = NULL;
  2895. }
  2896. void CMarketplaceWindow::setMax()
  2897. {
  2898. slider->moveToMax();
  2899. }
  2900. void CMarketplaceWindow::makeDeal()
  2901. {
  2902. int sliderValue = 0;
  2903. if(slider)
  2904. sliderValue = slider->value;
  2905. else
  2906. sliderValue = !deal->isBlocked(); //should always be 1
  2907. if(!sliderValue)
  2908. return;
  2909. int leftIdToSend = -1;
  2910. if(mode == CREATURE_RESOURCE)
  2911. leftIdToSend = hLeft->serial;
  2912. else if(mode == ARTIFACT_RESOURCE)
  2913. leftIdToSend = hLeft->getArtInstance()->id;
  2914. else
  2915. leftIdToSend = hLeft->id;
  2916. if(slider)
  2917. {
  2918. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2919. slider->moveTo(0);
  2920. }
  2921. else
  2922. {
  2923. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2924. }
  2925. madeTransaction = true;
  2926. hLeft = NULL;
  2927. hRight = NULL;
  2928. selectionChanged(true);
  2929. }
  2930. void CMarketplaceWindow::sliderMoved( int to )
  2931. {
  2932. redraw();
  2933. }
  2934. void CMarketplaceWindow::selectionChanged(bool side)
  2935. {
  2936. readyToTrade = hLeft && hRight;
  2937. if(mode == RESOURCE_RESOURCE)
  2938. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  2939. if(mode == ARTIFACT_RESOURCE && !hLeft)
  2940. arts->unmarkSlots(false);
  2941. if(readyToTrade)
  2942. {
  2943. int soldItemId = hLeft->id;
  2944. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  2945. if(slider)
  2946. {
  2947. int newAmount = -1;
  2948. if(itemsType[1] == RESOURCE)
  2949. newAmount = LOCPLINT->cb->getResourceAmount(soldItemId);
  2950. else if(itemsType[1] == CREATURE)
  2951. newAmount = hero->getStackCount(hLeft->serial) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2952. else
  2953. assert(0);
  2954. slider->setAmount(newAmount / r1);
  2955. slider->moveTo(0);
  2956. max->block(false);
  2957. deal->block(false);
  2958. }
  2959. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  2960. {
  2961. deal->block(LOCPLINT->cb->getResourceAmount(soldItemId) < r1);
  2962. }
  2963. else
  2964. deal->block(false);
  2965. }
  2966. else
  2967. {
  2968. if(slider)
  2969. {
  2970. max->block(true);
  2971. slider->setAmount(0);
  2972. slider->moveTo(0);
  2973. }
  2974. deal->block(true);
  2975. }
  2976. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2977. initSubs(false);
  2978. updateTraderText();
  2979. redraw();
  2980. }
  2981. bool CMarketplaceWindow::printButtonFor(EMarketMode M) const
  2982. {
  2983. return market->allowsTrade(M) && M != mode && (hero || ( M != CREATURE_RESOURCE && M != RESOURCE_ARTIFACT && M != ARTIFACT_RESOURCE ));
  2984. }
  2985. void CMarketplaceWindow::garrisonChanged()
  2986. {
  2987. if(mode != CREATURE_RESOURCE)
  2988. return;
  2989. std::set<CTradeableItem *> toRemove;
  2990. getEmptySlots(toRemove);
  2991. removeItems(toRemove);
  2992. initSubs(true);
  2993. }
  2994. void CMarketplaceWindow::artifactsChanged(bool Left)
  2995. {
  2996. assert(!Left);
  2997. if(mode != RESOURCE_ARTIFACT)
  2998. return;
  2999. std::vector<int> available = market->availableItemsIds(mode);
  3000. std::set<CTradeableItem *> toRemove;
  3001. BOOST_FOREACH(CTradeableItem *t, items[0])
  3002. if(!vstd::contains(available, t->id))
  3003. toRemove.insert(t);
  3004. removeItems(toRemove);
  3005. redraw();
  3006. }
  3007. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  3008. {
  3009. if(Left)
  3010. {
  3011. switch(itemsType[1])
  3012. {
  3013. case RESOURCE:
  3014. case CREATURE:
  3015. {
  3016. int val = slider
  3017. ? slider->value * r1
  3018. : (((deal->isBlocked())) ? 0 : r1);
  3019. return boost::lexical_cast<std::string>(val);
  3020. }
  3021. case ARTIFACT_INSTANCE:
  3022. return ((deal->isBlocked()) ? "0" : "1");
  3023. }
  3024. }
  3025. else
  3026. {
  3027. switch(itemsType[0])
  3028. {
  3029. case RESOURCE:
  3030. if(slider)
  3031. return boost::lexical_cast<std::string>( slider->value * r2 );
  3032. else
  3033. return boost::lexical_cast<std::string>(r2);
  3034. case ARTIFACT_TYPE:
  3035. return ((deal->isBlocked()) ? "0" : "1");
  3036. case PLAYER:
  3037. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  3038. }
  3039. }
  3040. return "???";
  3041. }
  3042. Point CMarketplaceWindow::selectionOffset(bool Left) const
  3043. {
  3044. if(Left)
  3045. {
  3046. switch(itemsType[1])
  3047. {
  3048. case RESOURCE:
  3049. return Point(122, 446);
  3050. case CREATURE:
  3051. return Point(128, 450);
  3052. case ARTIFACT_INSTANCE:
  3053. return Point(134, 466);
  3054. }
  3055. }
  3056. else
  3057. {
  3058. switch(itemsType[0])
  3059. {
  3060. case RESOURCE:
  3061. if(mode == ARTIFACT_RESOURCE)
  3062. return Point(410, 469);
  3063. else
  3064. return Point(410, 446);
  3065. case ARTIFACT_TYPE:
  3066. return Point(425, 447);
  3067. case PLAYER:
  3068. return Point(417, 451);
  3069. }
  3070. }
  3071. assert(0);
  3072. return Point(0,0);
  3073. }
  3074. void CMarketplaceWindow::resourceChanged(int type, int val)
  3075. {
  3076. initSubs(true);
  3077. }
  3078. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  3079. {
  3080. switch(type)
  3081. {
  3082. case RESOURCE:
  3083. dx = 82;
  3084. dy = 79;
  3085. x = 39;
  3086. y = 180;
  3087. h = 66;
  3088. w = 74;
  3089. break;
  3090. case PLAYER:
  3091. dx = 83;
  3092. dy = 118;
  3093. h = 64;
  3094. w = 58;
  3095. x = 44;
  3096. y = 83;
  3097. assert(Right);
  3098. break;
  3099. case CREATURE://45,123
  3100. x = 45;
  3101. y = 123;
  3102. w = 58;
  3103. h = 64;
  3104. dx = 83;
  3105. dy = 98;
  3106. assert(!Right);
  3107. break;
  3108. case ARTIFACT_TYPE://45,123
  3109. x = 340-289;
  3110. y = 180;
  3111. w = 44;
  3112. h = 44;
  3113. dx = 83;
  3114. dy = 79;
  3115. break;
  3116. }
  3117. leftToRightOffset = 289;
  3118. }
  3119. void CMarketplaceWindow::updateTraderText()
  3120. {
  3121. if(readyToTrade)
  3122. {
  3123. if(mode == RESOURCE_PLAYER)
  3124. {
  3125. //I can give %s to the %s player.
  3126. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  3127. }
  3128. else if(mode == RESOURCE_ARTIFACT)
  3129. {
  3130. //I can offer you the %s for %d %s of %s.
  3131. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  3132. }
  3133. else if(mode == RESOURCE_RESOURCE)
  3134. {
  3135. //I can offer you %d %s of %s for %d %s of %s.
  3136. 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()));
  3137. }
  3138. else if(mode == CREATURE_RESOURCE)
  3139. {
  3140. //I can offer you %d %s of %s for %d %s.
  3141. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  3142. }
  3143. else if(mode == ARTIFACT_RESOURCE)
  3144. {
  3145. //I can offer you %d %s of %s for your %s.
  3146. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  3147. }
  3148. return;
  3149. }
  3150. int gnrtxtnr = -1;
  3151. if(madeTransaction)
  3152. {
  3153. if(mode == RESOURCE_PLAYER)
  3154. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  3155. else
  3156. 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?
  3157. }
  3158. else
  3159. {
  3160. if(mode == RESOURCE_PLAYER)
  3161. 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.
  3162. else
  3163. 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.
  3164. }
  3165. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  3166. }
  3167. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= NULL*/, EMarketMode Mode)
  3168. :CTradeWindow(Market, Hero, Mode)
  3169. {
  3170. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3171. bg = new CPicture(Mode == CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp");
  3172. bg->colorizeAndConvert(LOCPLINT->playerID);
  3173. pos = bg->center();
  3174. if(Mode == CREATURE_EXP)
  3175. {
  3176. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 155, 30, FONT_SMALL, tytulowy, *bg); //%s's Creatures
  3177. printAtMiddle(CGI->generaltexth->allTexts[479], 450, 30, FONT_SMALL, tytulowy, *bg); //Altar of Sacrifice
  3178. 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
  3179. slider = new CSlider(231,481,137,0,0,0);
  3180. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  3181. max = new AdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  3182. sacrificedUnits.resize(ARMY_SIZE, 0);
  3183. sacrificeAll = new AdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  3184. sacrificeBackpack = NULL;
  3185. initItems(true);
  3186. mimicCres();
  3187. }
  3188. else
  3189. {
  3190. printAtMiddle(CGI->generaltexth->allTexts[477], 450, 34, FONT_SMALL, tytulowy, *bg); //Sacrifice artifacts for experience
  3191. printAtMiddle(CGI->generaltexth->allTexts[478], 302, 423, FONT_SMALL, tytulowy, *bg); //%s's Creatures
  3192. sacrificeAll = new AdventureMapButton(CGI->generaltexth->zelp[571],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  3193. sacrificeAll->block(!hero->artifactsInBackpack.size() && !hero->artifactsWorn.size());
  3194. sacrificeBackpack = new AdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  3195. sacrificeBackpack->block(!hero->artifactsInBackpack.size());
  3196. slider = NULL;
  3197. max = NULL;
  3198. initItems(true);
  3199. initItems(false);
  3200. }
  3201. printAtMiddleWB(CGI->generaltexth->allTexts[475], 72, 437, FONT_SMALL, 17, tytulowy, *bg); //Experience needed to reach next level
  3202. printAtMiddleWB(CGI->generaltexth->allTexts[476], 72, 505, FONT_SMALL, 17, tytulowy, *bg); //Total experience on the Altar
  3203. new CGStatusBar(302, 576);
  3204. ok = new AdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  3205. ok->assignedKeys.insert(SDLK_ESCAPE);
  3206. deal = new AdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  3207. if(Hero->getAlignment() != EVIL && Mode == CREATURE_EXP)
  3208. new AdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  3209. if(Hero->getAlignment() != GOOD && Mode == ARTIFACT_EXP)
  3210. new AdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  3211. expPerUnit.resize(ARMY_SIZE, 0);
  3212. getExpValues();
  3213. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  3214. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  3215. setExpToLevel();
  3216. calcTotalExp();
  3217. blockTrade();
  3218. }
  3219. CAltarWindow::~CAltarWindow()
  3220. {
  3221. }
  3222. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  3223. {
  3224. leftToRightOffset = 289;
  3225. x = 45;
  3226. y = 110;
  3227. w = 58;
  3228. h = 64;
  3229. dx = 83;
  3230. dy = 98;
  3231. }
  3232. void CAltarWindow::sliderMoved(int to)
  3233. {
  3234. sacrificedUnits[hLeft->serial] = to;
  3235. updateRight(hRight);
  3236. deal->block(!to);
  3237. calcTotalExp();
  3238. redraw();
  3239. }
  3240. void CAltarWindow::makeDeal()
  3241. {
  3242. if(mode == CREATURE_EXP)
  3243. {
  3244. blockTrade();
  3245. slider->value = 0;
  3246. std::vector<int> toSacrifice = sacrificedUnits;
  3247. for (int i = 0; i < toSacrifice.size(); i++)
  3248. {
  3249. if(toSacrifice[i])
  3250. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  3251. }
  3252. BOOST_FOREACH(int& val, sacrificedUnits)
  3253. val = 0;
  3254. BOOST_FOREACH(CTradeableItem *t, items[0])
  3255. {
  3256. t->type = CREATURE_PLACEHOLDER;
  3257. t->subtitle = "";
  3258. }
  3259. }
  3260. else
  3261. {
  3262. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar) //sacrifice each artifact on the list
  3263. {
  3264. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  3265. }
  3266. arts->artifactsOnAltar.clear();
  3267. BOOST_FOREACH(CTradeableItem *t, items[0])
  3268. {
  3269. t->id = -1;
  3270. t->subtitle = "";
  3271. }
  3272. arts->commonInfo->reset();
  3273. //arts->scrollBackpack(0);
  3274. deal->block(true);
  3275. }
  3276. calcTotalExp();
  3277. }
  3278. void CAltarWindow::SacrificeAll()
  3279. {
  3280. if(mode == CREATURE_EXP)
  3281. {
  3282. bool movedAnything = false;
  3283. BOOST_FOREACH(CTradeableItem *t, items[1])
  3284. sacrificedUnits[t->serial] = hero->getStackCount(t->serial);
  3285. sacrificedUnits[items[1].front()->serial]--;
  3286. BOOST_FOREACH(CTradeableItem *t, items[0])
  3287. {
  3288. updateRight(t);
  3289. if(t->type == CREATURE)
  3290. movedAnything = true;
  3291. }
  3292. deal->block(!movedAnything);
  3293. calcTotalExp();
  3294. }
  3295. else
  3296. {
  3297. for(std::map<ui16, ArtSlotInfo>::const_iterator i = hero->artifactsWorn.begin(); i != hero->artifactsWorn.end(); i++)
  3298. {
  3299. if(i->second.artifact->artType->id != 145) //ignore locks from assembled artifacts
  3300. moveFromSlotToAltar(i->first, NULL, i->second.artifact);
  3301. }
  3302. SacrificeBackpack();
  3303. }
  3304. redraw();
  3305. }
  3306. void CAltarWindow::selectionChanged(bool side)
  3307. {
  3308. if(mode != CREATURE_EXP)
  3309. return;
  3310. CTradeableItem *&selected = side ? hLeft : hRight;
  3311. CTradeableItem *&theOther = side ? hRight : hLeft;
  3312. theOther = *std::find_if(items[!side].begin(), items[!side].end(), boost::bind(&CTradeableItem::serial, _1) == selected->serial);
  3313. int stackCount = 0;
  3314. for (int i = 0; i < ARMY_SIZE; i++)
  3315. if(hero->getStackCount(i) > sacrificedUnits[i])
  3316. stackCount++;
  3317. slider->setAmount(hero->getStackCount(hLeft->serial) - (stackCount == 1));
  3318. slider->block(!slider->amount);
  3319. slider->value = sacrificedUnits[hLeft->serial];
  3320. max->block(!slider->amount);
  3321. readyToTrade = true;
  3322. redraw();
  3323. }
  3324. void CAltarWindow::mimicCres()
  3325. {
  3326. std::vector<Rect> positions;
  3327. getPositionsFor(positions, false, CREATURE);
  3328. BOOST_FOREACH(CTradeableItem *t, items[1])
  3329. {
  3330. CTradeableItem *hlp = new CTradeableItem(CREATURE_PLACEHOLDER, t->id, false, t->serial);
  3331. hlp->pos = positions[t->serial] + hlp->pos;
  3332. items[0].push_back(hlp);
  3333. }
  3334. }
  3335. Point CAltarWindow::selectionOffset(bool Left) const
  3336. {
  3337. if(Left)
  3338. return Point(150, 421);
  3339. else
  3340. return Point(396, 421);
  3341. }
  3342. std::string CAltarWindow::selectionSubtitle(bool Left) const
  3343. {
  3344. if(Left && slider && hLeft)
  3345. return boost::lexical_cast<std::string>(slider->value);
  3346. else if(!Left && hRight)
  3347. return hRight->subtitle;
  3348. else
  3349. return "";
  3350. }
  3351. void CAltarWindow::artifactsChanged(bool left)
  3352. {
  3353. }
  3354. void CAltarWindow::garrisonChanged()
  3355. {
  3356. if(mode != CREATURE_EXP)
  3357. return;
  3358. std::set<CTradeableItem *> empty;
  3359. getEmptySlots(empty);
  3360. BOOST_FOREACH(CTradeableItem *t, empty)
  3361. {
  3362. removeItem(*std::find_if(items[0].begin(), items[0].end(), boost::bind(&CTradeableItem::serial, _1) == t->serial));
  3363. removeItem(t);
  3364. }
  3365. initSubs(true);
  3366. getExpValues();
  3367. }
  3368. void CAltarWindow::getExpValues()
  3369. {
  3370. int dump;
  3371. BOOST_FOREACH(CTradeableItem *t, items[1])
  3372. if(t->id >= 0)
  3373. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], CREATURE_EXP);
  3374. }
  3375. void CAltarWindow::calcTotalExp()
  3376. {
  3377. int val = 0;
  3378. if(mode == CREATURE_EXP)
  3379. {
  3380. for (int i = 0; i < sacrificedUnits.size(); i++)
  3381. {
  3382. val += expPerUnit[i] * sacrificedUnits[i];
  3383. }
  3384. }
  3385. else
  3386. {
  3387. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar)
  3388. {
  3389. int dmp, valOfArt;
  3390. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  3391. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  3392. }
  3393. }
  3394. val *=(100+hero->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0f;
  3395. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  3396. }
  3397. void CAltarWindow::setExpToLevel()
  3398. {
  3399. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  3400. }
  3401. void CAltarWindow::blockTrade()
  3402. {
  3403. hLeft = hRight = NULL;
  3404. readyToTrade = false;
  3405. if(slider)
  3406. {
  3407. slider->block(true);
  3408. max->block(true);
  3409. }
  3410. deal->block(true);
  3411. }
  3412. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  3413. {
  3414. int val = sacrificedUnits[toUpdate->serial];
  3415. toUpdate->type = val ? CREATURE : CREATURE_PLACEHOLDER;
  3416. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  3417. }
  3418. int CAltarWindow::firstFreeSlot()
  3419. {
  3420. int ret = -1;
  3421. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  3422. return ret < items[0].size() ? ret : -1;
  3423. }
  3424. void CAltarWindow::SacrificeBackpack()
  3425. {
  3426. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  3427. for (int i = 0; i < hero->artifactsInBackpack.size(); i++)
  3428. {
  3429. if(vstd::contains(toOmmit, hero->artifactsInBackpack[i].artifact))
  3430. {
  3431. toOmmit -= hero->artifactsInBackpack[i].artifact;
  3432. continue;
  3433. }
  3434. putOnAltar(NULL, hero->artifactsInBackpack[i].artifact);
  3435. }
  3436. arts->scrollBackpack(0);
  3437. calcTotalExp();
  3438. }
  3439. void CAltarWindow::artifactPicked()
  3440. {
  3441. redraw();
  3442. }
  3443. void CAltarWindow::showAll(SDL_Surface * to)
  3444. {
  3445. CTradeWindow::showAll(to);
  3446. if(mode == ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  3447. {
  3448. blitAtLoc(graphics->artDefs->ourImages[arts->commonInfo->src.art->artType->id].bitmap, 281, 442, to);
  3449. int dmp, val;
  3450. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, ARTIFACT_EXP);
  3451. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, zwykly, to);
  3452. }
  3453. }
  3454. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  3455. {
  3456. int artID = art->artType->id;
  3457. if(artID != 1 && artID < 7) //special art
  3458. {
  3459. tlog2 << "Cannot put special artifact on altar!\n";
  3460. return false;
  3461. }
  3462. if(!altarSlot)
  3463. {
  3464. int slotIndex = firstFreeSlot();
  3465. if(slotIndex < 0)
  3466. {
  3467. tlog2 << "No free slots on altar!\n";
  3468. return false;
  3469. }
  3470. altarSlot = items[0][slotIndex];
  3471. }
  3472. int dmp, val;
  3473. market->getOffer(artID, 0, dmp, val, ARTIFACT_EXP);
  3474. arts->artifactsOnAltar.insert(art);
  3475. altarSlot->setArtInstance(art);
  3476. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  3477. deal->block(false);
  3478. return true;
  3479. }
  3480. void CAltarWindow::moveFromSlotToAltar(int slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  3481. {
  3482. int freeBackpackSlot = hero->artifactsInBackpack.size() + Arts::BACKPACK_START;
  3483. if(arts->commonInfo->src.art)
  3484. {
  3485. arts->commonInfo->dst.slotID = freeBackpackSlot;
  3486. arts->commonInfo->dst.AOH = arts;
  3487. }
  3488. if(putOnAltar(altarSlot, art))
  3489. {
  3490. if(slotID < Arts::BACKPACK_START)
  3491. LOCPLINT->cb->swapArtifacts(hero, slotID, hero, freeBackpackSlot);
  3492. else
  3493. {
  3494. arts->commonInfo->src.clear();
  3495. arts->commonInfo->dst.clear();
  3496. CCS->curh->dragAndDropCursor(NULL);
  3497. arts->unmarkSlots(false);
  3498. }
  3499. }
  3500. }
  3501. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  3502. {
  3503. this->pos = pos;
  3504. SDL_Surface *hhlp = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  3505. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  3506. background = SDL_ConvertSurface(hhlp,screen->format,0);
  3507. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  3508. SDL_FreeSurface(hhlp);
  3509. //printing texts
  3510. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 242, 32, FONT_BIG, tytulowy, background); //window title
  3511. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 64, FONT_MEDIUM, tytulowy, background); //hero speed
  3512. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 130, FONT_MEDIUM, tytulowy, background); //enemy speed
  3513. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 196, FONT_MEDIUM, tytulowy, background); //map scroll speed
  3514. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 262, FONT_MEDIUM, tytulowy, background); //video quality
  3515. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 347, FONT_MEDIUM, tytulowy, background); //music volume
  3516. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 412, FONT_MEDIUM, tytulowy, background); //effects volume
  3517. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 282, 57, FONT_MEDIUM, zwykly, background); //show move path
  3518. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 282, 89, FONT_MEDIUM, zwykly, background); //show hero reminder
  3519. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 282, 121, FONT_MEDIUM, zwykly, background); //quick combat
  3520. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 282, 153, FONT_MEDIUM, zwykly, background); //video subtitles
  3521. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 282, 185, FONT_MEDIUM, zwykly, background); //town building outlines
  3522. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 282, 217, FONT_MEDIUM, zwykly, background); //spell book animation
  3523. //setting up buttons
  3524. // 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);
  3525. // std::swap(save->imgs[0][0], load->imgs[0][1]);
  3526. 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);
  3527. save->swappedImages = true;
  3528. save->update();
  3529. // 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);
  3530. // std::swap(save->imgs[0][0], restart->imgs[0][1]);
  3531. 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);
  3532. mainMenu->swappedImages = true;
  3533. mainMenu->update();
  3534. 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);
  3535. quitGame->swappedImages = true;
  3536. quitGame->update();
  3537. 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);
  3538. backToMap->swappedImages = true;
  3539. backToMap->update();
  3540. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3541. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3542. 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);
  3543. 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);
  3544. 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);
  3545. 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);
  3546. heroMoveSpeed->select(owner->sysOpts.heroMoveSpeed, 1);
  3547. heroMoveSpeed->onChange = boost::bind(&SystemOptions::setHeroMoveSpeed, &owner->sysOpts, _1);
  3548. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3549. 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);
  3550. 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);
  3551. 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);
  3552. mapScrollSpeed->select(owner->sysOpts.mapScrollingSpeed, 1);
  3553. mapScrollSpeed->onChange = boost::bind(&SystemOptions::setMapScrollingSpeed, &owner->sysOpts, _1);
  3554. musicVolume = new CHighlightableButtonsGroup(0, true);
  3555. for(int i=0; i<10; ++i)
  3556. {
  3557. 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);
  3558. }
  3559. musicVolume->select(CCS->musich->getVolume(), 1);
  3560. musicVolume->onChange = boost::bind(&SystemOptions::setMusicVolume, &owner->sysOpts, _1);
  3561. effectsVolume = new CHighlightableButtonsGroup(0, true);
  3562. for(int i=0; i<10; ++i)
  3563. {
  3564. 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);
  3565. }
  3566. effectsVolume->select(CCS->soundh->getVolume(), 1);
  3567. effectsVolume->onChange = boost::bind(&SystemOptions::setSoundVolume, &owner->sysOpts, _1);
  3568. }
  3569. CSystemOptionsWindow::~CSystemOptionsWindow()
  3570. {
  3571. SDL_FreeSurface(background);
  3572. delete save;
  3573. delete quitGame;
  3574. delete backToMap;
  3575. delete mainMenu;
  3576. delete heroMoveSpeed;
  3577. delete mapScrollSpeed;
  3578. delete musicVolume;
  3579. delete effectsVolume;
  3580. }
  3581. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  3582. {
  3583. GH.popIntTotally(this);
  3584. SDL_Event event;
  3585. event.type = type;
  3586. event.user.code = usercode; // not necessarily used
  3587. SDL_PushEvent(&event);
  3588. }
  3589. void CSystemOptionsWindow::bquitf()
  3590. {
  3591. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  3592. }
  3593. void CSystemOptionsWindow::breturnf()
  3594. {
  3595. GH.popIntTotally(this);
  3596. }
  3597. void CSystemOptionsWindow::bmainmenuf()
  3598. {
  3599. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, 2), 0, false);
  3600. }
  3601. void CSystemOptionsWindow::bsavef()
  3602. {
  3603. GH.popIntTotally(this);
  3604. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  3605. /*using namespace boost::posix_time;
  3606. std::ostringstream fnameStream;
  3607. fnameStream << second_clock::local_time();
  3608. std::string fname = fnameStream.str();
  3609. boost::algorithm::replace_all(fname,":","");
  3610. boost::algorithm::replace_all(fname," ","-");
  3611. 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);*/
  3612. }
  3613. void CSystemOptionsWindow::activate()
  3614. {
  3615. save->activate();
  3616. quitGame->activate();
  3617. backToMap->activate();
  3618. mainMenu->activate();
  3619. heroMoveSpeed->activate();
  3620. mapScrollSpeed->activate();
  3621. musicVolume->activate();
  3622. effectsVolume->activate();
  3623. }
  3624. void CSystemOptionsWindow::deactivate()
  3625. {
  3626. save->deactivate();
  3627. quitGame->deactivate();
  3628. backToMap->deactivate();
  3629. mainMenu->deactivate();
  3630. heroMoveSpeed->deactivate();
  3631. mapScrollSpeed->deactivate();
  3632. musicVolume->deactivate();
  3633. effectsVolume->deactivate();
  3634. }
  3635. void CSystemOptionsWindow::show(SDL_Surface *to)
  3636. {
  3637. CSDL_Ext::blitSurface(background, NULL, to, &pos);
  3638. save->showAll(to);
  3639. quitGame->showAll(to);
  3640. backToMap->showAll(to);
  3641. mainMenu->showAll(to);
  3642. heroMoveSpeed->showAll(to);
  3643. mapScrollSpeed->showAll(to);
  3644. musicVolume->showAll(to);
  3645. effectsVolume->showAll(to);
  3646. }
  3647. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj)
  3648. : tavernObj(TavernObj)
  3649. {
  3650. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3651. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3652. assert(h.size() == 2);
  3653. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3654. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3655. if(h[0])
  3656. selected = 0;
  3657. else
  3658. selected = -1;
  3659. oldSelected = -1;
  3660. bg = new CPicture("TPTAVERN.bmp");
  3661. bg->colorizeAndConvert(LOCPLINT->playerID);
  3662. pos = center(bg->pos);
  3663. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,FONT_BIG,tytulowy,*bg);
  3664. printAtMiddle("2500",320,328,FONT_SMALL,zwykly,*bg);
  3665. // printAtMiddle(CGI->generaltexth->jktexts[38],146,283,FONT_BIG,tytulowy,bg); //what is this???
  3666. printAtMiddleWB(LOCPLINT->cb->getTavernGossip(tavernObj), 200, 220, FONT_SMALL, 50, zwykly, *bg);
  3667. bar = new CGStatusBar(8, 478, "APHLFTRT.bmp", 380);
  3668. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3669. recruit = new AdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3670. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3671. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  3672. {
  3673. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3674. recruit->block(2);
  3675. }
  3676. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3677. {
  3678. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3679. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3680. recruit->block(2);
  3681. }
  3682. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3683. {
  3684. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3685. recruit->block(2);
  3686. }
  3687. else
  3688. {
  3689. if(!h[0])
  3690. recruit->block(1);
  3691. }
  3692. #ifdef _WIN32
  3693. CCS->videoh->open("TAVERN.BIK");
  3694. #else
  3695. CCS->videoh->open("tavern.mjpg", true, false);
  3696. #endif
  3697. }
  3698. void CTavernWindow::recruitb()
  3699. {
  3700. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3701. const CGObjectInstance *obj = tavernObj;
  3702. close();
  3703. LOCPLINT->cb->recruitHero(obj, toBuy);
  3704. }
  3705. void CTavernWindow::thievesguildb()
  3706. {
  3707. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3708. }
  3709. CTavernWindow::~CTavernWindow()
  3710. {
  3711. CCS->videoh->close();
  3712. }
  3713. void CTavernWindow::close()
  3714. {
  3715. GH.popIntTotally(this);
  3716. }
  3717. void CTavernWindow::show(SDL_Surface * to)
  3718. {
  3719. CIntObject::show(to);
  3720. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3721. if(selected >= 0)
  3722. {
  3723. HeroPortrait *sel = selected ? h2 : h1;
  3724. if (selected != oldSelected && !recruit->isBlocked())
  3725. {
  3726. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3727. oldSelected = selected;
  3728. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3729. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3730. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3731. }
  3732. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,zwykly,to);
  3733. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3734. }
  3735. }
  3736. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3737. {
  3738. if(previousState && !down && h)
  3739. as();
  3740. //ClickableL::clickLeft(down);
  3741. }
  3742. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3743. {
  3744. if(down && h)
  3745. {
  3746. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3747. }
  3748. }
  3749. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3750. :as(sel,id), h(H)
  3751. {
  3752. used = LCLICK | RCLICK | HOVER;
  3753. h = H;
  3754. pos.x = x;
  3755. pos.y = y;
  3756. pos.w = 58;
  3757. pos.h = 64;
  3758. if(H)
  3759. {
  3760. hoverName = CGI->generaltexth->tavernInfo[4];
  3761. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3762. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3763. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3764. if(vstd::contains(h->artifactsWorn,i))
  3765. artifs--;
  3766. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3767. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3768. descr[sizeof(descr)-1] = '\0';
  3769. }
  3770. }
  3771. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  3772. {
  3773. blitAt(graphics->portraitLarge[h->subID],pos,to);
  3774. }
  3775. void CTavernWindow::HeroPortrait::hover( bool on )
  3776. {
  3777. //Hoverable::hover(on);
  3778. if(on)
  3779. GH.statusbar->print(hoverName);
  3780. else
  3781. GH.statusbar->clear();
  3782. }
  3783. void CInGameConsole::activate()
  3784. {
  3785. activateKeys();
  3786. }
  3787. void CInGameConsole::deactivate()
  3788. {
  3789. deactivateKeys();
  3790. }
  3791. void CInGameConsole::show(SDL_Surface * to)
  3792. {
  3793. int number = 0;
  3794. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3795. texts_mx.lock();
  3796. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3797. {
  3798. SDL_Color green = {0,0xff,0,0};
  3799. Point leftBottomCorner(0, screen->h);
  3800. if(LOCPLINT->battleInt)
  3801. {
  3802. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3803. }
  3804. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3805. if(SDL_GetTicks() - it->second > defaultTimeout)
  3806. {
  3807. toDel.push_back(it);
  3808. }
  3809. }
  3810. for(int it=0; it<toDel.size(); ++it)
  3811. {
  3812. texts.erase(toDel[it]);
  3813. }
  3814. texts_mx.unlock();
  3815. }
  3816. void CInGameConsole::print(const std::string &txt)
  3817. {
  3818. texts_mx.lock();
  3819. int lineLen = conf.go()->ac.outputLineLength;
  3820. if(txt.size() < lineLen)
  3821. {
  3822. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3823. if(texts.size() > maxDisplayedTexts)
  3824. {
  3825. texts.pop_front();
  3826. }
  3827. }
  3828. else
  3829. {
  3830. assert(lineLen);
  3831. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3832. {
  3833. std::string part = txt.substr(g * lineLen, lineLen);
  3834. if(part.size() == 0)
  3835. break;
  3836. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3837. if(texts.size() > maxDisplayedTexts)
  3838. {
  3839. texts.pop_front();
  3840. }
  3841. }
  3842. }
  3843. texts_mx.unlock();
  3844. }
  3845. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3846. {
  3847. if(key.type != SDL_KEYDOWN) return;
  3848. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3849. switch(key.keysym.sym)
  3850. {
  3851. case SDLK_TAB:
  3852. case SDLK_ESCAPE:
  3853. {
  3854. if(captureAllKeys)
  3855. {
  3856. captureAllKeys = false;
  3857. endEnteringText(false);
  3858. }
  3859. else if(SDLK_TAB)
  3860. {
  3861. captureAllKeys = true;
  3862. startEnteringText();
  3863. }
  3864. break;
  3865. }
  3866. case SDLK_RETURN: //enter key
  3867. {
  3868. if(enteredText.size() > 0 && captureAllKeys)
  3869. {
  3870. captureAllKeys = false;
  3871. endEnteringText(true);
  3872. }
  3873. break;
  3874. }
  3875. case SDLK_BACKSPACE:
  3876. {
  3877. if(enteredText.size() > 1)
  3878. {
  3879. enteredText.resize(enteredText.size()-1);
  3880. enteredText[enteredText.size()-1] = '_';
  3881. refreshEnteredText();
  3882. }
  3883. break;
  3884. }
  3885. case SDLK_UP: //up arrow
  3886. {
  3887. if(previouslyEntered.size() == 0)
  3888. break;
  3889. if(prevEntDisp == -1)
  3890. {
  3891. prevEntDisp = previouslyEntered.size() - 1;
  3892. enteredText = previouslyEntered[prevEntDisp] + "_";
  3893. refreshEnteredText();
  3894. }
  3895. else if( prevEntDisp > 0)
  3896. {
  3897. --prevEntDisp;
  3898. enteredText = previouslyEntered[prevEntDisp] + "_";
  3899. refreshEnteredText();
  3900. }
  3901. break;
  3902. }
  3903. case SDLK_DOWN: //down arrow
  3904. {
  3905. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3906. {
  3907. ++prevEntDisp;
  3908. enteredText = previouslyEntered[prevEntDisp] + "_";
  3909. refreshEnteredText();
  3910. }
  3911. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3912. {
  3913. prevEntDisp = -1;
  3914. enteredText = "_";
  3915. refreshEnteredText();
  3916. }
  3917. break;
  3918. }
  3919. default:
  3920. {
  3921. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3922. {
  3923. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3924. {
  3925. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3926. enteredText += "_";
  3927. refreshEnteredText();
  3928. }
  3929. }
  3930. break;
  3931. }
  3932. }
  3933. }
  3934. void CInGameConsole::startEnteringText()
  3935. {
  3936. enteredText = "_";
  3937. if(GH.topInt() == adventureInt)
  3938. {
  3939. GH.statusbar->print(enteredText);
  3940. }
  3941. else if(LOCPLINT->battleInt)
  3942. {
  3943. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3944. }
  3945. }
  3946. void CInGameConsole::endEnteringText(bool printEnteredText)
  3947. {
  3948. prevEntDisp = -1;
  3949. if(printEnteredText)
  3950. {
  3951. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3952. LOCPLINT->cb->sendMessage(txt);
  3953. previouslyEntered.push_back(txt);
  3954. print(txt);
  3955. }
  3956. enteredText = "";
  3957. if(GH.topInt() == adventureInt)
  3958. {
  3959. GH.statusbar->clear();
  3960. }
  3961. else if(LOCPLINT->battleInt)
  3962. {
  3963. LOCPLINT->battleInt->console->ingcAlter = "";
  3964. }
  3965. }
  3966. void CInGameConsole::refreshEnteredText()
  3967. {
  3968. if(GH.topInt() == adventureInt)
  3969. {
  3970. GH.statusbar->print(enteredText);
  3971. }
  3972. else if(LOCPLINT->battleInt)
  3973. {
  3974. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3975. }
  3976. }
  3977. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3978. {
  3979. }
  3980. void CGarrisonWindow::close()
  3981. {
  3982. GH.popIntTotally(this);
  3983. }
  3984. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3985. {
  3986. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3987. bg = new CPicture("GARRISON.bmp");
  3988. bg->colorizeAndConvert(LOCPLINT->playerID);
  3989. pos = bg->center();
  3990. garr = new CGarrisonInt(92, 127, 4, Point(0,96), bg->bg, Point(93,127), up, down, removableUnits);
  3991. {
  3992. AdventureMapButton *split = new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3993. removeChild(split);
  3994. garr->addSplitBtn(split);
  3995. }
  3996. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3997. }
  3998. CGarrisonWindow::~CGarrisonWindow()
  3999. {
  4000. }
  4001. void CGarrisonWindow::showAll(SDL_Surface * to)
  4002. {
  4003. CIntObject::showAll(to);
  4004. std::string title;
  4005. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  4006. title = CGI->generaltexth->allTexts[709];
  4007. else
  4008. {
  4009. title = CGI->generaltexth->allTexts[35];
  4010. boost::algorithm::replace_first(title, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  4011. }
  4012. blitAtLoc(graphics->flags->ourImages[garr->armedObjs[1]->getOwner()].bitmap,28,124,to);
  4013. blitAtLoc(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait],29,222,to);
  4014. printAtMiddleLoc(title,275,30,FONT_BIG,tytulowy,to);
  4015. }
  4016. IShowActivable::IShowActivable()
  4017. {
  4018. type = 0;
  4019. }
  4020. CGarrisonHolder::CGarrisonHolder()
  4021. {
  4022. type |= WITH_GARRISON;
  4023. }
  4024. void CWindowWithGarrison::updateGarrisons()
  4025. {
  4026. garr->recreateSlots();
  4027. }
  4028. void CRClickPopupInt::show(SDL_Surface * to)
  4029. {
  4030. inner->show(to);
  4031. }
  4032. CRClickPopupInt::CRClickPopupInt( IShowActivable *our, bool deleteInt )
  4033. {
  4034. CCS->curh->hide();
  4035. inner = our;
  4036. delInner = deleteInt;
  4037. }
  4038. CRClickPopupInt::~CRClickPopupInt()
  4039. {
  4040. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  4041. // //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...?)
  4042. // if(LOCPLINT && inner == adventureInt->heroWindow)
  4043. // adventureInt->heroWindow->dispose();
  4044. if(delInner)
  4045. delete inner;
  4046. CCS->curh->show();
  4047. }
  4048. void CRClickPopupInt::showAll(SDL_Surface * to)
  4049. {
  4050. inner->showAll(to);
  4051. }
  4052. CArtPlace::CArtPlace(const CArtifactInstance* Art)
  4053. :picked(false), marked(false), locked(false), ourArt(Art)
  4054. {
  4055. }
  4056. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  4057. picked(false), marked(false), locked(false), ourArt(Art)
  4058. {
  4059. pos += position;
  4060. pos.w = pos.h = 44;
  4061. }
  4062. void CArtPlace::activate()
  4063. {
  4064. if(!active)
  4065. {
  4066. LRClickableAreaWTextComp::activate();
  4067. }
  4068. }
  4069. void CArtPlace::clickLeft(tribool down, bool previousState)
  4070. {
  4071. //LRClickableAreaWTextComp::clickLeft(down);
  4072. bool inBackpack = slotID >= Arts::BACKPACK_START,
  4073. srcInBackpack = ourOwner->commonInfo->src.slotID >= Arts::BACKPACK_START,
  4074. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  4075. if(ourOwner->highlightModeCallback && ourArt)
  4076. {
  4077. if(down)
  4078. {
  4079. if(ourArt->artType->id < 7) //War Machine or Spellbook
  4080. {
  4081. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  4082. }
  4083. else
  4084. {
  4085. ourOwner->unmarkSlots(false);
  4086. marked = true;
  4087. ourOwner->highlightModeCallback(this);
  4088. }
  4089. }
  4090. return;
  4091. }
  4092. // If clicked on spellbook, open it only if no artifact is held at the moment.
  4093. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  4094. {
  4095. if(ourArt->artType->id == 0)
  4096. {
  4097. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  4098. GH.pushInt(spellWindow);
  4099. }
  4100. }
  4101. if (!down && previousState)
  4102. {
  4103. if(ourArt && ourArt->artType->id == 0) //spellbook
  4104. return; //this is handled separately
  4105. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  4106. {
  4107. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  4108. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  4109. {
  4110. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  4111. {
  4112. std::vector<SComponent *> catapult(1, new SComponent(SComponent::artifact, 3, 0));
  4113. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  4114. return;
  4115. }
  4116. select();
  4117. }
  4118. }
  4119. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  4120. {
  4121. deselect();
  4122. }
  4123. else //perform artifact transition
  4124. {
  4125. if(inBackpack) // Backpack destination.
  4126. {
  4127. 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
  4128. {
  4129. deselect();
  4130. }
  4131. else
  4132. {
  4133. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  4134. switch(cur->id)
  4135. {
  4136. case 3:
  4137. //should not happen, catapult cannot be selected
  4138. assert(cur->id != 3);
  4139. break;
  4140. case 4: case 5: case 6: //war machines cannot go to backpack
  4141. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  4142. break;
  4143. default:
  4144. setMeAsDest();
  4145. amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + Arts::BACKPACK_START);
  4146. if(srcInBackpack && srcInSameHero)
  4147. {
  4148. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with amin above it will guarantee that dest is at most the last artifact
  4149. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  4150. ourOwner->commonInfo->dst.slotID--;
  4151. }
  4152. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  4153. deselect();
  4154. else
  4155. ourOwner->realizeCurrentTransaction();
  4156. break;
  4157. }
  4158. }
  4159. }
  4160. //check if swap is possible
  4161. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  4162. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  4163. {
  4164. setMeAsDest();
  4165. //
  4166. // // Special case when the dest artifact can't be fit into the src slot.
  4167. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  4168. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  4169. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  4170. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  4171. // {
  4172. // // Put dest artifact into owner's backpack.
  4173. // ourOwner->commonInfo->src.AOH = ourOwner;
  4174. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  4175. // }
  4176. ourOwner->realizeCurrentTransaction();
  4177. }
  4178. }
  4179. }
  4180. }
  4181. void CArtPlace::clickRight(tribool down, bool previousState)
  4182. {
  4183. if(down && ourArt && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  4184. {
  4185. if (slotID < 19)
  4186. {
  4187. if(ourOwner->allowedAssembling)
  4188. {
  4189. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  4190. // If the artifact can be assembled, display dialog.
  4191. BOOST_FOREACH(const CArtifact *combination, assemblyPossibilities)
  4192. {
  4193. LOCPLINT->showArtifactAssemblyDialog(
  4194. ourArt->artType->id,
  4195. combination->id,
  4196. true,
  4197. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, true, combination->id),
  4198. 0);
  4199. if(assemblyPossibilities.size() > 2)
  4200. {
  4201. tlog3 << "More than one possibility of assembling... taking only first\n";
  4202. break;
  4203. }
  4204. return;
  4205. }
  4206. // Otherwise if the artifact can be diasassembled, display dialog.
  4207. if(ourArt->canBeDisassembled())
  4208. {
  4209. LOCPLINT->showArtifactAssemblyDialog(
  4210. ourArt->artType->id,
  4211. 0,
  4212. false,
  4213. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, false, 0),
  4214. 0);
  4215. return;
  4216. }
  4217. }
  4218. }
  4219. // Lastly just show the artifact description.
  4220. LRClickableAreaWTextComp::clickRight(down, previousState);
  4221. }
  4222. }
  4223. /**
  4224. * Selects artifact slot so that the containing artifact looks like it's picked up.
  4225. */
  4226. void CArtPlace::select ()
  4227. {
  4228. if (locked)
  4229. return;
  4230. picked = true;
  4231. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  4232. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->artType->id].bitmap);
  4233. ourOwner->commonInfo->src.setTo(this, false);
  4234. ourOwner->markPossibleSlots(ourArt);
  4235. if(slotID >= Arts::BACKPACK_START)
  4236. ourOwner->scrollBackpack(0); //will update slots
  4237. ourOwner->updateParentWindow();
  4238. ourOwner->safeRedraw();
  4239. }
  4240. /**
  4241. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  4242. */
  4243. void CArtPlace::deselect ()
  4244. {
  4245. picked = false;
  4246. CCS->curh->dragAndDropCursor(NULL);
  4247. ourOwner->unmarkSlots();
  4248. ourOwner->commonInfo->src.clear();
  4249. if(slotID >= Arts::BACKPACK_START)
  4250. ourOwner->scrollBackpack(0); //will update slots
  4251. ourOwner->updateParentWindow();
  4252. ourOwner->safeRedraw();
  4253. }
  4254. void CArtPlace::deactivate()
  4255. {
  4256. if(active)
  4257. {
  4258. LRClickableAreaWTextComp::deactivate();
  4259. }
  4260. }
  4261. void CArtPlace::showAll(SDL_Surface *to)
  4262. {
  4263. 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
  4264. {
  4265. int graphic = locked ? 145 : ourArt->artType->id;
  4266. blitAt(graphics->artDefs->ourImages[graphic].bitmap, pos.x, pos.y, to);
  4267. }
  4268. if(marked && active)
  4269. {
  4270. // Draw vertical bars.
  4271. for (int i = 0; i < pos.h; ++i)
  4272. {
  4273. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  4274. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  4275. }
  4276. // Draw horizontal bars.
  4277. for (int i = 0; i < pos.w; ++i)
  4278. {
  4279. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  4280. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  4281. }
  4282. }
  4283. }
  4284. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  4285. {
  4286. // You can place 'no artifact' anywhere.
  4287. if(!art)
  4288. return true;
  4289. // Anything can but War Machines can be placed in backpack.
  4290. if (slotID >= 19)
  4291. return !CGI->arth->isBigArtifact(art->id);
  4292. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  4293. }
  4294. CArtPlace::~CArtPlace()
  4295. {
  4296. deactivate();
  4297. }
  4298. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  4299. {
  4300. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  4301. }
  4302. void CArtPlace::setArtifact(const CArtifactInstance *art)
  4303. {
  4304. baseType = -1; //by default we don't store any component
  4305. ourArt = art;
  4306. if(!art)
  4307. {
  4308. text = std::string();
  4309. hoverText = CGI->generaltexth->allTexts[507];
  4310. }
  4311. else
  4312. {
  4313. text = ourArt->artType->Description();
  4314. if(art->artType->id == 1) //spell scroll
  4315. {
  4316. // 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.
  4317. // so we want to replace text in [...] with a spell name
  4318. // however other language versions don't have name placeholder at all, so we have to be careful
  4319. int spellID = art->getGivenSpellID();
  4320. int nameStart = text.find_first_of('[');
  4321. int nameEnd = text.find_first_of(']', nameStart);
  4322. if(spellID >= 0)
  4323. {
  4324. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  4325. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  4326. //add spell component info (used to provide a pic in r-click popup)
  4327. baseType = SComponent::spell;
  4328. type = spellID;
  4329. bonusValue = 0;
  4330. }
  4331. }
  4332. if (locked) // Locks should appear as empty.
  4333. hoverText = CGI->generaltexth->allTexts[507];
  4334. else
  4335. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  4336. }
  4337. }
  4338. void HoverableArea::hover (bool on)
  4339. {
  4340. if (on)
  4341. GH.statusbar->print(hoverText);
  4342. else if (GH.statusbar->getCurrent()==hoverText)
  4343. GH.statusbar->clear();
  4344. }
  4345. HoverableArea::HoverableArea()
  4346. {
  4347. used |= HOVER;
  4348. }
  4349. HoverableArea::~HoverableArea()
  4350. {
  4351. }
  4352. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  4353. {
  4354. if(!down && previousState)
  4355. {
  4356. LOCPLINT->showInfoDialog(text);
  4357. }
  4358. }
  4359. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  4360. {
  4361. adventureInt->handleRightClick(text, down);
  4362. }
  4363. LRClickableAreaWText::LRClickableAreaWText()
  4364. {
  4365. init();
  4366. }
  4367. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText /*= ""*/, const std::string &ClickText /*= ""*/)
  4368. {
  4369. init();
  4370. pos = Pos + pos;
  4371. hoverText = HoverText;
  4372. text = ClickText;
  4373. }
  4374. LRClickableAreaWText::~LRClickableAreaWText()
  4375. {
  4376. }
  4377. void LRClickableAreaWText::init()
  4378. {
  4379. used = LCLICK | RCLICK | HOVER;
  4380. }
  4381. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  4382. {
  4383. if((!down) && previousState)
  4384. {
  4385. std::vector<SComponent*> comp(1, createComponent());
  4386. LOCPLINT->showInfoDialog(text, comp);
  4387. }
  4388. }
  4389. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  4390. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  4391. {
  4392. }
  4393. SComponent * LRClickableAreaWTextComp::createComponent() const
  4394. {
  4395. if(baseType >= 0)
  4396. return new SComponent(SComponent::Etype(baseType), type, bonusValue);
  4397. else
  4398. return NULL;
  4399. }
  4400. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  4401. {
  4402. if(down)
  4403. {
  4404. if(SComponent *comp = createComponent())
  4405. {
  4406. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  4407. return;
  4408. }
  4409. }
  4410. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occured
  4411. }
  4412. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  4413. {
  4414. used = LCLICK | RCLICK | HOVER;
  4415. pos.x += x; pos.w = 58;
  4416. pos.y += y; pos.h = 64;
  4417. }
  4418. void CHeroArea::clickLeft(tribool down, bool previousState)
  4419. {
  4420. if((!down) && previousState && hero)
  4421. LOCPLINT->openHeroWindow(hero);
  4422. }
  4423. void CHeroArea::clickRight(tribool down, bool previousState)
  4424. {
  4425. if((!down) && previousState && hero)
  4426. LOCPLINT->openHeroWindow(hero);
  4427. }
  4428. void CHeroArea::hover(bool on)
  4429. {
  4430. if (on && hero)
  4431. GH.statusbar->print(hero->hoverName);
  4432. else
  4433. GH.statusbar->clear();
  4434. }
  4435. void CHeroArea::showAll(SDL_Surface * to)
  4436. {
  4437. if (hero)
  4438. blitAtLoc(graphics->portraitLarge[hero->portrait],0,0,to);
  4439. }
  4440. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  4441. {
  4442. if((!down) && previousState && town)
  4443. {
  4444. LOCPLINT->openTownWindow(town);
  4445. if ( type == 2 )
  4446. LOCPLINT->castleInt->builds->buildingClicked(10);
  4447. else if ( type == 3 && town->fortLevel() )
  4448. LOCPLINT->castleInt->builds->buildingClicked(7);
  4449. }
  4450. }
  4451. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  4452. {
  4453. if((!down) && previousState && town)
  4454. LOCPLINT->openTownWindow(town);//TODO: popup?
  4455. }
  4456. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  4457. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  4458. {
  4459. }
  4460. void CArtifactsOfHero::SCommonPart::reset()
  4461. {
  4462. src.clear();
  4463. dst.clear();
  4464. CCS->curh->dragAndDropCursor(NULL);
  4465. }
  4466. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  4467. {
  4468. // // An update is made, rather than initialization.
  4469. // if (curHero && curHero->id == hero->id)
  4470. // {
  4471. // if(curHero != hero)
  4472. // {
  4473. // //delete curHero;
  4474. // curHero = hero; //was: creating a copy
  4475. // }
  4476. //
  4477. // // Compensate backpack pos if an artifact was insertad before it.
  4478. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  4479. // && commonInfo->dst.slotID - 19 < backpackPos)
  4480. // {
  4481. // backpackPos++;
  4482. // }
  4483. //
  4484. // if (updateState && commonInfo->srcAOH == this)
  4485. // {
  4486. // // A swap was made, make the replaced artifact the current selected.
  4487. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  4488. // {
  4489. // // // Temporarily remove artifact from hero.
  4490. // // if (commonInfo->srcSlotID < 19)
  4491. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  4492. // // else
  4493. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  4494. //
  4495. // updateParentWindow(); //TODO: evil! but does the thing
  4496. //
  4497. // // Source <- Dest
  4498. // commonInfo->srcArtifact = commonInfo->destArtifact;
  4499. //
  4500. // // Reset destination parameters.
  4501. // commonInfo->dst.clear();
  4502. //
  4503. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  4504. // markPossibleSlots(commonInfo->srcArtifact);
  4505. // }
  4506. // else if (commonInfo->destAOH != NULL)
  4507. // {
  4508. // // Reset all parameters.
  4509. // commonInfo->reset();
  4510. // unmarkSlots();
  4511. // }
  4512. // }
  4513. // }
  4514. // else
  4515. // {
  4516. // commonInfo->reset();
  4517. // }
  4518. //
  4519. // if(hero != curHero)
  4520. // {
  4521. // // delete curHero;
  4522. // // curHero = new CGHeroInstance(*hero);
  4523. // curHero = hero; //was: creating a copy
  4524. // }
  4525. curHero = hero;
  4526. if (curHero->artifactsInBackpack.size() > 0)
  4527. backpackPos %= curHero->artifactsInBackpack.size();
  4528. else
  4529. backpackPos = 0;
  4530. // Fill the slots for worn artifacts and backpack.
  4531. for (int g = 0; g < artWorn.size() ; g++)
  4532. setSlotData(artWorn[g], g);
  4533. scrollBackpack(0);
  4534. }
  4535. void CArtifactsOfHero::dispose()
  4536. {
  4537. //delNull(curHero);
  4538. unmarkSlots(false);
  4539. CCS->curh->dragAndDropCursor(NULL);
  4540. }
  4541. void CArtifactsOfHero::scrollBackpack(int dir)
  4542. {
  4543. int artsInBackpack = curHero->artifactsInBackpack.size();
  4544. backpackPos += dir;
  4545. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  4546. backpackPos += artsInBackpack;
  4547. if(artsInBackpack)
  4548. backpackPos %= artsInBackpack;
  4549. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  4550. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  4551. toOmit.insert(commonInfo->src.art);
  4552. int omitedSoFar = 0;
  4553. //set new data
  4554. size_t s = 0;
  4555. for( ; s < artsInBackpack; ++s)
  4556. {
  4557. if (s < artsInBackpack)
  4558. {
  4559. int slotID = 19 + (s + backpackPos)%artsInBackpack;
  4560. const CArtifactInstance *art = curHero->getArt(slotID);
  4561. assert(art);
  4562. if(!vstd::contains(toOmit, art))
  4563. {
  4564. if(s - omitedSoFar < backpack.size())
  4565. setSlotData(backpack[s-omitedSoFar], slotID);
  4566. }
  4567. else
  4568. {
  4569. toOmit -= art;
  4570. omitedSoFar++;
  4571. continue;
  4572. }
  4573. }
  4574. }
  4575. for( ; s - omitedSoFar < backpack.size(); s++)
  4576. eraseSlotData(backpack[s-omitedSoFar], 19 + s);
  4577. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  4578. if(highlightModeCallback)
  4579. {
  4580. for(int i = 0; i < backpack.size(); i++)
  4581. {
  4582. if(backpack[i]->marked)
  4583. {
  4584. highlightModeCallback(backpack[i]);
  4585. break;
  4586. }
  4587. }
  4588. }
  4589. //blocking scrolling if there is not enough artifacts to scroll
  4590. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  4591. leftArtRoll->block(!scrollingPossible);
  4592. rightArtRoll->block(!scrollingPossible);
  4593. safeRedraw();
  4594. }
  4595. /**
  4596. * Marks possible slots where a given artifact can be placed, except backpack.
  4597. *
  4598. * @param art Artifact checked against.
  4599. */
  4600. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  4601. {
  4602. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4603. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  4604. place->marked = art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true);
  4605. safeRedraw();
  4606. }
  4607. /**
  4608. * Unamarks all slots.
  4609. */
  4610. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  4611. {
  4612. if(commonInfo)
  4613. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4614. aoh->unmarkLocalSlots(false);
  4615. else
  4616. unmarkLocalSlots(false);\
  4617. if(withRedraw)
  4618. safeRedraw();
  4619. }
  4620. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw /*= true*/)
  4621. {
  4622. BOOST_FOREACH(CArtPlace *place, artWorn)
  4623. place->marked = false;
  4624. BOOST_FOREACH(CArtPlace *place, backpack)
  4625. place->marked = false;
  4626. if(withRedraw)
  4627. safeRedraw();
  4628. }
  4629. /**
  4630. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  4631. */
  4632. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, int slotID)
  4633. {
  4634. if(!artPlace && slotID >= Arts::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  4635. {
  4636. return;
  4637. }
  4638. artPlace->picked = false;
  4639. artPlace->slotID = slotID;
  4640. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  4641. {
  4642. artPlace->setArtifact(asi->artifact);
  4643. artPlace->locked = asi->locked;
  4644. }
  4645. else
  4646. artPlace->setArtifact(NULL);
  4647. }
  4648. /**
  4649. * Makes given artifact slot appear as empty with a certain slot ID.
  4650. */
  4651. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, int slotID)
  4652. {
  4653. artPlace->picked = false;
  4654. artPlace->slotID = slotID;
  4655. artPlace->setArtifact(NULL);
  4656. }
  4657. CArtifactsOfHero::CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  4658. AdventureMapButton *leftScroll, AdventureMapButton *rightScroll, bool createCommonPart):
  4659. curHero(NULL),
  4660. artWorn(ArtWorn), backpack(Backpack),
  4661. backpackPos(0), commonInfo(NULL), updateState(false),
  4662. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  4663. allowedAssembling(true), highlightModeCallback(0)
  4664. {
  4665. if(createCommonPart)
  4666. {
  4667. commonInfo = new CArtifactsOfHero::SCommonPart;
  4668. commonInfo->participants.insert(this);
  4669. }
  4670. // Init slots for worn artifacts.
  4671. for (size_t g = 0; g < artWorn.size() ; g++)
  4672. {
  4673. artWorn[g]->ourOwner = this;
  4674. eraseSlotData(artWorn[g], g);
  4675. }
  4676. // Init slots for the backpack.
  4677. for(size_t s=0; s<backpack.size(); ++s)
  4678. {
  4679. backpack[s]->ourOwner = this;
  4680. eraseSlotData(backpack[s], 19 + s);
  4681. }
  4682. leftArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1);
  4683. rightArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1);
  4684. }
  4685. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  4686. : curHero(NULL), backpackPos(0), commonInfo(NULL), updateState(false), allowedAssembling(true), highlightModeCallback(0)
  4687. {
  4688. if(createCommonPart)
  4689. {
  4690. commonInfo = new CArtifactsOfHero::SCommonPart;
  4691. commonInfo->participants.insert(this);
  4692. }
  4693. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4694. pos += position;
  4695. artWorn.resize(19);
  4696. std::vector<Rect> slotPos;
  4697. slotPos += genRect(44,44,509,30), genRect(44,44,567,240), genRect(44,44,509,80),
  4698. genRect(44,44,383,68), genRect(44,44,564,183), genRect(44,44,509,130),
  4699. genRect(44,44,431,68), genRect(44,44,610,183), genRect(44,44,515,295),
  4700. genRect(44,44,383,143), genRect(44,44,399,194), genRect(44,44,415,245),
  4701. genRect(44,44,431,296), genRect(44,44,564,30), genRect(44,44,610,30),
  4702. genRect(44,44,610,76), genRect(44,44,610,122), genRect(44,44,610,310),
  4703. genRect(44,44,381,296);
  4704. // Create slots for worn artifacts.
  4705. for (size_t g = 0; g < 19 ; g++)
  4706. {
  4707. artWorn[g] = new CArtPlace(NULL);
  4708. artWorn[g]->pos = slotPos[g] + pos;
  4709. artWorn[g]->ourOwner = this;
  4710. eraseSlotData(artWorn[g], g);
  4711. }
  4712. // Create slots for the backpack.
  4713. for(size_t s=0; s<5; ++s)
  4714. {
  4715. CArtPlace * add = new CArtPlace(NULL);
  4716. add->ourOwner = this;
  4717. add->pos.x = pos.x + 403 + 46*s;
  4718. add->pos.y = pos.y + 365;
  4719. add->pos.h = add->pos.w = 44;
  4720. eraseSlotData(add, 19 + s);
  4721. backpack.push_back(add);
  4722. }
  4723. leftArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  4724. rightArtRoll = new AdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  4725. }
  4726. CArtifactsOfHero::~CArtifactsOfHero()
  4727. {
  4728. dispose();
  4729. }
  4730. void CArtifactsOfHero::updateParentWindow()
  4731. {
  4732. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  4733. {
  4734. if(updateState)
  4735. chw->curHero = curHero;
  4736. else
  4737. chw->update(curHero, true);
  4738. }
  4739. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  4740. {
  4741. //use our copy of hero to draw window
  4742. if(cew->heroInst[0]->id == curHero->id)
  4743. cew->heroInst[0] = curHero;
  4744. else
  4745. cew->heroInst[1] = curHero;
  4746. if(!updateState)
  4747. {
  4748. cew->deactivate();
  4749. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  4750. // {
  4751. // if(cew->heroInst[g] == curHero)
  4752. // {
  4753. // cew->artifs[g]->setHero(curHero);
  4754. // }
  4755. // }
  4756. cew->prepareBackground();
  4757. cew->redraw();
  4758. cew->activate();
  4759. }
  4760. }
  4761. }
  4762. void CArtifactsOfHero::safeRedraw()
  4763. {
  4764. if(parent)
  4765. parent->redraw();
  4766. else
  4767. redraw();
  4768. }
  4769. void CArtifactsOfHero::realizeCurrentTransaction()
  4770. {
  4771. assert(commonInfo->src.AOH);
  4772. assert(commonInfo->dst.AOH);
  4773. LOCPLINT->cb->swapArtifacts(commonInfo->src.AOH->curHero, commonInfo->src.slotID,
  4774. commonInfo->dst.AOH->curHero, commonInfo->dst.slotID);
  4775. }
  4776. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4777. {
  4778. if(src.hero == curHero && src.slot >= Arts::BACKPACK_START)
  4779. updateSlot(src.slot);
  4780. if(dst.hero == curHero && dst.slot >= Arts::BACKPACK_START)
  4781. updateSlot(dst.slot);
  4782. if(src.hero == curHero || dst.hero == curHero) //we need to update all slots, artifact might be combined and affect more slots
  4783. updateWornSlots(false);
  4784. if (src.hero != curHero && dst.hero != curHero)
  4785. return;
  4786. if(commonInfo->src == src) //artifact was taken from us
  4787. {
  4788. assert(commonInfo->dst == dst || dst.slot == dst.hero->artifactsInBackpack.size() + Arts::BACKPACK_START);
  4789. commonInfo->reset();
  4790. unmarkSlots();
  4791. updateParentWindow();
  4792. }
  4793. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4794. {
  4795. assert(dst.slot >= Arts::BACKPACK_START);
  4796. commonInfo->reset();
  4797. CArtPlace *ap = NULL;
  4798. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4799. {
  4800. if(aoh->curHero == dst.hero)
  4801. {
  4802. commonInfo->src.AOH = aoh;
  4803. if((ap = aoh->getArtPlace(dst.slot)))
  4804. break;
  4805. }
  4806. }
  4807. if(ap)
  4808. {
  4809. ap->select();
  4810. }
  4811. else
  4812. {
  4813. commonInfo->src.art = dst.getArt();
  4814. commonInfo->src.slotID = dst.slot;
  4815. assert(commonInfo->src.AOH);
  4816. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[dst.getArt()->artType->id].bitmap);
  4817. markPossibleSlots(dst.getArt());
  4818. updateParentWindow();
  4819. }
  4820. }
  4821. else if(src.slot >= Arts::BACKPACK_START &&
  4822. src.slot < commonInfo->src.slotID &&
  4823. src.hero == commonInfo->src.AOH->curHero) //artifact taken from before currently picked one
  4824. {
  4825. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4826. commonInfo->src.slotID--;
  4827. assert(commonInfo->src.valid());
  4828. updateParentWindow();
  4829. }
  4830. else
  4831. {
  4832. tlog1 << "Unexpected artifact movement...\n";
  4833. }
  4834. int shift = 0;
  4835. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4836. // shift++;
  4837. //
  4838. if(src.slot < Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4839. shift++;
  4840. if(dst.slot < Arts::BACKPACK_START && src.slot - Arts::BACKPACK_START < backpackPos)
  4841. shift--;
  4842. if( (src.hero == curHero && src.slot >= Arts::BACKPACK_START)
  4843. || (dst.hero == curHero && dst.slot >= Arts::BACKPACK_START) )
  4844. scrollBackpack(shift); //update backpack slots
  4845. }
  4846. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  4847. {
  4848. if(al.hero == curHero)
  4849. {
  4850. if(al.slot < Arts::BACKPACK_START)
  4851. updateWornSlots(0);
  4852. else
  4853. scrollBackpack(0); //update backpack slots
  4854. }
  4855. }
  4856. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4857. {
  4858. if(slot < Arts::BACKPACK_START)
  4859. {
  4860. return artWorn[slot];
  4861. }
  4862. else
  4863. {
  4864. BOOST_FOREACH(CArtPlace *ap, backpack)
  4865. if(ap->slotID == slot)
  4866. return ap;
  4867. }
  4868. return NULL;
  4869. }
  4870. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4871. {
  4872. if(al.hero == curHero)
  4873. updateWornSlots();
  4874. }
  4875. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4876. {
  4877. if(al.hero == curHero)
  4878. updateWornSlots();
  4879. }
  4880. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4881. {
  4882. for(int i = 0; i < artWorn.size(); i++)
  4883. updateSlot(i);
  4884. if(redrawParent)
  4885. updateParentWindow();
  4886. }
  4887. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4888. {
  4889. return curHero;
  4890. }
  4891. void CArtifactsOfHero::updateSlot(int slotID)
  4892. {
  4893. setSlotData(getArtPlace(slotID), slotID);
  4894. }
  4895. void CExchangeWindow::close()
  4896. {
  4897. GH.popIntTotally(this);
  4898. }
  4899. void CExchangeWindow::activate()
  4900. {
  4901. quit->activate();
  4902. garr->activate();
  4903. //artifs[0]->setHero(heroInst[0]);
  4904. artifs[0]->activate();
  4905. //artifs[1]->setHero(heroInst[1]);
  4906. artifs[1]->activate();
  4907. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4908. {
  4909. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4910. {
  4911. secSkillAreas[g][b]->activate();
  4912. }
  4913. }
  4914. for(int b=0; b<primSkillAreas.size(); ++b)
  4915. primSkillAreas[b]->activate();
  4916. GH.statusbar = ourBar;
  4917. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4918. questlogButton[g]->activate();
  4919. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4920. morale[g]->activate();
  4921. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4922. luck[g]->activate();
  4923. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  4924. portrait[g]->activate();
  4925. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  4926. spellPoints[g]->activate();
  4927. for(int g=0; g<ARRAY_COUNT(experience); g++)
  4928. experience[g]->activate();
  4929. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  4930. speciality[g]->activate();
  4931. }
  4932. void CExchangeWindow::deactivate()
  4933. {
  4934. quit->deactivate();
  4935. garr->deactivate();
  4936. artifs[0]->deactivate();
  4937. artifs[1]->deactivate();
  4938. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4939. {
  4940. for(int b=0; b<secSkillAreas[g].size(); ++b)
  4941. {
  4942. secSkillAreas[g][b]->deactivate();
  4943. }
  4944. }
  4945. for(int b=0; b<primSkillAreas.size(); ++b)
  4946. primSkillAreas[b]->deactivate();
  4947. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  4948. questlogButton[g]->deactivate();
  4949. for(int g=0; g<ARRAY_COUNT(morale); g++)
  4950. morale[g]->deactivate();
  4951. for(int g=0; g<ARRAY_COUNT(luck); g++)
  4952. luck[g]->deactivate();
  4953. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  4954. portrait[g]->deactivate();
  4955. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  4956. spellPoints[g]->deactivate();
  4957. for(int g=0; g<ARRAY_COUNT(experience); g++)
  4958. experience[g]->deactivate();
  4959. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  4960. speciality[g]->deactivate();
  4961. }
  4962. void CExchangeWindow::show(SDL_Surface * to)
  4963. {
  4964. blitAt(bg, pos, to);
  4965. quit->showAll(to);
  4966. //printing border around window
  4967. if(screen->w != 800 || screen->h !=600)
  4968. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4969. artifs[0]->showAll(to);
  4970. artifs[1]->showAll(to);
  4971. ourBar->showAll(to);
  4972. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  4973. {
  4974. questlogButton[g]->showAll(to);//FIXME: for array count(secondary skill) show quest log button? WTF?
  4975. }
  4976. garr->showAll(to);
  4977. }
  4978. void CExchangeWindow::questlog(int whichHero)
  4979. {
  4980. CCS->curh->dragAndDropCursor(NULL);
  4981. }
  4982. void CExchangeWindow::prepareBackground()
  4983. {
  4984. if(bg)
  4985. SDL_FreeSurface(bg);
  4986. SDL_Surface * bgtemp; //loaded as 8bpp surface
  4987. bgtemp = BitmapHandler::loadBitmap("TRADE2.BMP");
  4988. graphics->blueToPlayersAdv(bgtemp, heroInst[0]->tempOwner);
  4989. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  4990. SDL_FreeSurface(bgtemp);
  4991. //printing heroes' names and levels
  4992. std::ostringstream os, os2;
  4993. os<<heroInst[0]->name<<", Level "<<heroInst[0]->level<<" "<<heroInst[0]->type->heroClass->name;
  4994. CSDL_Ext::printAtMiddle(os.str(), 147, 25, FONT_SMALL, zwykly, bg);
  4995. os2<<heroInst[1]->name<<", Level "<<heroInst[1]->level<<" "<<heroInst[1]->type->heroClass->name;
  4996. CSDL_Ext::printAtMiddle(os2.str(), 653, 25, FONT_SMALL, zwykly, bg);
  4997. //printing primary skills
  4998. CDefHandler * skilldef = CDefHandler::giveDef("PSKIL32.DEF");
  4999. for(int g=0; g<4; ++g)
  5000. {
  5001. //graphics
  5002. blitAt(skilldef->ourImages[g].bitmap, genRect(32, 32, 385, 19 + 36 * g), bg);
  5003. }
  5004. CDefHandler * un32 = CDefHandler::giveDef("UN32.DEF");
  5005. //heroes related thing
  5006. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  5007. {
  5008. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  5009. //printing primary skills' amounts
  5010. for(int m=0; m<4; ++m)
  5011. {
  5012. std::ostringstream primarySkill;
  5013. primarySkill << heroWArt.getPrimSkillLevel(m);
  5014. CSDL_Ext::printAtMiddle(primarySkill.str(), 352 + 93 * b, 35 + 36 * m, FONT_SMALL, zwykly, bg);
  5015. }
  5016. //printing secondary skills
  5017. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  5018. {
  5019. 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);
  5020. }
  5021. //hero's specialty
  5022. blitAt(un32->ourImages[heroInst[b]->subID].bitmap, 67 + 490*b, 45, bg);
  5023. //experience
  5024. blitAt(skilldef->ourImages[4].bitmap, 103 + 490*b, 45, bg);
  5025. printAtMiddle( makeNumberShort(heroInst[b]->exp), 119 + 490*b, 71, FONT_SMALL, zwykly, bg );
  5026. //mana points
  5027. blitAt(skilldef->ourImages[5].bitmap, 139 + 490*b, 45, bg);
  5028. printAtMiddle( makeNumberShort(heroInst[b]->mana), 155 + 490*b, 71, FONT_SMALL, zwykly, bg );
  5029. //setting morale
  5030. blitAt(graphics->morale30->ourImages[heroWArt.MoraleVal()+3].bitmap, 177 + 490*b, 45, bg);
  5031. //setting luck
  5032. blitAt(graphics->luck30->ourImages[heroWArt.LuckVal()+3].bitmap, 213 + 490*b, 45, bg);
  5033. }
  5034. //printing portraits
  5035. blitAt(graphics->portraitLarge[heroInst[0]->portrait], 257, 13, bg);
  5036. blitAt(graphics->portraitLarge[heroInst[1]->portrait], 485, 13, bg);
  5037. delete un32;
  5038. delete skilldef;
  5039. }
  5040. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) : bg(NULL)
  5041. {
  5042. char bufor[400];
  5043. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  5044. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  5045. prepareBackground();
  5046. pos.x = screen->w/2 - bg->w/2;
  5047. pos.y = screen->h/2 - bg->h/2;
  5048. pos.w = screen->w;
  5049. pos.h = screen->h;
  5050. artifs[0] = new CArtifactsOfHero(Point(pos.x + -334, pos.y + 150));
  5051. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  5052. artifs[0]->commonInfo->participants.insert(artifs[0]);
  5053. artifs[0]->setHero(heroInst[0]);
  5054. artifs[1] = new CArtifactsOfHero(Point(pos.x + 96, pos.y + 150));
  5055. artifs[1]->commonInfo = artifs[0]->commonInfo;
  5056. artifs[1]->commonInfo->participants.insert(artifs[1]);
  5057. artifs[1]->setHero(heroInst[1]);
  5058. artSets.push_back(artifs[0]);
  5059. artSets.push_back(artifs[1]);
  5060. //primary skills
  5061. for(int g=0; g<4; ++g)
  5062. {
  5063. //primary skill's clickable areas
  5064. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  5065. primSkillAreas[g]->pos = genRect(32, 140, pos.x+329, pos.y + 19 + 36 * g);
  5066. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  5067. primSkillAreas[g]->type = g;
  5068. primSkillAreas[g]->bonusValue = -1;
  5069. primSkillAreas[g]->baseType = 0;
  5070. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  5071. primSkillAreas[g]->hoverText = std::string(bufor);
  5072. }
  5073. //heroes related thing
  5074. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  5075. {
  5076. //secondary skill's clickable areas
  5077. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  5078. {
  5079. int skill = heroInst[b]->secSkills[g].first,
  5080. level = heroInst[b]->secSkills[g].second; // <1, 3>
  5081. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  5082. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  5083. secSkillAreas[b][g]->baseType = 1;
  5084. secSkillAreas[b][g]->type = skill;
  5085. secSkillAreas[b][g]->bonusValue = level;
  5086. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  5087. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  5088. secSkillAreas[b][g]->hoverText = std::string(bufor);
  5089. }
  5090. portrait[b] = new CHeroArea(pos.x + 257 + 228*b, pos.y + 13, heroInst[b]);
  5091. speciality[b] = new LRClickableAreaWText();
  5092. speciality[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  5093. speciality[b]->hoverText = CGI->generaltexth->heroscrn[27];
  5094. speciality[b]->text = CGI->generaltexth->hTxts[heroInst[b]->subID].longBonus;
  5095. experience[b] = new LRClickableAreaWText();
  5096. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  5097. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  5098. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  5099. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  5100. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  5101. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  5102. spellPoints[b] = new LRClickableAreaWText();
  5103. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  5104. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  5105. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  5106. spellPoints[b]->text = std::string(bufor);
  5107. //setting morale
  5108. morale[b] = new MoraleLuckBox(true, genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45));
  5109. morale[b]->set(heroInst[b]);
  5110. //setting luck
  5111. luck[b] = new MoraleLuckBox(false, genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45));
  5112. luck[b]->set(heroInst[b]);
  5113. }
  5114. //buttons
  5115. quit = new AdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  5116. questlogButton[0] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  5117. questlogButton[1] = new AdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  5118. //statusbar
  5119. //FIXME - this image is a bit bigger than required - part of background should be used instead
  5120. ourBar = new CGStatusBar(pos.x + 3, pos.y + 577, "KSTATBAR");
  5121. //garrison interface
  5122. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  5123. garr->addSplitBtn(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  5124. garr->addSplitBtn(new AdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  5125. }
  5126. CExchangeWindow::~CExchangeWindow() //d-tor
  5127. {
  5128. SDL_FreeSurface(bg);
  5129. delete quit;
  5130. //warning: don't experiment with these =NULL lines, they prevent heap corruption!
  5131. delete artifs[0]->commonInfo;
  5132. artifs[0]->commonInfo = NULL;
  5133. delete artifs[0];
  5134. artifs[1]->commonInfo = NULL;
  5135. delete artifs[1];
  5136. delete garr;
  5137. delete ourBar;
  5138. for(int g=0; g<ARRAY_COUNT(secSkillAreas); g++)
  5139. {
  5140. for(int b=0; b<secSkillAreas[g].size(); ++b)
  5141. {
  5142. delete secSkillAreas[g][b];
  5143. }
  5144. }
  5145. for(int b=0; b<primSkillAreas.size(); ++b)
  5146. {
  5147. delete primSkillAreas[b];
  5148. }
  5149. for(int g=0; g<ARRAY_COUNT(questlogButton); g++)
  5150. {
  5151. delete questlogButton[g];
  5152. }
  5153. for(int g=0; g<ARRAY_COUNT(morale); g++)
  5154. delete morale[g];
  5155. for(int g=0; g<ARRAY_COUNT(luck); g++)
  5156. delete luck[g];
  5157. for(int g=0; g<ARRAY_COUNT(portrait); g++)
  5158. delete portrait[g];
  5159. for(int g=0; g<ARRAY_COUNT(spellPoints); g++)
  5160. delete spellPoints[g];
  5161. for(int g=0; g<ARRAY_COUNT(experience); g++)
  5162. delete experience[g];
  5163. for(int g=0; g<ARRAY_COUNT(speciality); g++)
  5164. delete speciality[g];
  5165. }
  5166. void CShipyardWindow::activate()
  5167. {
  5168. build->activate();
  5169. quit->activate();
  5170. }
  5171. void CShipyardWindow::deactivate()
  5172. {
  5173. build->deactivate();
  5174. quit->deactivate();
  5175. }
  5176. void CShipyardWindow::show( SDL_Surface * to )
  5177. {
  5178. blitAt(bg,pos,to);
  5179. Rect clipRect = genRect(64, 96, pos.x+110, pos.y+85);
  5180. CSDL_Ext::blit8bppAlphaTo24bpp(graphics->boatAnims[boat]->ourImages[21 + frame++/8%8].bitmap, NULL, to, &clipRect);
  5181. build->showAll(to);
  5182. quit->showAll(to);
  5183. }
  5184. CShipyardWindow::~CShipyardWindow()
  5185. {
  5186. delete build;
  5187. delete quit;
  5188. }
  5189. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy)
  5190. {
  5191. boat = boatType;
  5192. frame = 0;
  5193. SDL_Surface * bgtemp; //loaded as 8bpp surface
  5194. bgtemp = BitmapHandler::loadBitmap("TPSHIP.bmp");
  5195. pos.x = screen->w/2 - bgtemp->w/2;
  5196. pos.y = screen->h/2 - bgtemp->h/2;
  5197. pos.w = bgtemp->w;
  5198. pos.h = bgtemp->h;
  5199. SDL_SetColorKey(bgtemp,SDL_SRCCOLORKEY,SDL_MapRGB(bgtemp->format,0,255,255));
  5200. graphics->blueToPlayersAdv(bgtemp, LOCPLINT->playerID);
  5201. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  5202. SDL_FreeSurface(bgtemp);
  5203. bgtemp = BitmapHandler::loadBitmap("TPSHIPBK.bmp");
  5204. blitAt(bgtemp, 100, 69, bg);
  5205. SDL_FreeSurface(bgtemp);
  5206. // Draw resource icons and costs.
  5207. std::string goldCost = boost::lexical_cast<std::string>(1000);
  5208. std::string woodCost = boost::lexical_cast<std::string>(10);
  5209. blitAt(graphics->resources32->ourImages[6].bitmap, 100, 244, bg);
  5210. printAtMiddle(goldCost.c_str(), 118, 294, FONT_SMALL, zwykly, bg);
  5211. blitAt(graphics->resources32->ourImages[0].bitmap, 196, 244, bg);
  5212. printAtMiddle(woodCost.c_str(), 212, 294, FONT_SMALL, zwykly, bg);
  5213. bool affordable = true;
  5214. for(int i = 0; i < cost.size(); i++)
  5215. {
  5216. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  5217. {
  5218. affordable = false;
  5219. break;
  5220. }
  5221. }
  5222. quit = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+224, pos.y+312, "ICANCEL.DEF", SDLK_RETURN);
  5223. build = new AdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+42, pos.y+312, "IBY6432.DEF", SDLK_RETURN);
  5224. build->callback += onBuy;
  5225. if(!affordable)
  5226. build->block(true);
  5227. printAtMiddle(CGI->generaltexth->jktexts[13], 164, 27, FONT_BIG, tytulowy, bg); //Build A New Ship
  5228. printAtMiddle(CGI->generaltexth->jktexts[14], 164, 220, FONT_MEDIUM, zwykly, bg); //Resource cost:
  5229. }
  5230. CPuzzleWindow::CPuzzleWindow(const int3 &grailPos, float discoveredRatio)
  5231. :animCount(0)
  5232. {
  5233. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  5234. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  5235. //make transparency black
  5236. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  5237. //the rest
  5238. background = SDL_ConvertSurface(back, screen->format, back->flags);
  5239. SDL_FreeSurface(back);
  5240. pos = genRect(background->h, background->w, (conf.cc.resx - background->w) / 2, (conf.cc.resy - background->h) / 2);
  5241. quitb = new AdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  5242. quitb->assignedKeys.insert(SDLK_ESCAPE);
  5243. quitb->borderColor = Colors::MetallicGold;
  5244. quitb->borderEnabled = true;
  5245. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  5246. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  5247. //printing necessary things to background
  5248. int3 moveInt = int3(8, 9, 0);
  5249. Rect mapRect = genRect(544, 591, 8, 8);
  5250. CGI->mh->terrainRect
  5251. (grailPos - moveInt, adventureInt->anim,
  5252. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  5253. background, &mapRect, 0, 0, true, moveInt);
  5254. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  5255. std::vector<SPuzzleInfo> puzzlesToPrint;
  5256. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  5257. {
  5258. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered > PUZZLES_PER_FACTION * discoveredRatio)
  5259. {
  5260. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  5261. }
  5262. else
  5263. {
  5264. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  5265. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  5266. }
  5267. }
  5268. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  5269. {
  5270. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  5271. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  5272. SDL_FreeSurface(puzzle);
  5273. }
  5274. }
  5275. CPuzzleWindow::~CPuzzleWindow()
  5276. {
  5277. delete quitb;
  5278. delete resdatabar;
  5279. SDL_FreeSurface(background);
  5280. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  5281. SDL_FreeSurface( puzzlesToPullBack[g].first );
  5282. }
  5283. void CPuzzleWindow::activate()
  5284. {
  5285. quitb->activate();
  5286. }
  5287. void CPuzzleWindow::deactivate()
  5288. {
  5289. quitb->deactivate();
  5290. }
  5291. void CPuzzleWindow::show(SDL_Surface * to)
  5292. {
  5293. blitAt(background, pos.x, pos.y, to);
  5294. quitb->showAll(to);
  5295. resdatabar->draw(to);
  5296. //blitting disappearing puzzles
  5297. for(int g=0; g<2; ++g)
  5298. if(animCount != 255)
  5299. ++animCount;
  5300. if(animCount != 255)
  5301. {
  5302. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  5303. {
  5304. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  5305. yPos = puzzlesToPullBack[b].second->y + pos.y;
  5306. SDL_Surface *from = puzzlesToPullBack[b].first;
  5307. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  5308. blitAt(from, xPos, yPos, to);
  5309. }
  5310. }
  5311. //disappearing puzzles blitted
  5312. //printing border around window
  5313. if(screen->w != 800 || screen->h !=600)
  5314. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  5315. }
  5316. void CTransformerWindow::CItem::showAll(SDL_Surface * to)
  5317. {
  5318. SDL_Surface * backgr = graphics->bigImgs[parent->army->getCreature(id)->idNumber];
  5319. blitAt(backgr, pos.x, pos.y, to);
  5320. printAtMiddle(boost::lexical_cast<std::string>(size),pos.x+28, pos.y+76,FONT_SMALL,zwykly,to);//stack size
  5321. }
  5322. void CTransformerWindow::CItem::move()
  5323. {
  5324. if (left)
  5325. pos.x += 289;
  5326. else
  5327. pos.x -= 289;
  5328. left = !left;
  5329. }
  5330. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  5331. {
  5332. if(previousState && (!down))
  5333. {
  5334. move();
  5335. parent->showAll(screen2);
  5336. }
  5337. }
  5338. CTransformerWindow::CItem::CItem(CTransformerWindow * _parent, int _size, int _id):
  5339. id(_id), size(_size), parent(_parent)
  5340. {
  5341. used = LCLICK;
  5342. left = true;
  5343. pos.w = 58;
  5344. pos.h = 64;
  5345. pos.x += 45 + (id%3)*83 + id/6*83;
  5346. pos.y += 109 + (id/3)*98;
  5347. }
  5348. CTransformerWindow::CItem::~CItem()
  5349. {
  5350. }
  5351. void CTransformerWindow::showAll(SDL_Surface * to)
  5352. {
  5353. CIntObject::showAll(to);
  5354. printAtMiddleLoc( CGI->generaltexth->allTexts[485], 153, 29,FONT_SMALL, tytulowy,to);//holding area
  5355. printAtMiddleLoc( CGI->generaltexth->allTexts[486], 153+295, 29,FONT_SMALL, tytulowy,to);//transformer
  5356. printAtMiddleWBLoc(CGI->generaltexth->allTexts[487], 153, 75,FONT_MEDIUM, 32,tytulowy,to);//move creatures to create skeletons
  5357. printAtMiddleWBLoc(CGI->generaltexth->allTexts[488], 153+295, 75,FONT_MEDIUM, 32,tytulowy,to);//creatures here will become skeletons
  5358. }
  5359. void CTransformerWindow::makeDeal()
  5360. {
  5361. for (int i=0; i<items.size(); i++)
  5362. if (!items[i]->left)
  5363. LOCPLINT->cb->trade(town, CREATURE_UNDEAD, items[i]->id, 0, 0, hero);
  5364. }
  5365. void CTransformerWindow::addAll()
  5366. {
  5367. for (int i=0; i<items.size(); i++)
  5368. if (items[i]->left)
  5369. items[i]->move();
  5370. showAll(screen2);
  5371. }
  5372. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):hero(_hero),town(_town)
  5373. {
  5374. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5375. bg = new CPicture ("SKTRNBK.PCX");
  5376. bg->colorizeAndConvert(LOCPLINT->playerID);
  5377. pos = center(bg->pos);
  5378. if (hero)
  5379. army = hero;
  5380. else
  5381. army = town;
  5382. for (int i=0; i<7; i++ )
  5383. if ( army->getCreature(i) )
  5384. items.push_back(new CItem(this, army->getStackCount(i), i));
  5385. all = new AdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  5386. convert= new AdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  5387. cancel = new AdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CGuiHandler::popIntTotally,&GH, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  5388. bar = new CGStatusBar(304, 469);
  5389. }
  5390. CTransformerWindow::~CTransformerWindow()
  5391. {
  5392. }
  5393. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  5394. {
  5395. if(previousState && (!down))
  5396. {
  5397. if ( state() != 2 )
  5398. return;
  5399. CUnivConfirmWindow *win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(6) >= 2000);
  5400. GH.pushInt(win);
  5401. }
  5402. }
  5403. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  5404. {
  5405. if(down)
  5406. {
  5407. CInfoPopup *message = new CInfoPopup();
  5408. message->free = true;
  5409. message->bitmap = CMessage::drawBoxTextBitmapSub
  5410. (LOCPLINT->playerID,
  5411. CGI->generaltexth->skillInfoTexts[ID][0],
  5412. graphics->abils82->ourImages[ID*3+3].bitmap,
  5413. CGI->generaltexth->skillName[ID]);
  5414. message->pos.x = screen->w/2 - message->bitmap->w/2;
  5415. message->pos.y = screen->h/2 - message->bitmap->h/2;
  5416. GH.pushInt(message);
  5417. }
  5418. }
  5419. void CUniversityWindow::CItem::hover(bool on)
  5420. {
  5421. if (on)
  5422. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  5423. else
  5424. GH.statusbar->clear();
  5425. }
  5426. int CUniversityWindow::CItem::state()
  5427. {
  5428. if (parent->hero->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ID)))//hero know this skill
  5429. return 1;
  5430. if (parent->hero->secSkills.size() >= SKILL_PER_HERO)//can't learn more skills
  5431. return 0;
  5432. if (parent->hero->type->heroClass->proSec[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  5433. return 0;
  5434. /* if (LOCPLINT->cb->getResourceAmount(6) < 2000 )//no gold - allowed in H3, confirm button is blocked instead
  5435. return 0;*/
  5436. return 2;
  5437. }
  5438. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  5439. {
  5440. CPicture * bar;
  5441. switch (state())
  5442. {
  5443. case 0: bar = parent->red;
  5444. break;
  5445. case 1: bar = parent->yellow;
  5446. break;
  5447. case 2: bar = parent->green;
  5448. break;
  5449. default:bar = NULL;
  5450. break;
  5451. }
  5452. assert(bar);
  5453. blitAtLoc(bar->bg, -28, -22, to);
  5454. blitAtLoc(bar->bg, -28, 48, to);
  5455. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, zwykly,to);//Name
  5456. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, zwykly,to);//Level(always basic)
  5457. CPicture::showAll(to);
  5458. }
  5459. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  5460. CPicture (graphics->abils44->ourImages[_ID*3+3].bitmap,X,Y,false),ID(_ID), parent(_parent)
  5461. {
  5462. used = LCLICK | RCLICK | HOVER;
  5463. }
  5464. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):hero(_hero), market(_market)
  5465. {
  5466. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5467. bg = new CPicture("UNIVERS1.PCX");
  5468. bg->colorizeAndConvert(LOCPLINT->playerID);
  5469. green = new CPicture("UNIVGREN.PCX");
  5470. yellow = new CPicture("UNIVGOLD.PCX");//bars
  5471. red = new CPicture("UNIVRED.PCX");
  5472. green->recActions =
  5473. yellow->recActions =
  5474. red->recActions = DISPOSE;
  5475. if ( market->o->ID == 104 ) // this is adventure map university
  5476. {
  5477. SDL_Surface * titleImage = BitmapHandler::loadBitmap("UNIVBLDG.PCX");
  5478. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  5479. SDL_FreeSurface(titleImage);
  5480. }
  5481. else if (LOCPLINT->castleInt && LOCPLINT->castleInt->town->subID == 8)// this is town university
  5482. {
  5483. SDL_Surface * titleImage = LOCPLINT->castleInt->bicons->ourImages[21].bitmap;
  5484. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  5485. }
  5486. else
  5487. tlog0<<"Error: Image for university was not found!\n";//This should not happen
  5488. printAtMiddleWBLoc(CGI->generaltexth->allTexts[603], 232, 153, FONT_SMALL, 70,zwykly,bg->bg);//Clerk speech
  5489. printAtMiddleLoc (CGI->generaltexth->allTexts[602], 231, 26 , FONT_MEDIUM ,tytulowy,bg->bg);//University
  5490. std::vector<int> list = market->availableItemsIds(RESOURCE_SKILL);
  5491. if (list.size()!=4)
  5492. tlog0<<"\t\tIncorrect size of available items vector!\n";
  5493. for (int i=0; i<list.size(); i++)//prepare clickable items
  5494. items.push_back(new CItem(this, list[i], pos.x+54+i*104, pos.y+234));
  5495. pos = center(bg->pos);
  5496. cancel = new AdventureMapButton(CGI->generaltexth->zelp[632],
  5497. boost::bind(&CGuiHandler::popIntTotally,&GH, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  5498. bar = new CGStatusBar(232, 371);
  5499. }
  5500. CUniversityWindow::~CUniversityWindow()
  5501. {
  5502. }
  5503. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):parent(PARENT)
  5504. {
  5505. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5506. bg = new CPicture("UNIVERS2.PCX");
  5507. bg->colorizeAndConvert(LOCPLINT->playerID);
  5508. std::string text = CGI->generaltexth->allTexts[608];
  5509. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  5510. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  5511. boost::replace_first(text, "%d", "2000");
  5512. printAtMiddleWBLoc(text, 230, 163, FONT_SMALL, 65,zwykly,bg->bg);//Clerk speech
  5513. printAtMiddleLoc (CGI->generaltexth-> skillName[SKILL], 230, 37, FONT_SMALL, zwykly,bg->bg);//Skill name
  5514. printAtMiddleLoc (CGI->generaltexth->levels[1], 230, 107, FONT_SMALL, zwykly,bg->bg);//Skill level
  5515. printAtMiddleLoc ("2000", 230, 267, FONT_SMALL, zwykly,bg->bg);//Cost
  5516. blitAtLoc(graphics->abils44->ourImages[SKILL*3+3].bitmap, 211, 51, bg->bg);//skill
  5517. blitAtLoc(graphics->resources32->ourImages[6].bitmap, 210, 210, bg->bg);//gold
  5518. pos = center(bg->pos);
  5519. std::string hoverText = CGI->generaltexth->allTexts[609];
  5520. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  5521. text = CGI->generaltexth->zelp[633].second;
  5522. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  5523. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  5524. boost::replace_first(text, "%d", "2000");
  5525. confirm= new AdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  5526. 148,299,"IBY6432.DEF",SDLK_RETURN);
  5527. confirm->block(!available);
  5528. cancel = new AdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CGuiHandler::popIntTotally, &GH, this),
  5529. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  5530. bar = new CGStatusBar(232, 371);
  5531. }
  5532. void CUnivConfirmWindow::makeDeal(int skill)
  5533. {
  5534. LOCPLINT->cb->trade(parent->market->o, RESOURCE_SKILL, 6, skill, 1, parent->hero);
  5535. GH.popIntTotally(this);
  5536. }
  5537. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  5538. fort(object),hero(visitor)
  5539. {
  5540. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5541. slotsCount=7;
  5542. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  5543. bg = new CPicture("APHLFTBK.PCX");
  5544. bg->colorizeAndConvert(LOCPLINT->playerID);
  5545. printAtMiddleLoc (fort->hoverName, 325, 32, FONT_BIG, tytulowy, bg->bg);//Hill Fort
  5546. pos = center(bg->pos);
  5547. heroPic = new CHeroArea(30, 60, hero);
  5548. currState.resize(slotsCount+1);
  5549. costs.resize(slotsCount);
  5550. totalSumm.resize(RESOURCE_QUANTITY);
  5551. std::vector<std::string> files;
  5552. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  5553. for (int i=0; i<slotsCount; i++)
  5554. {
  5555. currState[i] = getState(i);
  5556. upgrade[i] = new AdventureMapButton(getTextForSlot(i),"",boost::bind(&CHillFortWindow::makeDeal, this, i),
  5557. 107+i*76, 171, "", SDLK_1+i, &files);
  5558. upgrade[i]->block(currState[i] == -1);
  5559. }
  5560. files.clear();
  5561. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  5562. currState[slotsCount] = getState(slotsCount);
  5563. upgradeAll = new AdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, slotsCount),
  5564. 30, 231, "", SDLK_0, &files);
  5565. quit = new AdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  5566. bar = new CGStatusBar(327, 332);
  5567. garr = new CGarrisonInt(108, 60, 18, Point(),bg->bg,Point(108,60),hero,NULL);
  5568. updateGarrisons();
  5569. }
  5570. CHillFortWindow::~CHillFortWindow()
  5571. {
  5572. }
  5573. void CHillFortWindow::activate()
  5574. {
  5575. CIntObject::activate();
  5576. GH.statusbar = bar;
  5577. }
  5578. void CHillFortWindow::updateGarrisons()
  5579. {
  5580. for (int i=0; i<RESOURCE_QUANTITY; i++)
  5581. totalSumm[i]=0;
  5582. for (int i=0; i<slotsCount; i++)
  5583. {
  5584. costs[i].clear();
  5585. int newState = getState(i);
  5586. if (newState != -1)
  5587. {
  5588. UpgradeInfo info;
  5589. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  5590. if (info.newID.size())//we have upgrades here - update costs
  5591. for(std::set<std::pair<int,int> >::iterator it=info.cost[0].begin(); it!=info.cost[0].end(); it++)
  5592. {
  5593. std::pair<int, int> pair = std::make_pair(it->first, it->second * hero->getStackCount(i) );
  5594. costs[i].insert(pair);
  5595. totalSumm[pair.first] += pair.second;
  5596. }
  5597. }
  5598. currState[i] = newState;
  5599. upgrade[i]->setIndex(newState);
  5600. upgrade[i]->block(currState[i] == -1);
  5601. upgrade[i]->hoverTexts[0] = getTextForSlot(i);
  5602. }
  5603. int newState = getState(slotsCount);
  5604. currState[slotsCount] = newState;
  5605. upgradeAll->setIndex(newState);
  5606. garr->recreateSlots();
  5607. }
  5608. void CHillFortWindow::makeDeal(int slot)
  5609. {
  5610. int offset = (slot == slotsCount)?2:0;
  5611. switch (currState[slot])
  5612. {
  5613. case 0:
  5614. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  5615. std::vector<SComponent*>(), soundBase::sound_todo);
  5616. break;
  5617. case 1:
  5618. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  5619. std::vector<SComponent*>(), soundBase::sound_todo);
  5620. break;
  5621. case 2:
  5622. for (int i=0; i<slotsCount; i++)
  5623. if ( slot ==i || ( slot == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  5624. {
  5625. UpgradeInfo info;
  5626. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  5627. LOCPLINT->cb->upgradeCreature(hero, i, info.newID[0]);
  5628. }
  5629. break;
  5630. }
  5631. }
  5632. void CHillFortWindow::showAll (SDL_Surface *to)
  5633. {
  5634. CIntObject::showAll(to);
  5635. for ( int i=0; i<slotsCount; i++)
  5636. {
  5637. if ( currState[i] == 0 || currState[i] == 2 )
  5638. {
  5639. if ( costs[i].size() )//we have several elements
  5640. {
  5641. int curY = 128;//reverse iterator is used to display gold as first element
  5642. for( std::map<int,int>::reverse_iterator rit=costs[i].rbegin(); rit!=costs[i].rend(); rit++)
  5643. {
  5644. blitAtLoc(resources->ourImages[rit->first].bitmap, 104+76*i, curY, to);
  5645. printToLoc(boost::lexical_cast<std::string>(rit->second), 168+76*i, curY+16, FONT_SMALL, zwykly, to);
  5646. curY += 20;
  5647. }
  5648. }
  5649. else//free upgrade - print gold image and "Free" text
  5650. {
  5651. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  5652. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, zwykly, to);
  5653. }
  5654. }
  5655. }
  5656. for (int i=0; i<RESOURCE_QUANTITY; i++)
  5657. {
  5658. if (totalSumm[i])//this resource is used - display it
  5659. {
  5660. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  5661. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, zwykly, to);
  5662. }
  5663. }
  5664. }
  5665. std::string CHillFortWindow::getTextForSlot(int slot)
  5666. {
  5667. if ( !hero->getCreature(slot) )//we dont have creature here
  5668. return "";
  5669. std::string str = CGI->generaltexth->allTexts[318];
  5670. int amount = hero->getStackCount(slot);
  5671. if ( amount == 1 )
  5672. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  5673. else
  5674. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  5675. return str;
  5676. }
  5677. int CHillFortWindow::getState(int slot)
  5678. {
  5679. if ( slot == slotsCount )//"Upgrade all" slot
  5680. {
  5681. bool allUpgraded = true;//All creatures are upgraded?
  5682. for (int i=0; i<slotsCount; i++)
  5683. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  5684. if (allUpgraded)
  5685. return 1;
  5686. for ( int i=0; i<RESOURCE_QUANTITY; i++)//if we need more resources
  5687. if(LOCPLINT->cb->getResourceAmount(i) < totalSumm[i])
  5688. return 0;
  5689. return 2;
  5690. }
  5691. if (hero->slotEmpty(slot))//no creature here
  5692. return -1;
  5693. UpgradeInfo info;
  5694. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  5695. if (!info.newID.size())//already upgraded
  5696. return 1;
  5697. for(std::set<std::pair<int,int> >::iterator it=info.cost[0].begin(); it!=info.cost[0].end(); it++)
  5698. if(LOCPLINT->cb->getResourceAmount(it->first) < it->second * hero->getStackCount(slot))
  5699. return 0;
  5700. return 2;//can upgrade
  5701. }
  5702. void CThievesGuildWindow::activate()
  5703. {
  5704. CIntObject::activate();
  5705. GH.statusbar = statusBar;
  5706. }
  5707. void CThievesGuildWindow::show(SDL_Surface * to)
  5708. {
  5709. blitAt(background, pos.x, pos.y, to);
  5710. statusBar->show(to);
  5711. exitb->showAll(to);
  5712. resdatabar->show(to);
  5713. //showing border around window
  5714. if(screen->w != 800 || screen->h !=600)
  5715. {
  5716. CMessage::drawBorder(LOCPLINT->playerID, to, pos.w + 28, pos.h + 28, pos.x-14, pos.y-15);
  5717. }
  5718. }
  5719. void CThievesGuildWindow::bexitf()
  5720. {
  5721. GH.popIntTotally(this);
  5722. }
  5723. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner)
  5724. :owner(_owner)
  5725. {
  5726. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5727. SThievesGuildInfo tgi; //info to be displayed
  5728. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  5729. pos = center(Rect(0,0,800,600));// Rect( (conf.cc.resx - 800) / 2, (conf.cc.resy - 600) / 2, 800, 600 );
  5730. //loading backround and converting to more bpp form
  5731. SDL_Surface * bg = background = BitmapHandler::loadBitmap("TpRank.bmp", false);
  5732. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  5733. background = newSurface(bg->w, bg->h);
  5734. blitAt(bg, 0, 0, background);
  5735. SDL_FreeSurface(bg);
  5736. exitb = new AdventureMapButton (std::string(), std::string(), boost::bind(&CThievesGuildWindow::bexitf,this), 748, 556, "TPMAGE1.def", SDLK_RETURN);
  5737. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  5738. resdatabar = new CMinorResDataBar();
  5739. resdatabar->pos.x += pos.x;
  5740. resdatabar->pos.y += pos.y;
  5741. static std::vector< std::list< ui8 > > SThievesGuildInfo::* fields[] = { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  5742. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks, &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army,
  5743. &SThievesGuildInfo::income};
  5744. //printing texts & descriptions to background
  5745. for(int g=0; g<12; ++g)
  5746. {
  5747. int y;
  5748. if(g == 9) //best hero
  5749. {
  5750. y = 400;
  5751. }
  5752. else if(g == 10) //personality
  5753. {
  5754. y = 460;
  5755. }
  5756. else if(g == 11) //best monster
  5757. {
  5758. y = 510;
  5759. }
  5760. else
  5761. {
  5762. y = 52 + 32*g;
  5763. }
  5764. std::string text = CGI->generaltexth->jktexts[24+g];
  5765. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  5766. printAtMiddle(text, 135, y, FONT_MEDIUM, tytulowy, background);
  5767. }
  5768. CDefHandler * strips = CDefHandler::giveDef("PRSTRIPS.DEF");
  5769. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  5770. for(int g=0; g<tgi.playerColors.size(); ++g)
  5771. {
  5772. if(g > 0)
  5773. {
  5774. blitAt(strips->ourImages[g-1].bitmap, 250 + 66*g, 7, background);
  5775. }
  5776. printAtMiddle(CGI->generaltexth->jktexts[16+g], 283 + 66*g, 24, FONT_BIG, tytulowy, background);
  5777. SDL_Surface * box = BitmapHandler::loadBitmap(colorToBox[tgi.playerColors[g]]);
  5778. blitAt(box, 253 + 66*g, 334, background);
  5779. SDL_FreeSurface(box);
  5780. }
  5781. delete strips;
  5782. CDefHandler * flagPictures = CDefHandler::giveDef("itgflags.def");
  5783. //printing flags
  5784. for(int g=0; g<ARRAY_COUNT(fields); ++g) //by lines
  5785. {
  5786. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  5787. {
  5788. std::list<ui8> players = (tgi .* fields[g])[b]; //get players with this place in this line
  5789. //std::sort(players.begin(), players.end());
  5790. int counter = 0;
  5791. for(std::list<ui8>::const_iterator it = players.begin(); it != players.end(); ++it)
  5792. {
  5793. int xpos = 259 + 66 * b + 12 * (counter % 4) + 6 * (counter / 4);
  5794. int ypos = 41 + 32 * g + 4 * (counter / 4);
  5795. blitAt(flagPictures->ourImages[*it].bitmap, xpos, ypos, background);
  5796. counter++;
  5797. }
  5798. }
  5799. }
  5800. delete flagPictures;
  5801. flagPictures = NULL;
  5802. //printing best hero
  5803. int counter = 0;
  5804. for(std::map<ui8, InfoAboutHero>::const_iterator it = tgi.colorToBestHero.begin(); it != tgi.colorToBestHero.end(); ++it)
  5805. {
  5806. if(it->second.portrait >= 0)
  5807. blitAt(graphics->portraitSmall[it->second.portrait], 260 + 66 * counter, 360, background);
  5808. counter++;
  5809. //printing stats
  5810. if(it->second.details)
  5811. {
  5812. printAtWB(CGI->generaltexth->allTexts[184], 191 + 66*counter, 396, FONT_TINY, 10, zwykly, background);
  5813. for (int i=0; i<it->second.details->primskills.size(); ++i)
  5814. {
  5815. std::ostringstream skill;
  5816. skill << it->second.details->primskills[i];
  5817. printTo(skill.str(), 244 + 66 * counter, 407 + 11*i, FONT_TINY, zwykly, background);
  5818. }
  5819. }
  5820. }
  5821. //printing best creature
  5822. counter = 0;
  5823. for(std::map<ui8, si32>::const_iterator it = tgi.bestCreature.begin(); it != tgi.bestCreature.end(); ++it)
  5824. {
  5825. if(it->second >= 0)
  5826. blitAt(graphics->bigImgs[it->second], 255 + 66 * counter, 479, background);
  5827. counter++;
  5828. }
  5829. //printing personality
  5830. counter = 0;
  5831. for(std::map<ui8, si8>::const_iterator it = tgi.personality.begin(); it != tgi.personality.end(); ++it)
  5832. {
  5833. int toPrint = 0;
  5834. if(it->second == -1)
  5835. {
  5836. toPrint = 172;
  5837. }
  5838. else
  5839. {
  5840. toPrint = 168 + it->second;
  5841. }
  5842. printAtMiddle(CGI->generaltexth->arraytxt[toPrint], 283 + 66*counter, 459, FONT_SMALL, zwykly, background);
  5843. counter++;
  5844. }
  5845. }
  5846. CThievesGuildWindow::~CThievesGuildWindow()
  5847. {
  5848. SDL_FreeSurface(background);
  5849. // delete exitb;
  5850. // delete statusBar;
  5851. // delete resdatabar;
  5852. }
  5853. void MoraleLuckBox::set(const IBonusBearer *node)
  5854. {
  5855. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  5856. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  5857. 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.
  5858. const int componentType[] = {SComponent::luck, SComponent::morale};
  5859. const int hoverTextBase[] = {7, 4};
  5860. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  5861. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  5862. int mrlt = -9;
  5863. TModDescr mrl;
  5864. if (node)
  5865. {
  5866. node->getModifiersWDescr(mrl, bonusType[morale]);
  5867. bonusValue = (node->*getValue[morale])();
  5868. }
  5869. else
  5870. bonusValue = 0;
  5871. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  5872. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  5873. baseType = componentType[morale];
  5874. text = CGI->generaltexth->arraytxt[textId[morale]];
  5875. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  5876. if (!mrl.size())
  5877. text += CGI->generaltexth->arraytxt[noneTxtId];
  5878. else
  5879. {
  5880. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  5881. {
  5882. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  5883. }
  5884. else
  5885. {
  5886. for(int it=0; it < mrl.size(); it++)
  5887. text += "\n" + mrl[it].second;
  5888. }
  5889. }
  5890. }
  5891. void MoraleLuckBox::showAll(SDL_Surface * to)
  5892. {
  5893. CDefEssential *def;
  5894. if (small)
  5895. def = morale ? graphics->morale30 : graphics->luck30;
  5896. else
  5897. def = morale ? graphics->morale42 : graphics->luck42;
  5898. SDL_Surface *img = def->ourImages[bonusValue + 3].bitmap;
  5899. blitAt(img, Rect(img).centerIn(pos), to); //put img in the center of our pos
  5900. }
  5901. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small)
  5902. :morale(Morale),
  5903. small(Small)
  5904. {
  5905. bonusValue = 0;
  5906. pos = r + pos;
  5907. }
  5908. MoraleLuckBox::~MoraleLuckBox()
  5909. {
  5910. }
  5911. void CLabel::showAll(SDL_Surface * to)
  5912. {
  5913. CIntObject::showAll(to);
  5914. std::string *hlpText = NULL; //if NULL, text field will be used
  5915. if(ignoreLeadingWhitespace)
  5916. {
  5917. hlpText = new std::string(text);
  5918. boost::trim_left(*hlpText);
  5919. }
  5920. std::string &toPrint = hlpText ? *hlpText : text;
  5921. if(!toPrint.length())
  5922. return;
  5923. 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
  5924. printer[alignment](toPrint, pos.x + textOffset.x, pos.y + textOffset.y, font, color, to);
  5925. }
  5926. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  5927. :alignment(Align), font(Font), color(Color), text(Text)
  5928. {
  5929. autoRedraw = true;
  5930. pos.x += x;
  5931. pos.y += y;
  5932. pos.w = pos.h = 0;
  5933. bg = NULL;
  5934. ignoreLeadingWhitespace = false;
  5935. }
  5936. void CLabel::setTxt(const std::string &Txt)
  5937. {
  5938. text = Txt;
  5939. if(autoRedraw)
  5940. {
  5941. if(bg || !parent)
  5942. redraw();
  5943. else
  5944. parent->redraw();
  5945. }
  5946. }
  5947. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= zwykly*/)
  5948. :CLabel(rect.x, rect.y, Font, Align, Color, Text), sliderStyle(SliderStyle), slider(NULL)
  5949. {
  5950. redrawParentOnScrolling = false;
  5951. autoRedraw = false;
  5952. pos.h = rect.h;
  5953. pos.w = rect.w;
  5954. assert(Align == TOPLEFT || Align == CENTER); //TODO: support for other alignments
  5955. assert(pos.w >= 80); //we need some space
  5956. setTxt(Text);
  5957. }
  5958. void CTextBox::showAll(SDL_Surface * to)
  5959. {
  5960. CIntObject::showAll(to);
  5961. const Font &f = *graphics->fonts[font];
  5962. int dy = f.height; //line height
  5963. int base_y = pos.y;
  5964. if(alignment == CENTER)
  5965. base_y += std::max((pos.h - maxH)/2,0);
  5966. int howManyLinesToPrint = slider ? slider->capacity : lines.size();
  5967. int firstLineToPrint = slider ? slider->value : 0;
  5968. for (int i = 0; i < howManyLinesToPrint; i++)
  5969. {
  5970. const std::string &line = lines[i + firstLineToPrint];
  5971. if(!line.size()) continue;
  5972. int x = pos.x;
  5973. if(alignment == CENTER)
  5974. {
  5975. x += (pos.w - f.getWidth(line.c_str())) / 2;
  5976. if(slider)
  5977. x -= slider->pos.w / 2 + 5;
  5978. }
  5979. if(line[0] == '{' && line[line.size()-1] == '}')
  5980. printAt(line, x, base_y + i*dy, font, tytulowy, to);
  5981. else
  5982. printAt(line, x, base_y + i*dy, font, color, to);
  5983. }
  5984. }
  5985. void CTextBox::setTxt(const std::string &Txt)
  5986. {
  5987. recalculateLines(Txt);
  5988. CLabel::setTxt(Txt);
  5989. }
  5990. void CTextBox::sliderMoved(int to)
  5991. {
  5992. if(!slider)
  5993. return;
  5994. if(redrawParentOnScrolling)
  5995. parent->redraw();
  5996. redraw();
  5997. }
  5998. void CTextBox::setBounds(int limitW, int limitH)
  5999. {
  6000. pos.h = limitH;
  6001. pos.w = limitW;
  6002. recalculateLines(text);
  6003. }
  6004. void CTextBox::recalculateLines(const std::string &Txt)
  6005. {
  6006. delChildNUll(slider, true);
  6007. lines.clear();
  6008. const Font &f = *graphics->fonts[font];
  6009. int lineHeight = f.height;
  6010. int lineCapacity = pos.h / lineHeight;
  6011. lines = CMessage::breakText(Txt, pos.w, font);
  6012. if(lines.size() > lineCapacity) //we need to add a slider
  6013. {
  6014. lines = CMessage::breakText(Txt, pos.w - 32 - 10, font);
  6015. OBJ_CONSTRUCTION_CAPTURING_ALL;
  6016. slider = new CSlider(pos.w - 32, 0, pos.h, boost::bind(&CTextBox::sliderMoved, this, _1), lineCapacity, lines.size(), 0, false, sliderStyle);
  6017. if(active)
  6018. slider->activate();
  6019. }
  6020. maxH = lineHeight * lines.size();
  6021. maxW = 0;
  6022. BOOST_FOREACH(const std::string &line, lines)
  6023. amax(maxW, f.getWidth(line.c_str()));
  6024. }
  6025. void CGStatusBar::print(const std::string & Text)
  6026. {
  6027. setTxt(Text);
  6028. }
  6029. void CGStatusBar::clear()
  6030. {
  6031. setTxt("");
  6032. }
  6033. std::string CGStatusBar::getCurrent()
  6034. {
  6035. return text;
  6036. }
  6037. CGStatusBar::CGStatusBar(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= zwykly*/, const std::string &Text /*= ""*/)
  6038. : CLabel(x, y, Font, Align, Color, Text)
  6039. {
  6040. init();
  6041. }
  6042. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= zwykly*/)
  6043. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  6044. {
  6045. init();
  6046. bg = BG;
  6047. moveChild(bg, bg->parent, this);
  6048. pos = bg->pos;
  6049. calcOffset();
  6050. }
  6051. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  6052. : CLabel(x, y, FONT_SMALL, CENTER)
  6053. {
  6054. OBJ_CONSTRUCTION_CAPTURING_ALL;
  6055. init();
  6056. bg = new CPicture(name);
  6057. pos = bg->pos;
  6058. if(maxw < pos.w)
  6059. {
  6060. amin(pos.w, maxw);
  6061. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  6062. }
  6063. calcOffset();
  6064. }
  6065. CGStatusBar::~CGStatusBar()
  6066. {
  6067. GH.statusbar = oldStatusBar;
  6068. }
  6069. void CGStatusBar::show(SDL_Surface * to)
  6070. {
  6071. }
  6072. void CGStatusBar::init()
  6073. {
  6074. oldStatusBar = GH.statusbar;
  6075. GH.statusbar = this;
  6076. }
  6077. void CGStatusBar::calcOffset()
  6078. {
  6079. switch(alignment)
  6080. {
  6081. case CENTER:
  6082. textOffset = Point(pos.w/2, pos.h/2);
  6083. break;
  6084. case BOTTOMRIGHT:
  6085. textOffset = Point(pos.w, pos.h);
  6086. break;
  6087. }
  6088. }
  6089. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  6090. :cb(CB)
  6091. {
  6092. focus = false;
  6093. pos += Pos;
  6094. captureAllKeys = true;
  6095. OBJ_CONSTRUCTION;
  6096. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  6097. used = LCLICK | KEYBOARD;
  6098. giveFocus();
  6099. }
  6100. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  6101. {
  6102. focus = false;
  6103. pos += Pos;
  6104. captureAllKeys = true;
  6105. OBJ_CONSTRUCTION;
  6106. bg = new CPicture(Pos, 0, true);
  6107. Rect hlp = Pos;
  6108. if(srf)
  6109. CSDL_Ext::blitSurface(srf, &hlp, *bg, NULL);
  6110. else
  6111. SDL_FillRect(*bg, NULL, 0);
  6112. pos.w = bg->pos.w;
  6113. pos.h = bg->pos.h;
  6114. bg->pos = pos;
  6115. used = LCLICK | KEYBOARD;
  6116. giveFocus();
  6117. }
  6118. void CTextInput::showAll( SDL_Surface * to )
  6119. {
  6120. CIntObject::showAll(to);
  6121. const std::string toPrint = focus ? text + "_" : text;
  6122. CSDL_Ext::printAt(toPrint, pos.x, pos.y, FONT_SMALL, zwykly, to);
  6123. }
  6124. void CTextInput::clickLeft( tribool down, bool previousState )
  6125. {
  6126. if(down && !focus)
  6127. giveFocus();
  6128. }
  6129. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  6130. {
  6131. if(!focus || key.state != SDL_PRESSED)
  6132. return;
  6133. if(key.keysym.sym == SDLK_TAB)
  6134. {
  6135. moveFocus();
  6136. GH.breakEventHandling();
  6137. return;
  6138. }
  6139. switch(key.keysym.sym)
  6140. {
  6141. case SDLK_BACKSPACE:
  6142. if(text.size())
  6143. text.resize(text.size()-1);
  6144. break;
  6145. default:
  6146. char c = key.keysym.unicode; //TODO 16-/>8
  6147. static const std::string forbiddenChars = "<>:\"/\\|?*"; //if we are entering a filename, some special characters won't be allowed
  6148. if(!vstd::contains(forbiddenChars,c) && std::isprint(c))
  6149. text += c;
  6150. break;
  6151. }
  6152. redraw();
  6153. cb(text);
  6154. }
  6155. void CTextInput::setText( const std::string &nText, bool callCb )
  6156. {
  6157. text = nText;
  6158. redraw();
  6159. if(callCb)
  6160. cb(text);
  6161. }
  6162. CTextInput::~CTextInput()
  6163. {
  6164. }
  6165. CFocusable::CFocusable()
  6166. {
  6167. focusables.push_back(this);
  6168. }
  6169. CFocusable::~CFocusable()
  6170. {
  6171. if(inputWithFocus == this)
  6172. inputWithFocus = NULL;
  6173. focusables -= this;
  6174. }
  6175. void CFocusable::giveFocus()
  6176. {
  6177. if(inputWithFocus)
  6178. {
  6179. inputWithFocus->focus = false;
  6180. inputWithFocus->redraw();
  6181. }
  6182. focus = true;
  6183. inputWithFocus = this;
  6184. redraw();
  6185. }
  6186. void CFocusable::moveFocus()
  6187. {
  6188. std::list<CFocusable*>::iterator i = vstd::find(focusables, this),
  6189. ourIt = i;
  6190. for(i++; i != ourIt; i++)
  6191. {
  6192. if(i == focusables.end())
  6193. i = focusables.begin();
  6194. if((*i)->active)
  6195. {
  6196. (*i)->giveFocus();
  6197. break;;
  6198. }
  6199. }
  6200. }
  6201. CArtifactHolder::CArtifactHolder()
  6202. {
  6203. type |= WITH_ARTIFACTS;
  6204. }
  6205. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  6206. {
  6207. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  6208. aoh->artifactRemoved(artLoc);
  6209. }
  6210. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  6211. {
  6212. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  6213. aoh->artifactMoved(artLoc, destLoc);
  6214. }
  6215. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  6216. {
  6217. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  6218. aoh->artifactDisassembled(artLoc);
  6219. }
  6220. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  6221. {
  6222. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  6223. aoh->artifactAssembled(artLoc);
  6224. }
  6225. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  6226. {
  6227. slotID = -1;
  6228. AOH = NULL;
  6229. art = NULL;
  6230. }
  6231. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  6232. {
  6233. clear();
  6234. }
  6235. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  6236. {
  6237. slotID = place->slotID;
  6238. AOH = place->ourOwner;
  6239. if(slotID >= 19 && dontTakeBackpack)
  6240. art = NULL;
  6241. else
  6242. art = place->ourArt;
  6243. }
  6244. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  6245. {
  6246. if(!AOH)
  6247. return false;
  6248. bool ret = al.hero == AOH->curHero && al.slot == slotID;
  6249. //assert(al.getArt() == art);
  6250. return ret;
  6251. }
  6252. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  6253. {
  6254. assert(AOH && art);
  6255. return art == AOH->curHero->getArt(slotID);
  6256. }
  6257. CIntObject * createCreWindow(const CStack *s)
  6258. {
  6259. if(conf.cc.classicCreatureWindow)
  6260. return new CCreInfoWindow(*s);
  6261. else
  6262. return new CCreatureWindow(*s, CCreatureWindow::BATTLE);
  6263. }
  6264. CIntObject * createCreWindow(int Cid, int Type, int creatureCount)
  6265. {
  6266. if(conf.cc.classicCreatureWindow)
  6267. return new CCreInfoWindow(Cid, Type, creatureCount);
  6268. else
  6269. return new CCreatureWindow(Cid, Type, creatureCount);
  6270. }
  6271. CIntObject * createCreWindow(const CStackInstance *s, int type, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  6272. {
  6273. if(conf.cc.classicCreatureWindow)
  6274. return new CCreInfoWindow(*s, type==3, Upg, Dsm, ui);
  6275. else
  6276. return new CCreatureWindow(*s, type, Upg, Dsm, ui);
  6277. }