GUIClasses.cpp 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "UIFramework/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "BattleInterface/CBattleInterface.h"
  6. #include "BattleInterface/CBattleInterfaceClasses.h"
  7. #include "../CCallback.h"
  8. #include "CCastleInterface.h"
  9. #include "CCreatureWindow.h"
  10. #include "UIFramework/CCursorHandler.h"
  11. #include "CGameInfo.h"
  12. #include "CHeroWindow.h"
  13. #include "CMessage.h"
  14. #include "CConfigHandler.h"
  15. #include "CConfigHandler.h"
  16. #include "BattleInterface/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/CModHandler.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 "../lib/CStopWatch.h"
  32. #include "../lib/NetPacks.h"
  33. #include "CSpellWindow.h"
  34. #include "CHeroWindow.h"
  35. #include "CVideoHandler.h"
  36. #include "../lib/StartInfo.h"
  37. #include "CPreGame.h"
  38. #include "../lib/HeroBonus.h"
  39. #include "../lib/CCreatureHandler.h"
  40. #include "CMusicHandler.h"
  41. #include "../lib/BattleState.h"
  42. #include "../lib/CGameState.h"
  43. #include "../lib/GameConstants.h"
  44. #include "UIFramework/CGuiHandler.h"
  45. /*
  46. * GUIClasses.cpp, part of VCMI engine
  47. *
  48. * Authors: listed in file AUTHORS in main folder
  49. *
  50. * License: GNU General Public License v2.0 or later
  51. * Full text of license available in license.txt file, in main folder
  52. *
  53. */
  54. using namespace boost::assign;
  55. using namespace CSDL_Ext;
  56. extern std::queue<SDL_Event*> events;
  57. extern boost::mutex eventsM;
  58. std::list<CFocusable*> CFocusable::focusables;
  59. CFocusable * CFocusable::inputWithFocus;
  60. #undef min
  61. #undef max
  62. void CArmyTooltip::init(const InfoAboutArmy &army)
  63. {
  64. OBJ_CONSTRUCTION_CAPTURING_ALL;
  65. new CLabel(66, 2, FONT_SMALL, TOPLEFT, Colors::Cornsilk, army.name);
  66. std::vector<Point> slotsPos;
  67. slotsPos.push_back(Point(36,73));
  68. slotsPos.push_back(Point(72,73));
  69. slotsPos.push_back(Point(108,73));
  70. slotsPos.push_back(Point(18,122));
  71. slotsPos.push_back(Point(54,122));
  72. slotsPos.push_back(Point(90,122));
  73. slotsPos.push_back(Point(126,122));
  74. BOOST_FOREACH(auto & slot, army.army)
  75. {
  76. if(slot.first >= GameConstants::ARMY_SIZE)
  77. {
  78. tlog3 << "Warning: " << army.name << " has stack in slot " << slot.first << std::endl;
  79. continue;
  80. }
  81. new CAnimImage("CPRSMALL", slot.second.type->idNumber + 2, 0, slotsPos[slot.first].x, slotsPos[slot.first].y);
  82. std::string subtitle;
  83. if(army.army.isDetailed)
  84. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  85. else
  86. {
  87. //if =0 - we have no information about stack size at all
  88. if (slot.second.count)
  89. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  90. }
  91. new CLabel(slotsPos[slot.first].x + 17, slotsPos[slot.first].y + 41, FONT_TINY, CENTER, Colors::Cornsilk, subtitle);
  92. }
  93. }
  94. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy &army):
  95. CIntObject(0, pos)
  96. {
  97. init(army);
  98. }
  99. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  100. CIntObject(0, pos)
  101. {
  102. init(InfoAboutArmy(army, true));
  103. }
  104. void CHeroTooltip::init(const InfoAboutHero &hero)
  105. {
  106. OBJ_CONSTRUCTION_CAPTURING_ALL;
  107. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  108. if(hero.details)
  109. {
  110. for (size_t i = 0; i < hero.details->primskills.size(); i++)
  111. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::Cornsilk,
  112. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  113. new CLabel(158, 98, FONT_TINY, CENTER, Colors::Cornsilk,
  114. boost::lexical_cast<std::string>(hero.details->mana));
  115. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  116. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  117. }
  118. }
  119. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  120. CArmyTooltip(pos, hero)
  121. {
  122. init(hero);
  123. }
  124. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  125. CArmyTooltip(pos, InfoAboutHero(hero, true))
  126. {
  127. init(InfoAboutHero(hero, true));
  128. }
  129. void CTownTooltip::init(const InfoAboutTown &town)
  130. {
  131. OBJ_CONSTRUCTION_CAPTURING_ALL;
  132. //order of icons in def: fort, citadel, castle, no fort
  133. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  134. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  135. assert(town.tType);
  136. size_t imageIndex = town.tType->typeID * 2;
  137. if (town.fortLevel == 0)
  138. imageIndex += GameConstants::F_NUMBER * 2;
  139. if (town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN)
  140. imageIndex++;
  141. new CAnimImage("itpt", imageIndex, 0, 3, 2);
  142. if(town.details)
  143. {
  144. if (town.details->hallLevel)
  145. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  146. if (town.details->goldIncome)
  147. new CLabel(157, 58, FONT_TINY, CENTER, Colors::Cornsilk,
  148. boost::lexical_cast<std::string>(town.details->goldIncome));
  149. if(town.details->garrisonedHero) //garrisoned hero icon
  150. new CPicture("TOWNQKGH", 149, 76);
  151. if(town.details->customRes)//silo is built
  152. {
  153. if (town.tType->primaryRes == 127 )// wood & ore
  154. {
  155. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  156. new CAnimImage("SMALRES", Res::ORE , 0, 7, 88);
  157. }
  158. else
  159. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  160. }
  161. }
  162. }
  163. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown &town):
  164. CArmyTooltip(pos, town)
  165. {
  166. init(town);
  167. }
  168. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town):
  169. CArmyTooltip(pos, InfoAboutTown(town, true))
  170. {
  171. init(InfoAboutTown(town, true));
  172. }
  173. void CGarrisonSlot::hover (bool on)
  174. {
  175. ////Hoverable::hover(on);
  176. if(on)
  177. {
  178. std::string temp;
  179. if(creature)
  180. {
  181. if(owner->highlighted)
  182. {
  183. if(owner->highlighted == this)
  184. {
  185. temp = CGI->generaltexth->tcommands[4]; //View %s
  186. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  187. }
  188. else if (owner->highlighted->creature == creature)
  189. {
  190. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  191. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  192. }
  193. else if (owner->highlighted->creature)
  194. {
  195. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  196. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  197. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  198. }
  199. else
  200. {
  201. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  202. tlog2 << "Highlighted set to NULL"<<std::endl;
  203. owner->highlighted = NULL;
  204. }
  205. }
  206. else
  207. {
  208. if(upg)
  209. {
  210. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  211. }
  212. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == GameConstants::TOWNI_TYPE)
  213. {
  214. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  215. }
  216. else
  217. {
  218. temp = CGI->generaltexth->allTexts[481]; //Select %s
  219. }
  220. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  221. };
  222. }
  223. else
  224. {
  225. if(owner->highlighted)
  226. {
  227. const CArmedInstance *highl = owner->highlighted->getObj();
  228. if( highl->needsLastStack() //we are moving stack from hero's
  229. && highl->stacksCount() == 1 //it's only stack
  230. && owner->highlighted->upg != upg //we're moving it to the other garrison
  231. )
  232. {
  233. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  234. }
  235. else
  236. {
  237. temp = CGI->generaltexth->tcommands[6]; //Move %s
  238. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  239. }
  240. }
  241. else
  242. {
  243. temp = CGI->generaltexth->tcommands[11]; //Empty
  244. }
  245. }
  246. GH.statusbar->print(temp);
  247. }
  248. else
  249. {
  250. GH.statusbar->clear();
  251. }
  252. }
  253. const CArmedInstance * CGarrisonSlot::getObj()
  254. {
  255. return (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  256. }
  257. bool CGarrisonSlot::our()
  258. {
  259. return upg?(owner->owned[1]):(owner->owned[0]);
  260. }
  261. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  262. {
  263. if(down && creature)
  264. {
  265. GH.pushInt(createCreWindow(myStack, 2));
  266. }
  267. }
  268. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  269. {
  270. if(down)
  271. {
  272. bool refr = false;
  273. if(owner->highlighted)
  274. {
  275. if(owner->highlighted == this) //view info
  276. {
  277. UpgradeInfo pom;
  278. LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
  279. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  280. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  281. boost::function<void()> upgr = NULL;
  282. boost::function<void()> dism = NULL;
  283. if (canUpgrade) upgr = boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb, getObj(), ID, pom.newID[0]);
  284. if (canDismiss) dism = boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID);
  285. owner->highlighted = NULL;
  286. owner->splitting = false;
  287. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  288. owner->splitButtons[i]->block(true);
  289. redraw();
  290. refr = true;
  291. CIntObject *creWindow = createCreWindow(myStack, 3, upgr, dism, &pom);
  292. GH.pushInt(creWindow);
  293. }
  294. else
  295. {
  296. // Only allow certain moves if troops aren't removable or not ours.
  297. if ( ( owner->highlighted->our()//our creature is selected
  298. || owner->highlighted->creature == creature )//or we are rebalancing army
  299. && ( owner->removableUnits
  300. || (upg == 0 && ( owner->highlighted->upg == 1 && !creature ) )
  301. || (upg == 1 && owner->highlighted->upg == 1 ) ) )
  302. {
  303. //we want to split
  304. if((owner->splitting || LOCPLINT->shiftPressed())
  305. && (!creature
  306. || (creature == owner->highlighted->creature)))
  307. {
  308. owner->p2 = ID; //store the second stack pos
  309. owner->pb = upg;//store the second stack owner (up or down army)
  310. owner->splitting = false;
  311. int minLeft=0, minRight=0;
  312. if(upg != owner->highlighted->upg) //not splitting within same army
  313. {
  314. if(owner->highlighted->getObj()->stacksCount() == 1 //we're splitting away the last stack
  315. && owner->highlighted->getObj()->needsLastStack() )
  316. {
  317. minLeft = 1;
  318. }
  319. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  320. && owner->highlighted->creature == creature
  321. && getObj()->needsLastStack() )
  322. {
  323. minRight = 1;
  324. }
  325. }
  326. GH.pushInt(new CSplitWindow(owner->highlighted->creature, boost::bind(&CGarrisonInt::splitStacks, owner, _1, _2),
  327. minLeft, minRight, owner->highlighted->count, count));
  328. refr = true;
  329. }
  330. else if(creature != owner->highlighted->creature) //swap
  331. {
  332. LOCPLINT->cb->swapCreatures(
  333. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  334. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  335. ID,owner->highlighted->ID);
  336. }
  337. else //merge
  338. {
  339. LOCPLINT->cb->mergeStacks(
  340. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  341. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  342. owner->highlighted->ID,ID);
  343. }
  344. }
  345. else // Highlight
  346. {
  347. if(creature)
  348. owner->highlighted = this;
  349. redraw();
  350. refr = true;
  351. }
  352. }
  353. }
  354. else //highlight or drop artifact
  355. {
  356. bool artSelected = false;
  357. if (CWindowWithArtifacts* chw = dynamic_cast<CWindowWithArtifacts*>(GH.topInt())) //dirty solution
  358. {
  359. const CArtifactsOfHero::SCommonPart *commonInfo = chw->artSets.front()->commonInfo;
  360. if (const CArtifactInstance *art = commonInfo->src.art)
  361. {
  362. const CGHeroInstance *srcHero = commonInfo->src.AOH->getHero();
  363. artSelected = true;
  364. ArtifactLocation src(srcHero, commonInfo->src.slotID);
  365. ArtifactLocation dst(myStack, ArtifactPosition::CREATURE_SLOT);
  366. if (art->canBePutAt(dst, true))
  367. { //equip clicked stack
  368. if(dst.getArt())
  369. {
  370. //creature can wear only one active artifact
  371. //if we are placing a new one, the old one will be returned to the hero's backpack
  372. LOCPLINT->cb->swapArtifacts(dst, ArtifactLocation(srcHero, dst.getArt()->firstBackpackSlot(srcHero)));
  373. }
  374. LOCPLINT->cb->swapArtifacts(src, dst);
  375. }
  376. }
  377. }
  378. if (!artSelected && creature)
  379. {
  380. owner->highlighted = this;
  381. if(creature)
  382. {
  383. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  384. owner->splitButtons[i]->block(false);
  385. }
  386. }
  387. redraw();
  388. refr = true;
  389. }
  390. if(refr) {hover(false); hover(true); } //to refresh statusbar
  391. }
  392. }
  393. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CStackInstance * Creature)
  394. {
  395. OBJ_CONSTRUCTION_CAPTURING_ALL;
  396. addUsedEvents(LCLICK | RCLICK | HOVER);
  397. pos.x += x;
  398. pos.y += y;
  399. owner = Owner;
  400. //assert(Creature == CGI->creh->creatures[Creature->idNumber]);
  401. upg = Upg;
  402. ID = IID;
  403. myStack = Creature;
  404. creature = Creature ? Creature->type : NULL;
  405. if (creature)
  406. {
  407. std::string imgName = owner->smallIcons ? "cprsmall" : "TWCRPORT";
  408. creatureImage = new CAnimImage(imgName, creature->idNumber + 2);
  409. }
  410. else
  411. creatureImage = nullptr;
  412. count = Creature ? Creature->count : 0;
  413. if(Owner->smallIcons)
  414. {
  415. pos.w = 32;
  416. pos.h = 32;
  417. }
  418. else
  419. {
  420. pos.w = 58;
  421. pos.h = 64;
  422. }
  423. }
  424. void CGarrisonSlot::showAll(SDL_Surface * to)
  425. {
  426. std::map<int,SDL_Surface*> &imgs = (owner->smallIcons ? graphics->smallImgs : graphics->bigImgs);
  427. if(creature)
  428. {
  429. creatureImage->showAll(to);
  430. char buf[15];
  431. SDL_itoa(count,buf,10);
  432. printTo(buf, pos.x+pos.w, pos.y+pos.h+1, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, Colors::Cornsilk, to);
  433. if((owner->highlighted==this)
  434. || (owner->splitting && owner->highlighted->creature == creature))
  435. {
  436. blitAt(imgs[-1],pos,to);
  437. }
  438. }
  439. else//empty slot
  440. {
  441. if(owner->splitting && owner->highlighted->our())
  442. blitAt(imgs[-1],pos,to);
  443. }
  444. }
  445. void CGarrisonInt::addSplitBtn(CAdventureMapButton * button)
  446. {
  447. addChild(button);
  448. button->recActions = defActions;
  449. splitButtons.push_back(button);
  450. }
  451. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  452. {
  453. ret.resize(7);
  454. for(TSlots::const_iterator i=set->Slots().begin(); i!=set->Slots().end(); i++)
  455. {
  456. ret[i->first] = new CGarrisonSlot(this, posX + (i->first*distance), posY, i->first, Upg, i->second);
  457. }
  458. for(int i=0; i<ret.size(); i++)
  459. if(!ret[i])
  460. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY,i,Upg,NULL);
  461. if (twoRows)
  462. for (int i=4; i<ret.size(); i++)
  463. {
  464. ret[i]->pos.x -= 126;
  465. ret[i]->pos.y += 37;
  466. };
  467. }
  468. void CGarrisonInt::createSlots()
  469. {
  470. OBJ_CONSTRUCTION_CAPTURING_ALL;
  471. int width = smallIcons? 32 : 58;
  472. if(armedObjs[0])
  473. createSet(slotsUp, armedObjs[0], 0, 0, width+interx, 0);
  474. if(armedObjs[1])
  475. createSet (slotsDown, armedObjs[1], garOffset.x, garOffset.y, width+interx, 1);
  476. }
  477. void CGarrisonInt::deleteSlots()
  478. {
  479. for (int i=0; i<slotsUp.size(); i++)
  480. vstd::clear_pointer(slotsUp[i]);
  481. for (int i=0; i<slotsDown.size(); i++)
  482. vstd::clear_pointer(slotsDown[i]);
  483. }
  484. void CGarrisonInt::recreateSlots()
  485. {
  486. splitting = false;
  487. highlighted = NULL;
  488. for(size_t i = 0; i<splitButtons.size(); i++)
  489. splitButtons[i]->block(true);
  490. deleteSlots();
  491. createSlots();
  492. }
  493. void CGarrisonInt::splitClick()
  494. {
  495. if(!highlighted)
  496. return;
  497. splitting = !splitting;
  498. redraw();
  499. }
  500. void CGarrisonInt::splitStacks(int, int amountRight)
  501. {
  502. LOCPLINT->cb->splitStack(armedObjs[highlighted->upg], armedObjs[pb], highlighted->ID, p2, amountRight);
  503. }
  504. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  505. SDL_Surface *pomsur, const Point& SurOffset,
  506. const CArmedInstance *s1, const CArmedInstance *s2,
  507. bool _removableUnits, bool smallImgs, bool _twoRows )
  508. : interx(inx), garOffset(garsOffset), highlighted(NULL), splitting(false),
  509. smallIcons(smallImgs), removableUnits (_removableUnits), twoRows(_twoRows)
  510. {
  511. setArmy(s1, false);
  512. setArmy(s2, true);
  513. pos.x += x;
  514. pos.y += y;
  515. createSlots();
  516. }
  517. void CGarrisonInt::activate()
  518. {
  519. for(size_t i = 0; i<splitButtons.size(); i++)
  520. if( (splitButtons[i]->isBlocked()) != !highlighted)
  521. splitButtons[i]->block(!highlighted);
  522. CIntObject::activate();
  523. }
  524. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  525. {
  526. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == 254) : false; //254 - neutral objects (pandora, banks)
  527. armedObjs[bottomGarrison] = army;
  528. }
  529. CInfoWindow::CInfoWindow(std::string Text, int player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  530. {
  531. OBJ_CONSTRUCTION_CAPTURING_ALL;
  532. type |= BLOCK_ADV_HOTKEYS;
  533. ID = -1;
  534. for(int i=0;i<Buttons.size();i++)
  535. {
  536. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first);
  537. button->borderColor = Colors::MetallicGold;
  538. button->borderEnabled = true;
  539. button->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  540. buttons.push_back(button);
  541. }
  542. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::Cornsilk);
  543. if(!text->slider)
  544. {
  545. text->pos.w = text->maxW;
  546. text->pos.h = text->maxH;
  547. }
  548. if(buttons.size())
  549. {
  550. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  551. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  552. }
  553. for(int i=0;i<comps.size();i++)
  554. {
  555. comps[i]->recActions = 0xff;
  556. addChild(comps[i]);
  557. comps[i]->recActions &= ~(SHOWALL | UPDATE);
  558. components.push_back(comps[i]);
  559. }
  560. setDelComps(delComps);
  561. CMessage::drawIWindow(this,Text,player);
  562. }
  563. CInfoWindow::CInfoWindow()
  564. {
  565. ID = -1;
  566. setDelComps(false);
  567. text = NULL;
  568. }
  569. void CInfoWindow::close()
  570. {
  571. GH.popIntTotally(this);
  572. if(LOCPLINT)
  573. LOCPLINT->showingDialog->setn(false);
  574. }
  575. void CInfoWindow::show(SDL_Surface * to)
  576. {
  577. CIntObject::show(to);
  578. }
  579. CInfoWindow::~CInfoWindow()
  580. {
  581. if(!delComps)
  582. {
  583. for (int i=0;i<components.size();i++)
  584. removeChild(components[i]);
  585. }
  586. }
  587. void CInfoWindow::showAll(SDL_Surface * to)
  588. {
  589. CSimpleWindow::show(to);
  590. CIntObject::showAll(to);
  591. }
  592. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, int player)
  593. {
  594. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  595. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  596. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  597. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  598. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  599. for(int i=0;i<onYes.funcs.size();i++)
  600. temp->buttons[0]->callback += onYes.funcs[i];
  601. for(int i=0;i<onNo.funcs.size();i++)
  602. temp->buttons[1]->callback += onNo.funcs[i];
  603. GH.pushInt(temp);
  604. }
  605. CInfoWindow * CInfoWindow::create(const std::string &text, int playerID /*= 1*/, const std::vector<CComponent*> *components /*= NULL*/, bool DelComps)
  606. {
  607. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  608. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  609. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  610. return ret;
  611. }
  612. void CInfoWindow::setDelComps(bool DelComps)
  613. {
  614. delComps = DelComps;
  615. BOOST_FOREACH(CComponent *comp, components)
  616. {
  617. if(delComps)
  618. comp->recActions |= DISPOSE;
  619. else
  620. comp->recActions &= ~DISPOSE;
  621. }
  622. }
  623. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  624. :free(Free),bitmap(Bitmap)
  625. {
  626. init(x, y);
  627. }
  628. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  629. : free(Free),bitmap(Bitmap)
  630. {
  631. switch(alignment)
  632. {
  633. case BOTTOMRIGHT:
  634. init(p.x - Bitmap->w, p.y - Bitmap->h);
  635. break;
  636. case CENTER:
  637. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  638. break;
  639. case TOPLEFT:
  640. init(p.x, p.y);
  641. break;
  642. default:
  643. assert(0); //not implemented
  644. }
  645. }
  646. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  647. {
  648. CCS->curh->hide();
  649. free=Free;
  650. bitmap=Bitmap;
  651. if(bitmap)
  652. {
  653. pos.x = screen->w/2 - bitmap->w/2;
  654. pos.y = screen->h/2 - bitmap->h/2;
  655. pos.h = bitmap->h;
  656. pos.w = bitmap->w;
  657. }
  658. }
  659. void CInfoPopup::close()
  660. {
  661. if(free)
  662. SDL_FreeSurface(bitmap);
  663. GH.popIntTotally(this);
  664. }
  665. void CInfoPopup::show(SDL_Surface * to)
  666. {
  667. blitAt(bitmap,pos.x,pos.y,to);
  668. }
  669. CInfoPopup::~CInfoPopup()
  670. {
  671. CCS->curh->show();
  672. }
  673. void CInfoPopup::init(int x, int y)
  674. {
  675. CCS->curh->hide();
  676. pos.x = x;
  677. pos.y = y;
  678. pos.h = bitmap->h;
  679. pos.w = bitmap->w;
  680. // Put the window back on screen if necessary
  681. vstd::amax(pos.x, 0);
  682. vstd::amax(pos.y, 0);
  683. vstd::amin(pos.x, screen->w - bitmap->w);
  684. vstd::amin(pos.y, screen->h - bitmap->h);
  685. }
  686. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  687. image(nullptr),
  688. perDay(false)
  689. {
  690. addUsedEvents(RCLICK);
  691. init(Type, Subtype, Val, imageSize);
  692. }
  693. CComponent::CComponent(const Component &c):
  694. image(nullptr),
  695. perDay(false)
  696. {
  697. addUsedEvents(RCLICK);
  698. if(c.id == Component::RESOURCE && c.when==-1)
  699. perDay = true;
  700. init((Etype)c.id,c.subtype,c.val, large);
  701. }
  702. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  703. {
  704. OBJ_CONSTRUCTION_CAPTURING_ALL;
  705. compType = Type;
  706. subtype = Subtype;
  707. val = Val;
  708. size = imageSize;
  709. assert(compType < typeInvalid);
  710. assert(size < sizeInvalid);
  711. setSurface(getFileName()[size], getIndex());
  712. pos.w = image->pos.w;
  713. pos.h = image->pos.h;
  714. pos.h += 4; //distance between text and image
  715. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), FONT_SMALL);
  716. BOOST_FOREACH(auto & line, textLines)
  717. {
  718. int height = graphics->fonts[FONT_SMALL]->height;
  719. CLabel * label = new CLabel(pos.w/2, pos.h + height/2, FONT_SMALL, CENTER, Colors::Cornsilk, line);
  720. pos.h += height;
  721. if (label->pos.w > pos.w)
  722. {
  723. pos.x -= (label->pos.w - pos.w)/2;
  724. pos.w = label->pos.w;
  725. }
  726. }
  727. }
  728. const std::vector<std::string> CComponent::getFileName()
  729. {
  730. static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  731. static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  732. static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
  733. static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
  734. static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
  735. static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
  736. static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  737. static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  738. static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
  739. static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
  740. auto gen = [](const std::string * arr)
  741. {
  742. return std::vector<std::string>(arr, arr + 4);
  743. };
  744. switch(compType)
  745. {
  746. case primskill: return gen(primSkillsArr);
  747. case secskill: return gen(secSkillsArr);
  748. case resource: return gen(resourceArr);
  749. case creature: return gen(creatureArr);
  750. case artifact: return gen(artifactArr);
  751. case experience: return gen(primSkillsArr);
  752. case spell: return gen(spellsArr);
  753. case morale: return gen(moraleArr);
  754. case luck: return gen(luckArr);
  755. case building: return std::vector<std::string>(4, CGI->townh->towns[subtype].clientInfo.buildingsIcons);
  756. case hero: return gen(heroArr);
  757. case flag: return gen(flagArr);
  758. }
  759. assert(0);
  760. return std::vector<std::string>();
  761. }
  762. size_t CComponent::getIndex()
  763. {
  764. switch(compType)
  765. {
  766. case primskill: return subtype;
  767. case secskill: return subtype*3 + 3 + val - 1;
  768. case resource: return subtype;
  769. case creature: return subtype+2;
  770. case artifact: return subtype;
  771. case experience: return 4;
  772. case spell: return subtype;
  773. case morale: return val+3;
  774. case luck: return val+3;
  775. case building: return val;
  776. case hero: return subtype;
  777. case flag: return subtype;
  778. }
  779. assert(0);
  780. return 0;
  781. }
  782. std::string CComponent::getDescription()
  783. {
  784. switch (compType)
  785. {
  786. case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
  787. : CGI->generaltexth->allTexts[149]; //mana
  788. case secskill: return CGI->generaltexth->skillInfoTexts[subtype][val-1];
  789. case resource: return CGI->generaltexth->allTexts[242];
  790. case creature: return "";
  791. case artifact: return CGI->arth->artifacts[subtype]->Description();
  792. case experience: return CGI->generaltexth->allTexts[241];
  793. case spell: return CGI->spellh->spells[subtype]->descriptions[val];
  794. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  795. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  796. case building: return CGI->townh->towns[subtype].buildings[val]->Description();
  797. case hero: return CGI->heroh->heroes[subtype]->name;
  798. case flag: return "";
  799. }
  800. assert(0);
  801. return "";
  802. }
  803. std::string CComponent::getSubtitle()
  804. {
  805. if (!perDay)
  806. return getSubtitleInternal();
  807. std::string ret = CGI->generaltexth->allTexts[3];
  808. boost::replace_first(ret, "%d", getSubtitleInternal());
  809. return ret;
  810. }
  811. std::string CComponent::getSubtitleInternal()
  812. {
  813. //FIXME: some of these are horrible (e.g creature)
  814. switch(compType)
  815. {
  816. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  817. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->generaltexth->skillName[subtype];
  818. case resource: return boost::lexical_cast<std::string>(val);
  819. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  820. case artifact: return CGI->arth->artifacts[subtype]->Name();
  821. case experience: return (subtype && val==1) ? CGI->generaltexth->allTexts[442] : boost::lexical_cast<std::string>(val);
  822. case spell: return CGI->spellh->spells[subtype]->name;
  823. case morale: return "";
  824. case luck: return "";
  825. case building: return CGI->townh->towns[subtype].buildings[val]->Name();
  826. case hero: return CGI->heroh->heroes[subtype]->name;
  827. case flag: return CGI->generaltexth->capColors[subtype];
  828. }
  829. assert(0);
  830. return "";
  831. }
  832. void CComponent::setSurface(std::string defName, int imgPos)
  833. {
  834. OBJ_CONSTRUCTION_CAPTURING_ALL;
  835. vstd::clear_pointer(image);
  836. image = new CAnimImage(defName, imgPos);
  837. }
  838. void CComponent::clickRight(tribool down, bool previousState)
  839. {
  840. if(!getDescription().empty())
  841. adventureInt->handleRightClick(getDescription(), down);
  842. }
  843. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  844. {
  845. if (down)
  846. {
  847. if(onSelect)
  848. onSelect();
  849. }
  850. }
  851. void CSelectableComponent::init()
  852. {
  853. selected = false;
  854. }
  855. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect):
  856. CComponent(c),onSelect(OnSelect)
  857. {
  858. type |= REDRAW_PARENT;
  859. addUsedEvents(LCLICK | KEYBOARD);
  860. init();
  861. }
  862. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, boost::function<void()> OnSelect):
  863. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  864. {
  865. type |= REDRAW_PARENT;
  866. addUsedEvents(LCLICK | KEYBOARD);
  867. init();
  868. }
  869. void CSelectableComponent::select(bool on)
  870. {
  871. if(on != selected)
  872. {
  873. selected = on;
  874. redraw();
  875. }
  876. }
  877. void CSelectableComponent::showAll(SDL_Surface * to)
  878. {
  879. CComponent::showAll(to);
  880. if(selected)
  881. {
  882. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  883. }
  884. }
  885. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  886. {
  887. if (newSelection == selected)
  888. return;
  889. if (selected)
  890. selected->select(false);
  891. selected = newSelection;
  892. if (onSelect)
  893. onSelect(selectedIndex());
  894. if (selected)
  895. selected->select(true);
  896. }
  897. int CComponentBox::selectedIndex()
  898. {
  899. if (selected)
  900. return std::find(components.begin(), components.end(), selected) - components.begin();
  901. return -1;
  902. }
  903. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  904. {
  905. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  906. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  907. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  908. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  909. }
  910. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  911. {
  912. static const int betweenImagesMin = 50;
  913. static const int betweenSubtitlesMin = 10;
  914. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  915. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  916. int subtitlesOffset = leftSubtitle + rightSubtitle;
  917. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  918. }
  919. void CComponentBox::placeComponents(bool selectable)
  920. {
  921. static const int betweenRows = 22;
  922. OBJ_CONSTRUCTION_CAPTURING_ALL;
  923. if (components.empty())
  924. return;
  925. //prepare components
  926. BOOST_FOREACH(auto & comp, components)
  927. {
  928. addChild(comp);
  929. comp->moveTo(Point(pos.x, pos.y));
  930. }
  931. struct RowData
  932. {
  933. size_t comps;
  934. int width;
  935. int height;
  936. RowData (size_t Comps, int Width, int Height):
  937. comps(Comps), width (Width), height (Height){};
  938. };
  939. std::vector<RowData> rows;
  940. rows.push_back (RowData (0,0,0));
  941. //split components in rows
  942. CComponent * prevComp = nullptr;
  943. BOOST_FOREACH(CComponent * comp, components)
  944. {
  945. //make sure that components are smaller than our width
  946. //assert(pos.w == 0 || pos.w < comp->pos.w);
  947. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  948. //start next row
  949. if (pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w)
  950. {
  951. prevComp = nullptr;
  952. rows.push_back (RowData (0,0,0));
  953. }
  954. if (prevComp)
  955. rows.back().width += distance;
  956. rows.back().comps++;
  957. rows.back().width += comp->pos.w;
  958. vstd::amax(rows.back().height, comp->pos.h);
  959. prevComp = comp;
  960. }
  961. if (pos.w == 0)
  962. {
  963. BOOST_FOREACH(auto & row, rows)
  964. vstd::amax(pos.w, row.width);
  965. }
  966. int height = (rows.size() - 1) * betweenRows;
  967. BOOST_FOREACH(auto & row, rows)
  968. height += row.height;
  969. //assert(pos.h == 0 || pos.h < height);
  970. if (pos.h == 0)
  971. pos.h = height;
  972. auto iter = components.begin();
  973. int currentY = (pos.h - height) / 2;
  974. //move components to their positions
  975. for (size_t row = 0; row < rows.size(); row++)
  976. {
  977. prevComp = nullptr;
  978. int currentX = (pos.w - rows[row].width) / 2;
  979. for (size_t col = 0; col < rows[row].comps; col++)
  980. {
  981. if (prevComp)
  982. {
  983. if (selectable)
  984. {
  985. Point orPos = Point(currentX, currentY) + getOrTextPos(prevComp, *iter);
  986. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::Cornsilk, CGI->generaltexth->allTexts[4]);
  987. }
  988. currentX += getDistance(prevComp, *iter);
  989. }
  990. (*iter)->moveBy(Point(currentX, currentY));
  991. currentX += (*iter)->pos.w;
  992. prevComp = *(iter++);
  993. }
  994. currentY += rows[row].height + betweenRows;
  995. }
  996. }
  997. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  998. components(1, _components),
  999. selected(nullptr)
  1000. {
  1001. type |= REDRAW_PARENT;
  1002. pos = position + pos;
  1003. placeComponents(false);
  1004. }
  1005. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  1006. components(_components),
  1007. selected(nullptr)
  1008. {
  1009. type |= REDRAW_PARENT;
  1010. pos = position + pos;
  1011. placeComponents(false);
  1012. }
  1013. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, boost::function<void(int newID)> _onSelect):
  1014. components(_components.begin(), _components.end()),
  1015. selected(nullptr),
  1016. onSelect(_onSelect)
  1017. {
  1018. type |= REDRAW_PARENT;
  1019. pos = position + pos;
  1020. placeComponents(true);
  1021. assert(!components.empty());
  1022. int key = SDLK_1;
  1023. BOOST_FOREACH(auto & comp, _components)
  1024. {
  1025. comp->onSelect = boost::bind(&CComponentBox::selectionChanged, this, comp);
  1026. comp->assignedKeys.insert(key++);
  1027. }
  1028. selectionChanged(_components.front());
  1029. }
  1030. void CSelWindow::selectionChange(unsigned to)
  1031. {
  1032. for (unsigned i=0;i<components.size();i++)
  1033. {
  1034. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  1035. if (!pom)
  1036. continue;
  1037. pom->select(i==to);
  1038. }
  1039. redraw();
  1040. }
  1041. 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)
  1042. {
  1043. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1044. ID = askID;
  1045. for(int i=0;i<Buttons.size();i++)
  1046. {
  1047. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  1048. if(!i && askID >= 0)
  1049. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  1050. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  1051. }
  1052. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::Cornsilk);
  1053. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  1054. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  1055. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  1056. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb,0,askID);
  1057. for(int i=0;i<comps.size();i++)
  1058. {
  1059. comps[i]->recActions = 255;
  1060. addChild(comps[i]);
  1061. components.push_back(comps[i]);
  1062. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  1063. if(i<9)
  1064. comps[i]->assignedKeys.insert(SDLK_1+i);
  1065. }
  1066. CMessage::drawIWindow(this, Text, player);
  1067. }
  1068. void CSelWindow::madeChoice()
  1069. {
  1070. if(ID < 0)
  1071. return;
  1072. int ret = -1;
  1073. for (int i=0;i<components.size();i++)
  1074. {
  1075. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  1076. {
  1077. ret = i;
  1078. }
  1079. }
  1080. LOCPLINT->cb->selectionMade(ret+1,ID);
  1081. }
  1082. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  1083. {
  1084. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1085. pos.x+=x;
  1086. pos.y+=y;
  1087. assert(vstd::contains(CGI->townh->factions, cre->faction));
  1088. if(Big)
  1089. bg = new CPicture(CGI->townh->factions[cre->faction].creatureBg130);
  1090. else
  1091. bg = new CPicture(CGI->townh->factions[cre->faction].creatureBg120);
  1092. bg->needRefresh = true;
  1093. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  1094. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  1095. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  1096. pos.w = bg->pos.w;
  1097. pos.h = bg->pos.h;
  1098. }
  1099. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow *window, const CCreature *crea, int totalAmount):
  1100. CIntObject(LCLICK | RCLICK),
  1101. parent(window),
  1102. selected(false),
  1103. creature(crea),
  1104. amount(totalAmount)
  1105. {
  1106. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1107. pic = new CCreaturePic(1,1, creature, true, true);
  1108. // 1 + 1 px for borders
  1109. pos.w = pic->pos.w + 2;
  1110. pos.h = pic->pos.h + 2;
  1111. }
  1112. void CRecruitmentWindow::CCreatureCard::select(bool on)
  1113. {
  1114. selected = on;
  1115. redraw();
  1116. }
  1117. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  1118. {
  1119. if (down)
  1120. parent->select(this);
  1121. }
  1122. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  1123. {
  1124. if (down)
  1125. GH.pushInt(createCreWindow(creature->idNumber, 0, 0));
  1126. }
  1127. void CRecruitmentWindow::CCreatureCard::showAll(SDL_Surface * to)
  1128. {
  1129. CIntObject::showAll(to);
  1130. if (selected)
  1131. drawBorder(to, pos, int3(248, 0, 0));
  1132. else
  1133. drawBorder(to, pos, int3(232, 212, 120));
  1134. }
  1135. CRecruitmentWindow::CCostBox::CCostBox(Rect position, std::string title)
  1136. {
  1137. type |= REDRAW_PARENT;
  1138. pos = position + pos;
  1139. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1140. new CLabel(pos.w/2, 10, FONT_SMALL, CENTER, Colors::Cornsilk, title);
  1141. }
  1142. void CRecruitmentWindow::CCostBox::set(TResources res)
  1143. {
  1144. //just update values
  1145. BOOST_FOREACH(auto & item, resources)
  1146. {
  1147. item.second.first->setTxt(boost::lexical_cast<std::string>(res[item.first]));
  1148. }
  1149. }
  1150. void CRecruitmentWindow::CCostBox::createItems(TResources res)
  1151. {
  1152. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1153. BOOST_FOREACH(auto & curr, resources)
  1154. {
  1155. delete curr.second.first;
  1156. delete curr.second.second;
  1157. }
  1158. resources.clear();
  1159. TResources::nziterator iter(res);
  1160. while (iter.valid())
  1161. {
  1162. CAnimImage * image = new CAnimImage("RESOURCE", iter->resType);
  1163. CLabel * text = new CLabel(15, 43, FONT_SMALL, CENTER, Colors::Cornsilk, "0");
  1164. resources.insert(std::make_pair(iter->resType, std::make_pair(text, image)));
  1165. iter++;
  1166. }
  1167. if (!resources.empty())
  1168. {
  1169. int curx = pos.w / 2 - (16 * resources.size()) - (8 * (resources.size() - 1));
  1170. //reverse to display gold as first resource
  1171. BOOST_REVERSE_FOREACH(auto & res, resources)
  1172. {
  1173. res.second.first->moveBy(Point(curx, 22));
  1174. res.second.second->moveBy(Point(curx, 22));
  1175. curx += 48;
  1176. }
  1177. }
  1178. redraw();
  1179. }
  1180. void CRecruitmentWindow::select(CCreatureCard *card)
  1181. {
  1182. if (card == selected)
  1183. return;
  1184. if (selected)
  1185. selected->select(false);
  1186. selected = card;
  1187. if (selected)
  1188. selected->select(true);
  1189. if (card)
  1190. {
  1191. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  1192. vstd::amin(maxAmount, card->amount);
  1193. slider->setAmount(maxAmount);
  1194. if(slider->value)
  1195. slider->moveTo(0);
  1196. else // if slider already at 0 - emulate call to sliderMoved()
  1197. sliderMoved(0);
  1198. costPerTroopValue->createItems(card->creature->cost);
  1199. totalCostValue->createItems(card->creature->cost);
  1200. costPerTroopValue->set(card->creature->cost);
  1201. //Recruit %s
  1202. title->setTxt(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->namePl));
  1203. maxButton->block(maxAmount == 0);
  1204. slider->block(maxAmount == 0);
  1205. }
  1206. }
  1207. void CRecruitmentWindow::buy()
  1208. {
  1209. int crid = selected->creature->idNumber,
  1210. dstslot = dst-> getSlotFor(crid);
  1211. if(dstslot < 0 && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->convertMachineID(crid, true))) //no available slot
  1212. {
  1213. std::string txt;
  1214. if(dst->ID == GameConstants::HEROI_TYPE)
  1215. {
  1216. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1217. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1218. }
  1219. else
  1220. {
  1221. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1222. }
  1223. LOCPLINT->showInfoDialog(txt);
  1224. return;
  1225. }
  1226. onRecruit(crid, slider->value);
  1227. if(level >= 0)
  1228. close();
  1229. }
  1230. void CRecruitmentWindow::showAll(SDL_Surface * to)
  1231. {
  1232. CWindowObject::showAll(to);
  1233. // recruit\total values
  1234. drawBorder(to, pos.x + 172, pos.y + 222, 67, 42, int3(239,215,123));
  1235. drawBorder(to, pos.x + 246, pos.y + 222, 67, 42, int3(239,215,123));
  1236. //cost boxes
  1237. drawBorder(to, pos.x + 64, pos.y + 222, 99, 76, int3(239,215,123));
  1238. drawBorder(to, pos.x + 322, pos.y + 222, 99, 76, int3(239,215,123));
  1239. //buttons borders
  1240. drawBorder(to, pos.x + 133, pos.y + 312, 66, 34, int3(173,142,66));
  1241. drawBorder(to, pos.x + 211, pos.y + 312, 66, 34, int3(173,142,66));
  1242. drawBorder(to, pos.x + 289, pos.y + 312, 66, 34, int3(173,142,66));
  1243. }
  1244. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit, int y_offset):
  1245. CWindowObject(PLAYER_COLORED, "TPRCRT"),
  1246. onRecruit(Recruit),
  1247. level(Level),
  1248. dst(Dst),
  1249. selected(nullptr),
  1250. dwelling(Dwelling)
  1251. {
  1252. moveBy(Point(0, y_offset));
  1253. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1254. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1255. slider = new CSlider(176,279,135,0,0,0,0,true);
  1256. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  1257. maxButton = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CSlider::moveToMax,slider),134,313,"IRCBTNS.DEF",SDLK_m);
  1258. buyButton = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  1259. cancelButton = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::close,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  1260. title = new CLabel(243, 32, FONT_BIG, CENTER, Colors::Jasmine);
  1261. availableValue = new CLabel(205, 253, FONT_SMALL, CENTER, Colors::Cornsilk);
  1262. toRecruitValue = new CLabel(279, 253, FONT_SMALL, CENTER, Colors::Cornsilk);
  1263. costPerTroopValue = new CCostBox(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  1264. totalCostValue = new CCostBox(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  1265. new CLabel(205, 233, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth->allTexts[465]); //available t
  1266. new CLabel(279, 233, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth->allTexts[16]); //recruit t
  1267. availableCreaturesChanged();
  1268. }
  1269. void CRecruitmentWindow::availableCreaturesChanged()
  1270. {
  1271. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1272. size_t selectedIndex = 0;
  1273. if (!cards.empty() && selected) // find position of selected item
  1274. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  1275. //deselect card
  1276. select(nullptr);
  1277. //delete old cards
  1278. BOOST_FOREACH(auto & card, cards)
  1279. delete card;
  1280. cards.clear();
  1281. for(int i=0; i<dwelling->creatures.size(); i++)
  1282. {
  1283. //find appropriate level
  1284. if(level >= 0 && i != level)
  1285. continue;
  1286. int amount = dwelling->creatures[i].first;
  1287. //create new cards
  1288. BOOST_REVERSE_FOREACH(auto & creature, dwelling->creatures[i].second)
  1289. cards.push_back(new CCreatureCard(this, CGI->creh->creatures[creature], amount));
  1290. }
  1291. assert(!cards.empty());
  1292. const int creatureWidth = 102;
  1293. //normal distance between cards - 18px
  1294. int requiredSpace = 18;
  1295. //maximum distance we can use without reaching window borders
  1296. int availableSpace = pos.w - 50 - creatureWidth * cards.size();
  1297. if (cards.size() > 1) // avoid division by zero
  1298. availableSpace /= cards.size() - 1;
  1299. else
  1300. availableSpace = 0;
  1301. assert(availableSpace >= 0);
  1302. const int spaceBetween = std::min(requiredSpace, availableSpace);
  1303. const int totalCreatureWidth = spaceBetween + creatureWidth;
  1304. //now we know total amount of cards and can move them to correct position
  1305. int curx = pos.w / 2 - (creatureWidth*cards.size()/2) - (spaceBetween*(cards.size()-1)/2);
  1306. BOOST_FOREACH(auto & card, cards)
  1307. {
  1308. card->moveBy(Point(curx, 64));
  1309. curx += totalCreatureWidth;
  1310. }
  1311. //restore selection
  1312. select(cards[selectedIndex]);
  1313. if(slider->value)
  1314. slider->moveTo(0);
  1315. else // if slider already at 0 - emulate call to sliderMoved()
  1316. sliderMoved(0);
  1317. }
  1318. void CRecruitmentWindow::sliderMoved(int to)
  1319. {
  1320. if (!selected)
  1321. return;
  1322. buyButton->block(!to);
  1323. availableValue->setTxt(boost::lexical_cast<std::string>(selected->amount - to));
  1324. toRecruitValue->setTxt(boost::lexical_cast<std::string>(to));
  1325. totalCostValue->set(selected->creature->cost * to);
  1326. }
  1327. CSplitWindow::CSplitWindow(const CCreature * creature, boost::function<void(int, int)> callback_,
  1328. int leftMin_, int rightMin_, int leftAmount_, int rightAmount_):
  1329. CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  1330. callback(callback_),
  1331. leftAmount(leftAmount_),
  1332. rightAmount(rightAmount_),
  1333. leftMin(leftMin_),
  1334. rightMin(rightMin_),
  1335. slider(nullptr)
  1336. {
  1337. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1338. int total = leftAmount + rightAmount;
  1339. int leftMax = total - rightMin;
  1340. int rightMax = total - leftMin;
  1341. ok = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::apply, this), 20, 263, "IOK6432", SDLK_RETURN);
  1342. cancel = new CAdventureMapButton("", "", boost::bind(&CSplitWindow::close, this), 214, 263, "ICN6432", SDLK_ESCAPE);
  1343. int sliderPositions = total - leftMin - rightMin;
  1344. leftInput = new CTextInput(Rect(20, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, true));
  1345. rightInput = new CTextInput(Rect(176, 218, 100, 36), FONT_BIG, boost::bind(&CSplitWindow::setAmountText, this, _1, false));
  1346. //add filters to allow only number input
  1347. leftInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax));
  1348. rightInput->filters.add(boost::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax));
  1349. leftInput->setTxt(boost::lexical_cast<std::string>(leftAmount), false);
  1350. rightInput->setTxt(boost::lexical_cast<std::string>(rightAmount), false);
  1351. animLeft = new CCreaturePic(20, 54, creature, true, false);
  1352. animRight = new CCreaturePic(177, 54,creature, true, false);
  1353. slider = new CSlider(21, 194, 257, boost::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPositions, rightAmount - rightMin, true);
  1354. std::string title = CGI->generaltexth->allTexts[256];
  1355. boost::algorithm::replace_first(title,"%s", creature->namePl);
  1356. new CLabel(150, 34, FONT_BIG, CENTER, Colors::Jasmine, title);
  1357. }
  1358. void CSplitWindow::setAmountText(std::string text, bool left)
  1359. {
  1360. try
  1361. {
  1362. setAmount(boost::lexical_cast<int>(text), left);
  1363. slider->moveTo(rightAmount - rightMin);
  1364. }
  1365. catch(boost::bad_lexical_cast &)
  1366. {
  1367. }
  1368. }
  1369. void CSplitWindow::setAmount(int value, bool left)
  1370. {
  1371. int total = leftAmount + rightAmount;
  1372. leftAmount = left ? value : total - value;
  1373. rightAmount = left ? total - value : value;
  1374. leftInput->setTxt(boost::lexical_cast<std::string>(leftAmount));
  1375. rightInput->setTxt(boost::lexical_cast<std::string>(rightAmount));
  1376. }
  1377. void CSplitWindow::apply()
  1378. {
  1379. callback(leftAmount, rightAmount);
  1380. close();
  1381. }
  1382. void CSplitWindow::sliderMoved(int to)
  1383. {
  1384. setAmount(rightMin + to, false);
  1385. }
  1386. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> callback):
  1387. CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  1388. cb(callback)
  1389. {
  1390. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1391. LOCPLINT->showingDialog->setn(true);
  1392. new CAnimImage("PortraitsLarge", hero->portrait, 0, 170, 66);
  1393. new CAdventureMapButton("", "", boost::bind(&CLevelWindow::close, this), 297, 413, "IOKAY", SDLK_RETURN);
  1394. //%s has gained a level.
  1395. new CLabel(192, 33, FONT_MEDIUM, CENTER, Colors::Cornsilk,
  1396. boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->name));
  1397. //%s is now a level %d %s.
  1398. new CLabel(192, 162, FONT_MEDIUM, CENTER, Colors::Cornsilk,
  1399. boost::str(boost::format(CGI->generaltexth->allTexts[445]) % hero->name % hero->level % hero->type->heroClass->name));
  1400. new CAnimImage("PSKIL42", pskill, 0, 174, 190);
  1401. new CLabel(192, 253, FONT_MEDIUM, CENTER, Colors::Cornsilk,
  1402. CGI->generaltexth->primarySkillNames[pskill] + " +1");
  1403. if (!skills.empty())
  1404. {
  1405. std::vector<CSelectableComponent *> comps;
  1406. for(size_t i=0;i<skills.size();i++)
  1407. {
  1408. comps.push_back(new CSelectableComponent(
  1409. CComponent::secskill,
  1410. skills[i],
  1411. hero->getSecSkillLevel( static_cast<CGHeroInstance::SecondarySkill>(skills[i]) )+1,
  1412. CComponent::medium));
  1413. }
  1414. box = new CComponentBox(comps, Rect(25, 300, pos.w - 50, 100));
  1415. }
  1416. else
  1417. box = nullptr;
  1418. }
  1419. CLevelWindow::~CLevelWindow()
  1420. {
  1421. //FIXME: call callback if there was nothing to select?
  1422. if (box && box->selectedIndex() != -1)
  1423. cb(box->selectedIndex());
  1424. LOCPLINT->showingDialog->setn(false);
  1425. }
  1426. void CMinorResDataBar::show(SDL_Surface * to)
  1427. {
  1428. }
  1429. void CMinorResDataBar::showAll(SDL_Surface * to)
  1430. {
  1431. blitAt(bg,pos.x,pos.y,to);
  1432. char buf[30];
  1433. for (int i=0;i<7;i++)
  1434. {
  1435. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  1436. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,Colors::Cornsilk,to);
  1437. }
  1438. std::vector<std::string> temp;
  1439. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  1440. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  1441. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  1442. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  1443. CGI->generaltexth->allTexts[62]
  1444. +": %s, "
  1445. + CGI->generaltexth->allTexts[63]
  1446. + ": %s, "
  1447. + CGI->generaltexth->allTexts[64]
  1448. + ": %s",temp)
  1449. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,Colors::Cornsilk,to);
  1450. }
  1451. CMinorResDataBar::CMinorResDataBar()
  1452. {
  1453. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  1454. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1455. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1456. pos.x = 7;
  1457. pos.y = 575;
  1458. pos.w = bg->w;
  1459. pos.h = bg->h;
  1460. }
  1461. CMinorResDataBar::~CMinorResDataBar()
  1462. {
  1463. SDL_FreeSurface(bg);
  1464. }
  1465. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  1466. parent(_parent),
  1467. index(_id)
  1468. {
  1469. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1470. border = new CPicture("TPGATES");
  1471. pos = border->pos;
  1472. addUsedEvents(LCLICK);
  1473. type |= REDRAW_PARENT;
  1474. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::Cornsilk, _text);
  1475. select(index == parent->selected);
  1476. }
  1477. void CObjectListWindow::CItem::select(bool on)
  1478. {
  1479. if (on)
  1480. border->recActions = 255;
  1481. else
  1482. border->recActions = ~(UPDATE | SHOWALL);
  1483. redraw();
  1484. }
  1485. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  1486. {
  1487. if( previousState && !down)
  1488. parent->changeSelection(index);
  1489. }
  1490. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1491. boost::function<void(int)> Callback):
  1492. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1493. onSelect(Callback)
  1494. {
  1495. items.reserve(_items.size());
  1496. BOOST_FOREACH(int id, _items)
  1497. {
  1498. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->hoverName));
  1499. }
  1500. init(titlePic, _title, _descr);
  1501. }
  1502. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1503. boost::function<void(int)> Callback):
  1504. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1505. onSelect(Callback)
  1506. {
  1507. items.reserve(_items.size());
  1508. for (size_t i=0; i<_items.size(); i++)
  1509. items.push_back(std::make_pair(int(i), _items[i]));
  1510. init(titlePic, _title, _descr);
  1511. }
  1512. void CObjectListWindow::init(CPicture * titlePic, std::string _title, std::string _descr)
  1513. {
  1514. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1515. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::Jasmine, _title);
  1516. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::Cornsilk, _descr);
  1517. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  1518. ok->block(true);
  1519. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  1520. if (titlePic)
  1521. {
  1522. titleImage = titlePic;
  1523. addChild(titleImage);
  1524. titleImage->recActions = defActions;
  1525. titleImage->pos.x = pos.w/2 + pos.x - titleImage->pos.w/2;
  1526. titleImage->pos.y =75 + pos.y - titleImage->pos.h/2;
  1527. }
  1528. list = new CListBox(boost::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  1529. Point(15, 152), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1530. list->type |= REDRAW_PARENT;
  1531. }
  1532. CIntObject * CObjectListWindow::genItem(size_t index)
  1533. {
  1534. if (index < items.size())
  1535. return new CItem(this, index, items[index].second);
  1536. return NULL;
  1537. }
  1538. void CObjectListWindow::elementSelected()
  1539. {
  1540. boost::function<void(int)> toCall = onSelect;//save
  1541. int where = items[selected].first; //required variables
  1542. GH.popIntTotally(this);//then destroy window
  1543. toCall(where);//and send selected object
  1544. }
  1545. void CObjectListWindow::changeSelection(size_t which)
  1546. {
  1547. ok->block(false);
  1548. if (selected == which)
  1549. return;
  1550. std::list< CIntObject * > elements = list->getItems();
  1551. BOOST_FOREACH(CIntObject * element, elements)
  1552. {
  1553. CItem *item;
  1554. if ( (item = dynamic_cast<CItem*>(element)) )
  1555. if (item->index == selected)
  1556. item->select(false);
  1557. if (item->index == which)
  1558. item->select(true);
  1559. }
  1560. selected = which;
  1561. }
  1562. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  1563. {
  1564. if(key.state != SDL_PRESSED)
  1565. return;
  1566. int sel = selected;
  1567. switch(key.keysym.sym)
  1568. {
  1569. break; case SDLK_UP:
  1570. sel -=1;
  1571. break; case SDLK_DOWN:
  1572. sel +=1;
  1573. break; case SDLK_PAGEUP:
  1574. sel -=9;
  1575. break; case SDLK_PAGEDOWN:
  1576. sel +=9;
  1577. break; case SDLK_HOME:
  1578. sel = 0;
  1579. break; case SDLK_END:
  1580. sel = items.size();
  1581. break; default:
  1582. return;
  1583. }
  1584. vstd::abetween(sel, 0, items.size()-1);
  1585. list->scrollTo(sel);
  1586. changeSelection(sel);
  1587. }
  1588. CTradeWindow::CTradeableItem::CTradeableItem( EType Type, int ID, bool Left, int Serial)
  1589. {
  1590. serial = Serial;
  1591. left = Left;
  1592. type = Type;
  1593. id = ID;
  1594. addUsedEvents(LCLICK | HOVER | RCLICK);
  1595. downSelection = false;
  1596. hlp = NULL;
  1597. }
  1598. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  1599. {
  1600. Point posToBitmap;
  1601. Point posToSubCenter;
  1602. switch(type)
  1603. {
  1604. case RESOURCE:
  1605. posToBitmap = Point(19,9);
  1606. posToSubCenter = Point(36, 59);
  1607. break;
  1608. case CREATURE_PLACEHOLDER:
  1609. case CREATURE:
  1610. posToSubCenter = Point(29, 76);
  1611. if(downSelection)
  1612. posToSubCenter.y += 5;
  1613. break;
  1614. case PLAYER:
  1615. posToSubCenter = Point(31, 76);
  1616. break;
  1617. case ARTIFACT_PLACEHOLDER:
  1618. case ARTIFACT_INSTANCE:
  1619. posToSubCenter = Point(19, 55);
  1620. if(downSelection)
  1621. posToSubCenter.y += 8;
  1622. break;
  1623. case ARTIFACT_TYPE:
  1624. posToSubCenter = Point(19, 58);
  1625. break;
  1626. }
  1627. if(SDL_Surface *hlp = getSurface())
  1628. blitAt(hlp, pos + posToBitmap, to);
  1629. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::Cornsilk, to);
  1630. }
  1631. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  1632. {
  1633. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  1634. assert(mw);
  1635. if(down)
  1636. {
  1637. if(type == ARTIFACT_PLACEHOLDER)
  1638. {
  1639. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  1640. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  1641. {
  1642. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  1643. }
  1644. else if(const CArtifactInstance *art = getArtInstance())
  1645. {
  1646. aw->arts->commonInfo->src.AOH = aw->arts;
  1647. aw->arts->commonInfo->src.art = art;
  1648. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  1649. aw->arts->markPossibleSlots(art);
  1650. //aw->arts->commonInfo->dst.AOH = aw->arts;
  1651. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[art->artType->id].bitmap);
  1652. aw->arts->artifactsOnAltar.erase(art);
  1653. id = -1;
  1654. subtitle = "";
  1655. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  1656. }
  1657. aw->calcTotalExp();
  1658. return;
  1659. }
  1660. if(left)
  1661. {
  1662. if(mw->hLeft != this)
  1663. mw->hLeft = this;
  1664. else
  1665. return;
  1666. }
  1667. else
  1668. {
  1669. if(mw->hRight != this)
  1670. mw->hRight = this;
  1671. else
  1672. return;
  1673. }
  1674. mw->selectionChanged(left);
  1675. }
  1676. }
  1677. SDL_Surface * CTradeWindow::CTradeableItem::getSurface()
  1678. {
  1679. switch(type)
  1680. {
  1681. case RESOURCE:
  1682. return graphics->resources32->ourImages[id].bitmap;
  1683. case PLAYER:
  1684. return graphics->flags->ourImages[id].bitmap;
  1685. case ARTIFACT_TYPE:
  1686. case ARTIFACT_PLACEHOLDER:
  1687. case ARTIFACT_INSTANCE:
  1688. return id >= 0 ? graphics->artDefs->ourImages[id].bitmap : NULL;
  1689. case CREATURE:
  1690. return graphics->bigImgs[id];
  1691. default:
  1692. return NULL;
  1693. }
  1694. }
  1695. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  1696. {
  1697. Rect oldPos = pos;
  1698. std::string oldSub = subtitle;
  1699. downSelection = true;
  1700. pos = dstPos;
  1701. subtitle = customSub;
  1702. showAll(to);
  1703. downSelection = false;
  1704. pos = oldPos;
  1705. subtitle = oldSub;
  1706. }
  1707. void CTradeWindow::CTradeableItem::hover(bool on)
  1708. {
  1709. if(!on)
  1710. {
  1711. GH.statusbar->clear();
  1712. return;
  1713. }
  1714. switch(type)
  1715. {
  1716. case CREATURE:
  1717. case CREATURE_PLACEHOLDER:
  1718. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1719. break;
  1720. case ARTIFACT_PLACEHOLDER:
  1721. if(id < 0)
  1722. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  1723. else
  1724. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  1725. break;
  1726. }
  1727. }
  1728. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  1729. {
  1730. if(down)
  1731. {
  1732. switch(type)
  1733. {
  1734. case CREATURE:
  1735. case CREATURE_PLACEHOLDER:
  1736. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  1737. break;
  1738. case ARTIFACT_TYPE:
  1739. case ARTIFACT_PLACEHOLDER:
  1740. if(id >= 0)
  1741. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  1742. break;
  1743. }
  1744. }
  1745. }
  1746. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  1747. {
  1748. switch(type)
  1749. {
  1750. case PLAYER:
  1751. return CGI->generaltexth->capColors[id];
  1752. case RESOURCE:
  1753. return CGI->generaltexth->restypes[id];
  1754. case CREATURE:
  1755. if(number == 1)
  1756. return CGI->creh->creatures[id]->nameSing;
  1757. else
  1758. return CGI->creh->creatures[id]->namePl;
  1759. case ARTIFACT_TYPE:
  1760. case ARTIFACT_INSTANCE:
  1761. return CGI->arth->artifacts[id]->Name();
  1762. }
  1763. assert(0);
  1764. return "";
  1765. }
  1766. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  1767. {
  1768. switch(type)
  1769. {
  1770. case ARTIFACT_PLACEHOLDER:
  1771. case ARTIFACT_INSTANCE:
  1772. return (const CArtifactInstance *)hlp;
  1773. default:
  1774. return NULL;
  1775. }
  1776. }
  1777. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  1778. {
  1779. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  1780. hlp = art;
  1781. if(art)
  1782. id = art->artType->id;
  1783. else
  1784. id = -1;
  1785. }
  1786. CTradeWindow::CTradeWindow(std::string bgName, const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode):
  1787. CWindowObject(PLAYER_COLORED, bgName),
  1788. market(Market),
  1789. hero(Hero),
  1790. arts(NULL),
  1791. hLeft(NULL),
  1792. hRight(NULL),
  1793. readyToTrade(false)
  1794. {
  1795. type |= BLOCK_ADV_HOTKEYS;
  1796. mode = Mode;
  1797. initTypes();
  1798. }
  1799. void CTradeWindow::initTypes()
  1800. {
  1801. switch(mode)
  1802. {
  1803. case EMarketMode::RESOURCE_RESOURCE:
  1804. itemsType[1] = RESOURCE;
  1805. itemsType[0] = RESOURCE;
  1806. break;
  1807. case EMarketMode::RESOURCE_PLAYER:
  1808. itemsType[1] = RESOURCE;
  1809. itemsType[0] = PLAYER;
  1810. break;
  1811. case EMarketMode::CREATURE_RESOURCE:
  1812. itemsType[1] = CREATURE;
  1813. itemsType[0] = RESOURCE;
  1814. break;
  1815. case EMarketMode::RESOURCE_ARTIFACT:
  1816. itemsType[1] = RESOURCE;
  1817. itemsType[0] = ARTIFACT_TYPE;
  1818. break;
  1819. case EMarketMode::ARTIFACT_RESOURCE:
  1820. itemsType[1] = ARTIFACT_INSTANCE;
  1821. itemsType[0] = RESOURCE;
  1822. break;
  1823. case EMarketMode::CREATURE_EXP:
  1824. itemsType[1] = CREATURE;
  1825. itemsType[0] = CREATURE_PLACEHOLDER;
  1826. break;
  1827. case EMarketMode::ARTIFACT_EXP:
  1828. itemsType[1] = ARTIFACT_TYPE;
  1829. itemsType[0] = ARTIFACT_PLACEHOLDER;
  1830. break;
  1831. }
  1832. }
  1833. void CTradeWindow::initItems(bool Left)
  1834. {
  1835. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  1836. {
  1837. int xOffset = 0, yOffset = 0;
  1838. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1839. {
  1840. xOffset = -361;
  1841. yOffset = +46;
  1842. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], -1, 1, 0);
  1843. hlp->recActions &= ~(UPDATE | SHOWALL);
  1844. hlp->pos += Rect(137, 469, 42, 42);
  1845. items[Left].push_back(hlp);
  1846. }
  1847. else //ARTIFACT_EXP
  1848. {
  1849. xOffset = -363;
  1850. yOffset = -12;
  1851. }
  1852. BLOCK_CAPTURING;
  1853. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  1854. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  1855. arts->commonInfo->participants.insert(arts);
  1856. arts->recActions = 255;
  1857. arts->setHero(hero);
  1858. arts->allowedAssembling = false;
  1859. addChild(arts);
  1860. artSets.push_back(arts);
  1861. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  1862. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  1863. return;
  1864. }
  1865. std::vector<int> *ids = getItemsIds(Left);
  1866. std::vector<Rect> pos;
  1867. int amount = -1;
  1868. getPositionsFor(pos, Left, itemsType[Left]);
  1869. if(Left || !ids)
  1870. amount = 7;
  1871. else
  1872. amount = ids->size();
  1873. if(ids)
  1874. vstd::amin(amount, ids->size());
  1875. for(int j=0; j<amount; j++)
  1876. {
  1877. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  1878. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  1879. continue;
  1880. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], id, Left, j);
  1881. hlp->pos = pos[j] + hlp->pos;
  1882. items[Left].push_back(hlp);
  1883. }
  1884. initSubs(Left);
  1885. }
  1886. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  1887. {
  1888. std::vector<int> *ids = NULL;
  1889. if(mode == EMarketMode::ARTIFACT_EXP)
  1890. return new std::vector<int>(22, -1);
  1891. if(Left)
  1892. {
  1893. switch(itemsType[1])
  1894. {
  1895. case CREATURE:
  1896. ids = new std::vector<int>;
  1897. for(int i = 0; i < 7; i++)
  1898. {
  1899. if(const CCreature *c = hero->getCreature(i))
  1900. ids->push_back(c->idNumber);
  1901. else
  1902. ids->push_back(-1);
  1903. }
  1904. break;
  1905. }
  1906. }
  1907. else
  1908. {
  1909. switch(itemsType[0])
  1910. {
  1911. case PLAYER:
  1912. ids = new std::vector<int>;
  1913. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  1914. if(i != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  1915. ids->push_back(i);
  1916. break;
  1917. case ARTIFACT_TYPE:
  1918. ids = new std::vector<int>(market->availableItemsIds(mode));
  1919. break;
  1920. }
  1921. }
  1922. return ids;
  1923. }
  1924. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  1925. {
  1926. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  1927. {
  1928. //22 boxes, 5 in row, last row: two boxes centered
  1929. int h, w, x, y, dx, dy;
  1930. h = w = 44;
  1931. x = 317;
  1932. y = 53;
  1933. dx = 54;
  1934. dy = 70;
  1935. for (int i = 0; i < 4 ; i++)
  1936. for (int j = 0; j < 5 ; j++)
  1937. poss += Rect(x + dx*j, y + dy*i, w, h);
  1938. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  1939. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  1940. }
  1941. else
  1942. {
  1943. //seven boxes:
  1944. // X X X
  1945. // X X X
  1946. // X
  1947. int h, w, x, y, dx, dy;
  1948. int leftToRightOffset;
  1949. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  1950. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  1951. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  1952. genRect(h, w, x + dx, y + 2*dy);
  1953. if(!Left)
  1954. {
  1955. BOOST_FOREACH(Rect &r, poss)
  1956. r.x += leftToRightOffset;
  1957. }
  1958. }
  1959. }
  1960. void CTradeWindow::initSubs(bool Left)
  1961. {
  1962. BOOST_FOREACH(CTradeableItem *t, items[Left])
  1963. {
  1964. if(Left)
  1965. {
  1966. switch(itemsType[1])
  1967. {
  1968. case CREATURE:
  1969. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(t->serial));
  1970. break;
  1971. case RESOURCE:
  1972. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(t->serial));
  1973. break;
  1974. }
  1975. }
  1976. else //right side
  1977. {
  1978. if(itemsType[0] == PLAYER)
  1979. {
  1980. t->subtitle = CGI->generaltexth->capColors[t->id];
  1981. }
  1982. else if(hLeft)//artifact, creature
  1983. {
  1984. int h1, h2; //hlp variables for getting offer
  1985. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  1986. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  1987. {
  1988. std::ostringstream oss;
  1989. oss << h2;
  1990. if(h1!=1)
  1991. oss << "/" << h1;
  1992. t->subtitle = oss.str();
  1993. }
  1994. else
  1995. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  1996. }
  1997. else
  1998. t->subtitle = "";
  1999. }
  2000. }
  2001. }
  2002. void CTradeWindow::showAll(SDL_Surface * to)
  2003. {
  2004. CWindowObject::showAll(to);
  2005. if(hRight)
  2006. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2007. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  2008. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2009. if(readyToTrade)
  2010. {
  2011. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2012. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2013. }
  2014. }
  2015. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2016. {
  2017. BOOST_FOREACH(CTradeableItem *t, toRemove)
  2018. removeItem(t);
  2019. }
  2020. void CTradeWindow::removeItem(CTradeableItem * t)
  2021. {
  2022. items[t->left] -= t;
  2023. delete t;
  2024. if(hRight == t)
  2025. {
  2026. hRight = NULL;
  2027. selectionChanged(false);
  2028. }
  2029. }
  2030. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2031. {
  2032. BOOST_FOREACH(CTradeableItem *t, items[1])
  2033. if(!hero->getStackCount(t->serial))
  2034. toRemove.insert(t);
  2035. }
  2036. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  2037. {
  2038. const IMarket *m = market;
  2039. const CGHeroInstance *h = hero;
  2040. CTradeWindow *nwindow = NULL;
  2041. GH.popIntTotally(this);
  2042. switch(Mode)
  2043. {
  2044. case EMarketMode::CREATURE_EXP:
  2045. case EMarketMode::ARTIFACT_EXP:
  2046. nwindow = new CAltarWindow(m, h, Mode);
  2047. break;
  2048. default:
  2049. nwindow = new CMarketplaceWindow(m, h, Mode);
  2050. break;
  2051. }
  2052. GH.pushInt(nwindow);
  2053. }
  2054. void CTradeWindow::artifactSelected(CArtPlace *slot)
  2055. {
  2056. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  2057. items[1][0]->setArtInstance(slot->ourArt);
  2058. if(slot->ourArt)
  2059. hLeft = items[1][0];
  2060. else
  2061. hLeft = NULL;
  2062. selectionChanged(true);
  2063. }
  2064. std::string CMarketplaceWindow::getBackgroundForMode(EMarketMode::EMarketMode mode)
  2065. {
  2066. switch(mode)
  2067. {
  2068. case EMarketMode::RESOURCE_RESOURCE:
  2069. return "TPMRKRES.bmp";
  2070. case EMarketMode::RESOURCE_PLAYER:
  2071. return "TPMRKPTS.bmp";
  2072. case EMarketMode::CREATURE_RESOURCE:
  2073. return "TPMRKCRS.bmp";
  2074. case EMarketMode::RESOURCE_ARTIFACT:
  2075. return "TPMRKABS.bmp";
  2076. case EMarketMode::ARTIFACT_RESOURCE:
  2077. return "TPMRKASS.bmp";
  2078. }
  2079. assert(0);
  2080. return "";
  2081. }
  2082. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2083. : CTradeWindow(getBackgroundForMode(Mode), Market, Hero, Mode)
  2084. {
  2085. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2086. madeTransaction = false;
  2087. bool sliderNeeded = true;
  2088. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2089. std::string title;
  2090. if (market->o->ID == GameConstants::TOWNI_TYPE)
  2091. {
  2092. switch (mode)
  2093. {
  2094. break; case EMarketMode::CREATURE_RESOURCE:
  2095. title = CGI->townh->towns[ETownType::STRONGHOLD].buildings[EBuilding::FREELANCERS_GUILD]->Name();
  2096. break; case EMarketMode::RESOURCE_ARTIFACT:
  2097. title = CGI->townh->towns[market->o->subID].buildings[EBuilding::ARTIFACT_MERCHANT]->Name();
  2098. break; case EMarketMode::ARTIFACT_RESOURCE:
  2099. title = CGI->townh->towns[market->o->subID].buildings[EBuilding::ARTIFACT_MERCHANT]->Name();
  2100. break; default:
  2101. title = CGI->generaltexth->allTexts[158];
  2102. }
  2103. }
  2104. else
  2105. {
  2106. switch (market->o->ID)
  2107. {
  2108. break; case 7: title = CGI->generaltexth->allTexts[349];
  2109. break; case 99: title = CGI->generaltexth->allTexts[159];
  2110. break; case 221: title = CGI->generaltexth->allTexts[159];
  2111. break; default: title = market->o->getHoverText();
  2112. }
  2113. }
  2114. new CLabel(300, 27, FONT_BIG, CENTER, Colors::Jasmine, title);
  2115. initItems(false);
  2116. initItems(true);
  2117. ok = new CAdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2118. ok->assignedKeys.insert(SDLK_ESCAPE);
  2119. deal = new CAdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2120. deal->block(true);
  2121. if(sliderNeeded)
  2122. {
  2123. slider = new CSlider(231,490,137,0,0,0);
  2124. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2125. max = new CAdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2126. max->block(true);
  2127. }
  2128. else
  2129. {
  2130. slider = NULL;
  2131. max = NULL;
  2132. deal->moveBy(Point(-30, 0));
  2133. }
  2134. Rect traderTextRect;
  2135. //left side
  2136. switch(Mode)
  2137. {
  2138. case EMarketMode::RESOURCE_RESOURCE:
  2139. case EMarketMode::RESOURCE_PLAYER:
  2140. case EMarketMode::RESOURCE_ARTIFACT:
  2141. new CLabel(154, 148, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth->allTexts[270]);
  2142. break;
  2143. case EMarketMode::CREATURE_RESOURCE:
  2144. //%s's Creatures
  2145. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::Cornsilk,
  2146. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2147. break;
  2148. case EMarketMode::ARTIFACT_RESOURCE:
  2149. //%s's Artifacts
  2150. new CLabel(152, 102, FONT_SMALL, CENTER, Colors::Cornsilk,
  2151. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2152. break;
  2153. }
  2154. //right side
  2155. switch(Mode)
  2156. {
  2157. case EMarketMode::RESOURCE_RESOURCE:
  2158. case EMarketMode::CREATURE_RESOURCE:
  2159. case EMarketMode::RESOURCE_ARTIFACT:
  2160. case EMarketMode::ARTIFACT_RESOURCE:
  2161. new CLabel(445, 148, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth->allTexts[168]);
  2162. traderTextRect = Rect(316, 48, 260, 75);
  2163. break;
  2164. case EMarketMode::RESOURCE_PLAYER:
  2165. new CLabel(445, 55, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth->allTexts[169]);
  2166. traderTextRect = Rect(28, 48, 260, 75);
  2167. break;
  2168. }
  2169. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2170. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  2171. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  2172. new CAdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2173. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  2174. new CAdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  2175. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  2176. new CAdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  2177. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  2178. new CAdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  2179. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  2180. new CAdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"); //was y=450, changed to not overlap res-art in some conditions
  2181. updateTraderText();
  2182. }
  2183. CMarketplaceWindow::~CMarketplaceWindow()
  2184. {
  2185. hLeft = hRight = NULL;
  2186. for(int i=0;i<items[1].size();i++)
  2187. delete items[1][i];
  2188. for(int i=0;i<items[0].size();i++)
  2189. delete items[0][i];
  2190. items[1].clear();
  2191. items[0].clear();
  2192. }
  2193. void CMarketplaceWindow::setMax()
  2194. {
  2195. slider->moveToMax();
  2196. }
  2197. void CMarketplaceWindow::makeDeal()
  2198. {
  2199. int sliderValue = 0;
  2200. if(slider)
  2201. sliderValue = slider->value;
  2202. else
  2203. sliderValue = !deal->isBlocked(); //should always be 1
  2204. if(!sliderValue)
  2205. return;
  2206. int leftIdToSend = -1;
  2207. switch (mode)
  2208. {
  2209. case EMarketMode::CREATURE_RESOURCE:
  2210. leftIdToSend = hLeft->serial;
  2211. break;
  2212. case EMarketMode::ARTIFACT_RESOURCE:
  2213. leftIdToSend = hLeft->getArtInstance()->id;
  2214. break;
  2215. default:
  2216. leftIdToSend = hLeft->id;
  2217. break;
  2218. }
  2219. if(slider)
  2220. {
  2221. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2222. slider->moveTo(0);
  2223. }
  2224. else
  2225. {
  2226. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2227. }
  2228. madeTransaction = true;
  2229. hLeft = NULL;
  2230. hRight = NULL;
  2231. selectionChanged(true);
  2232. }
  2233. void CMarketplaceWindow::sliderMoved( int to )
  2234. {
  2235. redraw();
  2236. }
  2237. void CMarketplaceWindow::selectionChanged(bool side)
  2238. {
  2239. readyToTrade = hLeft && hRight;
  2240. if(mode == EMarketMode::RESOURCE_RESOURCE)
  2241. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  2242. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  2243. arts->unmarkSlots(false);
  2244. if(readyToTrade)
  2245. {
  2246. int soldItemId = hLeft->id;
  2247. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  2248. if(slider)
  2249. {
  2250. int newAmount = -1;
  2251. if(itemsType[1] == RESOURCE)
  2252. newAmount = LOCPLINT->cb->getResourceAmount(soldItemId);
  2253. else if(itemsType[1] == CREATURE)
  2254. newAmount = hero->getStackCount(hLeft->serial) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2255. else
  2256. assert(0);
  2257. slider->setAmount(newAmount / r1);
  2258. slider->moveTo(0);
  2259. max->block(false);
  2260. deal->block(false);
  2261. }
  2262. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  2263. {
  2264. deal->block(LOCPLINT->cb->getResourceAmount(soldItemId) < r1);
  2265. }
  2266. else
  2267. deal->block(false);
  2268. }
  2269. else
  2270. {
  2271. if(slider)
  2272. {
  2273. max->block(true);
  2274. slider->setAmount(0);
  2275. slider->moveTo(0);
  2276. }
  2277. deal->block(true);
  2278. }
  2279. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2280. initSubs(false);
  2281. updateTraderText();
  2282. redraw();
  2283. }
  2284. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  2285. {
  2286. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  2287. }
  2288. void CMarketplaceWindow::garrisonChanged()
  2289. {
  2290. if(mode != EMarketMode::CREATURE_RESOURCE)
  2291. return;
  2292. std::set<CTradeableItem *> toRemove;
  2293. getEmptySlots(toRemove);
  2294. removeItems(toRemove);
  2295. initSubs(true);
  2296. }
  2297. void CMarketplaceWindow::artifactsChanged(bool Left)
  2298. {
  2299. assert(!Left);
  2300. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  2301. return;
  2302. std::vector<int> available = market->availableItemsIds(mode);
  2303. std::set<CTradeableItem *> toRemove;
  2304. BOOST_FOREACH(CTradeableItem *t, items[0])
  2305. if(!vstd::contains(available, t->id))
  2306. toRemove.insert(t);
  2307. removeItems(toRemove);
  2308. redraw();
  2309. }
  2310. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  2311. {
  2312. if(Left)
  2313. {
  2314. switch(itemsType[1])
  2315. {
  2316. case RESOURCE:
  2317. case CREATURE:
  2318. {
  2319. int val = slider
  2320. ? slider->value * r1
  2321. : (((deal->isBlocked())) ? 0 : r1);
  2322. return boost::lexical_cast<std::string>(val);
  2323. }
  2324. case ARTIFACT_INSTANCE:
  2325. return ((deal->isBlocked()) ? "0" : "1");
  2326. }
  2327. }
  2328. else
  2329. {
  2330. switch(itemsType[0])
  2331. {
  2332. case RESOURCE:
  2333. if(slider)
  2334. return boost::lexical_cast<std::string>( slider->value * r2 );
  2335. else
  2336. return boost::lexical_cast<std::string>(r2);
  2337. case ARTIFACT_TYPE:
  2338. return ((deal->isBlocked()) ? "0" : "1");
  2339. case PLAYER:
  2340. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  2341. }
  2342. }
  2343. return "???";
  2344. }
  2345. Point CMarketplaceWindow::selectionOffset(bool Left) const
  2346. {
  2347. if(Left)
  2348. {
  2349. switch(itemsType[1])
  2350. {
  2351. case RESOURCE:
  2352. return Point(122, 446);
  2353. case CREATURE:
  2354. return Point(128, 450);
  2355. case ARTIFACT_INSTANCE:
  2356. return Point(134, 466);
  2357. }
  2358. }
  2359. else
  2360. {
  2361. switch(itemsType[0])
  2362. {
  2363. case RESOURCE:
  2364. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2365. return Point(410, 469);
  2366. else
  2367. return Point(410, 446);
  2368. case ARTIFACT_TYPE:
  2369. return Point(425, 447);
  2370. case PLAYER:
  2371. return Point(417, 451);
  2372. }
  2373. }
  2374. assert(0);
  2375. return Point(0,0);
  2376. }
  2377. void CMarketplaceWindow::resourceChanged(int type, int val)
  2378. {
  2379. initSubs(true);
  2380. }
  2381. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2382. {
  2383. switch(type)
  2384. {
  2385. case RESOURCE:
  2386. dx = 82;
  2387. dy = 79;
  2388. x = 39;
  2389. y = 180;
  2390. h = 66;
  2391. w = 74;
  2392. break;
  2393. case PLAYER:
  2394. dx = 83;
  2395. dy = 118;
  2396. h = 64;
  2397. w = 58;
  2398. x = 44;
  2399. y = 83;
  2400. assert(Right);
  2401. break;
  2402. case CREATURE://45,123
  2403. x = 45;
  2404. y = 123;
  2405. w = 58;
  2406. h = 64;
  2407. dx = 83;
  2408. dy = 98;
  2409. assert(!Right);
  2410. break;
  2411. case ARTIFACT_TYPE://45,123
  2412. x = 340-289;
  2413. y = 180;
  2414. w = 44;
  2415. h = 44;
  2416. dx = 83;
  2417. dy = 79;
  2418. break;
  2419. }
  2420. leftToRightOffset = 289;
  2421. }
  2422. void CMarketplaceWindow::updateTraderText()
  2423. {
  2424. if(readyToTrade)
  2425. {
  2426. if(mode == EMarketMode::RESOURCE_PLAYER)
  2427. {
  2428. //I can give %s to the %s player.
  2429. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  2430. }
  2431. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  2432. {
  2433. //I can offer you the %s for %d %s of %s.
  2434. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2435. }
  2436. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  2437. {
  2438. //I can offer you %d %s of %s for %d %s of %s.
  2439. 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()));
  2440. }
  2441. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2442. {
  2443. //I can offer you %d %s of %s for %d %s.
  2444. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  2445. }
  2446. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2447. {
  2448. //I can offer you %d %s of %s for your %s.
  2449. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  2450. }
  2451. return;
  2452. }
  2453. int gnrtxtnr = -1;
  2454. if(madeTransaction)
  2455. {
  2456. if(mode == EMarketMode::RESOURCE_PLAYER)
  2457. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  2458. else
  2459. 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?
  2460. }
  2461. else
  2462. {
  2463. if(mode == EMarketMode::RESOURCE_PLAYER)
  2464. 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.
  2465. else
  2466. 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.
  2467. }
  2468. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  2469. }
  2470. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= NULL*/, EMarketMode::EMarketMode Mode)
  2471. :CTradeWindow((Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp"), Market, Hero, Mode)
  2472. {
  2473. if(Mode == EMarketMode::CREATURE_EXP)
  2474. {
  2475. //%s's Creatures
  2476. new CLabel(155, 30, FONT_SMALL, CENTER, Colors::Jasmine,
  2477. boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name));
  2478. //Altar of Sacrifice
  2479. new CLabel(450, 30, FONT_SMALL, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[479]);
  2480. //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  2481. new CTextBox(CGI->generaltexth->allTexts[480], Rect(320, 56, 256, 40), 0, FONT_SMALL, CENTER, Colors::Jasmine);
  2482. slider = new CSlider(231,481,137,0,0,0);
  2483. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  2484. max = new CAdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  2485. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  2486. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  2487. sacrificeBackpack = NULL;
  2488. initItems(true);
  2489. mimicCres();
  2490. }
  2491. else
  2492. {
  2493. //Sacrifice artifacts for experience
  2494. new CLabel(450, 34, FONT_SMALL, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[477]);
  2495. //%s's Creatures
  2496. new CLabel(302, 423, FONT_SMALL, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[478]);
  2497. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[571], boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  2498. sacrificeAll->block(hero->artifactsInBackpack.empty() && hero->artifactsWorn.empty());
  2499. sacrificeBackpack = new CAdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  2500. sacrificeBackpack->block(hero->artifactsInBackpack.empty());
  2501. slider = NULL;
  2502. max = NULL;
  2503. initItems(true);
  2504. initItems(false);
  2505. }
  2506. //Experience needed to reach next level
  2507. new CTextBox(CGI->generaltexth->allTexts[475], Rect(15, 415, 125, 50), 0, FONT_SMALL, CENTER, Colors::Jasmine);
  2508. //Total experience on the Altar
  2509. new CTextBox(CGI->generaltexth->allTexts[476], Rect(15, 495, 125, 40), 0, FONT_SMALL, CENTER, Colors::Jasmine);
  2510. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  2511. ok = new CAdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2512. ok->assignedKeys.insert(SDLK_ESCAPE);
  2513. deal = new CAdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  2514. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  2515. new CAdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  2516. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  2517. new CAdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  2518. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  2519. getExpValues();
  2520. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  2521. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  2522. setExpToLevel();
  2523. calcTotalExp();
  2524. blockTrade();
  2525. }
  2526. CAltarWindow::~CAltarWindow()
  2527. {
  2528. }
  2529. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2530. {
  2531. leftToRightOffset = 289;
  2532. x = 45;
  2533. y = 110;
  2534. w = 58;
  2535. h = 64;
  2536. dx = 83;
  2537. dy = 98;
  2538. }
  2539. void CAltarWindow::sliderMoved(int to)
  2540. {
  2541. sacrificedUnits[hLeft->serial] = to;
  2542. updateRight(hRight);
  2543. deal->block(!to);
  2544. calcTotalExp();
  2545. redraw();
  2546. }
  2547. void CAltarWindow::makeDeal()
  2548. {
  2549. if(mode == EMarketMode::CREATURE_EXP)
  2550. {
  2551. blockTrade();
  2552. slider->value = 0;
  2553. std::vector<int> toSacrifice = sacrificedUnits;
  2554. for (int i = 0; i < toSacrifice.size(); i++)
  2555. {
  2556. if(toSacrifice[i])
  2557. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  2558. }
  2559. BOOST_FOREACH(int& val, sacrificedUnits)
  2560. val = 0;
  2561. BOOST_FOREACH(CTradeableItem *t, items[0])
  2562. {
  2563. t->type = CREATURE_PLACEHOLDER;
  2564. t->subtitle = "";
  2565. }
  2566. }
  2567. else
  2568. {
  2569. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar) //sacrifice each artifact on the list
  2570. {
  2571. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  2572. }
  2573. arts->artifactsOnAltar.clear();
  2574. BOOST_FOREACH(CTradeableItem *t, items[0])
  2575. {
  2576. t->id = -1;
  2577. t->subtitle = "";
  2578. }
  2579. arts->commonInfo->reset();
  2580. //arts->scrollBackpack(0);
  2581. deal->block(true);
  2582. }
  2583. calcTotalExp();
  2584. }
  2585. void CAltarWindow::SacrificeAll()
  2586. {
  2587. if(mode == EMarketMode::CREATURE_EXP)
  2588. {
  2589. bool movedAnything = false;
  2590. BOOST_FOREACH(CTradeableItem *t, items[1])
  2591. sacrificedUnits[t->serial] = hero->getStackCount(t->serial);
  2592. sacrificedUnits[items[1].front()->serial]--;
  2593. BOOST_FOREACH(CTradeableItem *t, items[0])
  2594. {
  2595. updateRight(t);
  2596. if(t->type == CREATURE)
  2597. movedAnything = true;
  2598. }
  2599. deal->block(!movedAnything);
  2600. calcTotalExp();
  2601. }
  2602. else
  2603. {
  2604. for(std::map<ui16, ArtSlotInfo>::const_iterator i = hero->artifactsWorn.begin(); i != hero->artifactsWorn.end(); i++)
  2605. {
  2606. if(i->second.artifact->artType->id != 145) //ignore locks from assembled artifacts
  2607. moveFromSlotToAltar(i->first, NULL, i->second.artifact);
  2608. }
  2609. SacrificeBackpack();
  2610. }
  2611. redraw();
  2612. }
  2613. void CAltarWindow::selectionChanged(bool side)
  2614. {
  2615. if(mode != EMarketMode::CREATURE_EXP)
  2616. return;
  2617. CTradeableItem *&selected = side ? hLeft : hRight;
  2618. CTradeableItem *&theOther = side ? hRight : hLeft;
  2619. theOther = *std::find_if(items[!side].begin(), items[!side].end(), boost::bind(&CTradeableItem::serial, _1) == selected->serial);
  2620. int stackCount = 0;
  2621. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  2622. if(hero->getStackCount(i) > sacrificedUnits[i])
  2623. stackCount++;
  2624. slider->setAmount(hero->getStackCount(hLeft->serial) - (stackCount == 1));
  2625. slider->block(!slider->amount);
  2626. slider->value = sacrificedUnits[hLeft->serial];
  2627. max->block(!slider->amount);
  2628. readyToTrade = true;
  2629. redraw();
  2630. }
  2631. void CAltarWindow::mimicCres()
  2632. {
  2633. std::vector<Rect> positions;
  2634. getPositionsFor(positions, false, CREATURE);
  2635. BOOST_FOREACH(CTradeableItem *t, items[1])
  2636. {
  2637. CTradeableItem *hlp = new CTradeableItem(CREATURE_PLACEHOLDER, t->id, false, t->serial);
  2638. hlp->pos = positions[t->serial] + hlp->pos;
  2639. items[0].push_back(hlp);
  2640. }
  2641. }
  2642. Point CAltarWindow::selectionOffset(bool Left) const
  2643. {
  2644. if(Left)
  2645. return Point(150, 421);
  2646. else
  2647. return Point(396, 421);
  2648. }
  2649. std::string CAltarWindow::selectionSubtitle(bool Left) const
  2650. {
  2651. if(Left && slider && hLeft)
  2652. return boost::lexical_cast<std::string>(slider->value);
  2653. else if(!Left && hRight)
  2654. return hRight->subtitle;
  2655. else
  2656. return "";
  2657. }
  2658. void CAltarWindow::artifactsChanged(bool left)
  2659. {
  2660. }
  2661. void CAltarWindow::garrisonChanged()
  2662. {
  2663. if(mode != EMarketMode::CREATURE_EXP)
  2664. return;
  2665. std::set<CTradeableItem *> empty;
  2666. getEmptySlots(empty);
  2667. BOOST_FOREACH(CTradeableItem *t, empty)
  2668. {
  2669. removeItem(*std::find_if(items[0].begin(), items[0].end(), boost::bind(&CTradeableItem::serial, _1) == t->serial));
  2670. removeItem(t);
  2671. }
  2672. initSubs(true);
  2673. getExpValues();
  2674. }
  2675. void CAltarWindow::getExpValues()
  2676. {
  2677. int dump;
  2678. BOOST_FOREACH(CTradeableItem *t, items[1])
  2679. if(t->id >= 0)
  2680. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  2681. }
  2682. void CAltarWindow::calcTotalExp()
  2683. {
  2684. int val = 0;
  2685. if(mode == EMarketMode::CREATURE_EXP)
  2686. {
  2687. for (int i = 0; i < sacrificedUnits.size(); i++)
  2688. {
  2689. val += expPerUnit[i] * sacrificedUnits[i];
  2690. }
  2691. }
  2692. else
  2693. {
  2694. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar)
  2695. {
  2696. int dmp, valOfArt;
  2697. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  2698. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  2699. }
  2700. }
  2701. val *=(100+hero->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0;
  2702. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  2703. }
  2704. void CAltarWindow::setExpToLevel()
  2705. {
  2706. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  2707. }
  2708. void CAltarWindow::blockTrade()
  2709. {
  2710. hLeft = hRight = NULL;
  2711. readyToTrade = false;
  2712. if(slider)
  2713. {
  2714. slider->block(true);
  2715. max->block(true);
  2716. }
  2717. deal->block(true);
  2718. }
  2719. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  2720. {
  2721. int val = sacrificedUnits[toUpdate->serial];
  2722. toUpdate->type = val ? CREATURE : CREATURE_PLACEHOLDER;
  2723. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  2724. }
  2725. int CAltarWindow::firstFreeSlot()
  2726. {
  2727. int ret = -1;
  2728. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  2729. return ret < items[0].size() ? ret : -1;
  2730. }
  2731. void CAltarWindow::SacrificeBackpack()
  2732. {
  2733. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  2734. for (int i = 0; i < hero->artifactsInBackpack.size(); i++)
  2735. {
  2736. if(vstd::contains(toOmmit, hero->artifactsInBackpack[i].artifact))
  2737. {
  2738. toOmmit -= hero->artifactsInBackpack[i].artifact;
  2739. continue;
  2740. }
  2741. putOnAltar(NULL, hero->artifactsInBackpack[i].artifact);
  2742. }
  2743. arts->scrollBackpack(0);
  2744. calcTotalExp();
  2745. }
  2746. void CAltarWindow::artifactPicked()
  2747. {
  2748. redraw();
  2749. }
  2750. void CAltarWindow::showAll(SDL_Surface * to)
  2751. {
  2752. CTradeWindow::showAll(to);
  2753. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  2754. {
  2755. blitAtLoc(graphics->artDefs->ourImages[arts->commonInfo->src.art->artType->id].bitmap, 281, 442, to);
  2756. int dmp, val;
  2757. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2758. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::Cornsilk, to);
  2759. }
  2760. }
  2761. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  2762. {
  2763. int artID = art->artType->id;
  2764. if(artID != 1 && artID < 7) //special art
  2765. {
  2766. tlog2 << "Cannot put special artifact on altar!\n";
  2767. return false;
  2768. }
  2769. if(!altarSlot)
  2770. {
  2771. int slotIndex = firstFreeSlot();
  2772. if(slotIndex < 0)
  2773. {
  2774. tlog2 << "No free slots on altar!\n";
  2775. return false;
  2776. }
  2777. altarSlot = items[0][slotIndex];
  2778. }
  2779. int dmp, val;
  2780. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  2781. arts->artifactsOnAltar.insert(art);
  2782. altarSlot->setArtInstance(art);
  2783. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  2784. deal->block(false);
  2785. return true;
  2786. }
  2787. void CAltarWindow::moveFromSlotToAltar(int slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  2788. {
  2789. int freeBackpackSlot = hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START;
  2790. if(arts->commonInfo->src.art)
  2791. {
  2792. arts->commonInfo->dst.slotID = freeBackpackSlot;
  2793. arts->commonInfo->dst.AOH = arts;
  2794. }
  2795. if(putOnAltar(altarSlot, art))
  2796. {
  2797. if(slotID < GameConstants::BACKPACK_START)
  2798. LOCPLINT->cb->swapArtifacts(ArtifactLocation(hero, slotID), ArtifactLocation(hero, freeBackpackSlot));
  2799. else
  2800. {
  2801. arts->commonInfo->src.clear();
  2802. arts->commonInfo->dst.clear();
  2803. CCS->curh->dragAndDropCursor(NULL);
  2804. arts->unmarkSlots(false);
  2805. }
  2806. }
  2807. }
  2808. void CSystemOptionsWindow::setMusicVolume( int newVolume )
  2809. {
  2810. Settings volume = settings.write["general"]["music"];
  2811. volume->Float() = newVolume;
  2812. }
  2813. void CSystemOptionsWindow::setSoundVolume( int newVolume )
  2814. {
  2815. Settings volume = settings.write["general"]["sound"];
  2816. volume->Float() = newVolume;
  2817. }
  2818. void CSystemOptionsWindow::setHeroMoveSpeed( int newSpeed )
  2819. {
  2820. Settings speed = settings.write["adventure"]["heroSpeed"];
  2821. speed->Float() = newSpeed;
  2822. }
  2823. void CSystemOptionsWindow::setMapScrollingSpeed( int newSpeed )
  2824. {
  2825. Settings speed = settings.write["adventure"]["scrollSpeed"];
  2826. speed->Float() = newSpeed;
  2827. }
  2828. CSystemOptionsWindow::CSystemOptionsWindow():
  2829. CWindowObject(PLAYER_COLORED, "SysOpBck")
  2830. {
  2831. //TODO: translation and\or config file
  2832. static const std::string fsLabel = "Fullscreen";
  2833. static const std::string fsHelp = "{Fullscreen}\n\n If selected, VCMI will run in fullscreen mode, othervice VCMI will run in window";
  2834. static const std::string cwLabel = "Classic creature window";
  2835. static const std::string cwHelp = "{Classic creature window}\n\n Enable original Heroes 3 creature window instead of new window from VCMI";
  2836. static const std::string rsLabel = "Select resolution";
  2837. static const std::string rsHelp = "{Select resolution}\n\n Change in-game screen resolution. Will only affect adventure map. Game restart required to apply new resolution.";
  2838. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2839. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[568]);
  2840. //left window section
  2841. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::Jasmine);
  2842. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  2843. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  2844. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  2845. leftGroup->add(122, 262, rsLabel); //CGI->generaltexth->allTexts[20]
  2846. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  2847. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  2848. //right section
  2849. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::Cornsilk);
  2850. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  2851. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  2852. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  2853. rightGroup->add(282, 153, CGI->generaltexth->allTexts[575]);
  2854. rightGroup->add(282, 185, cwLabel); //CGI->generaltexth->allTexts[576]
  2855. rightGroup->add(282, 217, fsLabel); //CGI->generaltexth->allTexts[577]
  2856. //setting up buttons
  2857. load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second,
  2858. boost::bind(&CSystemOptionsWindow::bloadf, this), 246, 298, "SOLOAD.DEF", SDLK_l);
  2859. load->swappedImages = true;
  2860. load->update();
  2861. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second,
  2862. boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  2863. save->swappedImages = true;
  2864. save->update();
  2865. restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second,
  2866. boost::bind(&CSystemOptionsWindow::brestartf, this), 246, 357, "SORSTRT", SDLK_r);
  2867. restart->swappedImages = true;
  2868. restart->update();
  2869. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second,
  2870. boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  2871. mainMenu->swappedImages = true;
  2872. mainMenu->update();
  2873. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second,
  2874. boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  2875. quitGame->swappedImages = true;
  2876. quitGame->update();
  2877. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second,
  2878. boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  2879. backToMap->swappedImages = true;
  2880. backToMap->update();
  2881. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  2882. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  2883. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  2884. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  2885. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  2886. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  2887. heroMoveSpeed->select(settings["adventure"]["heroSpeed"].Float(), 1);
  2888. heroMoveSpeed->onChange = boost::bind(&CSystemOptionsWindow::setHeroMoveSpeed, this, _1);
  2889. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  2890. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  2891. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  2892. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  2893. mapScrollSpeed->select(settings["adventure"]["scrollSpeed"].Float(), 1);
  2894. mapScrollSpeed->onChange = boost::bind(&CSystemOptionsWindow::setMapScrollingSpeed, this, _1);
  2895. musicVolume = new CHighlightableButtonsGroup(0, true);
  2896. for(int i=0; i<10; ++i)
  2897. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  2898. musicVolume->select(CCS->musich->getVolume(), 1);
  2899. musicVolume->onChange = boost::bind(&CSystemOptionsWindow::setMusicVolume, this, _1);
  2900. effectsVolume = new CHighlightableButtonsGroup(0, true);
  2901. for(int i=0; i<10; ++i)
  2902. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  2903. effectsVolume->select(CCS->soundh->getVolume(), 1);
  2904. effectsVolume->onChange = boost::bind(&CSystemOptionsWindow::setSoundVolume, this, _1);
  2905. showReminder = new CHighlightableButton(
  2906. boost::bind(&CSystemOptionsWindow::toggleReminder, this, true), boost::bind(&CSystemOptionsWindow::toggleReminder, this, false),
  2907. std::map<int,std::string>(), CGI->generaltexth->zelp[361].second, false, "sysopchk.def", NULL, 246, 87, false);
  2908. newCreatureWin = new CHighlightableButton(
  2909. boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, true), boost::bind(&CSystemOptionsWindow::toggleCreatureWin, this, false),
  2910. std::map<int,std::string>(), cwHelp, false, "sysopchk.def", NULL, 246, 183, false);
  2911. fullscreen = new CHighlightableButton(
  2912. boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, true), boost::bind(&CSystemOptionsWindow::toggleFullscreen, this, false),
  2913. std::map<int,std::string>(), fsHelp, false, "sysopchk.def", NULL, 246, 215, false);
  2914. showReminder->select(settings["adventure"]["heroReminder"].Bool());
  2915. newCreatureWin->select(settings["general"]["classicCreatureWindow"].Bool());
  2916. fullscreen->select(settings["video"]["fullscreen"].Bool());
  2917. gameResButton = new CAdventureMapButton("", rsHelp, boost::bind(&CSystemOptionsWindow::selectGameRes, this), 28, 275,"SYSOB12", SDLK_g);
  2918. }
  2919. void CSystemOptionsWindow::selectGameRes()
  2920. {
  2921. //TODO: translation and\or config file
  2922. static const std::string rsLabel = "Select resolution";
  2923. static const std::string rsHelp = "Change in-game screen resolution.";
  2924. std::vector<std::string> items;
  2925. BOOST_FOREACH( config::CConfigHandler::GuiOptionsMap::value_type& value, conf.guiOptions)
  2926. {
  2927. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  2928. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  2929. items.push_back(resX + 'x' + resY);
  2930. }
  2931. GH.pushInt(new CObjectListWindow(items, NULL, rsLabel, rsHelp,
  2932. boost::bind(&CSystemOptionsWindow::setGameRes, this, _1)));
  2933. }
  2934. void CSystemOptionsWindow::setGameRes(int index)
  2935. {
  2936. config::CConfigHandler::GuiOptionsMap::const_iterator iter = conf.guiOptions.begin();
  2937. while (index--)
  2938. iter++;
  2939. //do not set resolution to illegal one (0x0)
  2940. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  2941. Settings gameRes = settings.write["video"]["screenRes"];
  2942. gameRes["width"].Float() = iter->first.first;
  2943. gameRes["height"].Float() = iter->first.second;
  2944. }
  2945. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  2946. {
  2947. GH.popIntTotally(this);
  2948. SDL_Event event;
  2949. event.type = type;
  2950. event.user.code = usercode; // not necessarily used
  2951. SDL_PushEvent(&event);
  2952. }
  2953. void CSystemOptionsWindow::toggleReminder(bool on)
  2954. {
  2955. Settings heroReminder = settings.write["adventure"]["heroReminder"];
  2956. heroReminder->Bool() = on;
  2957. }
  2958. void CSystemOptionsWindow::toggleCreatureWin(bool on)
  2959. {
  2960. Settings classicCreatureWindow = settings.write["general"]["classicCreatureWindow"];
  2961. classicCreatureWindow->Bool() = on;
  2962. }
  2963. void CSystemOptionsWindow::toggleFullscreen(bool on)
  2964. {
  2965. Settings fullscreen = settings.write["video"]["fullscreen"];
  2966. fullscreen->Bool() = on;
  2967. }
  2968. void CSystemOptionsWindow::bquitf()
  2969. {
  2970. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  2971. }
  2972. void CSystemOptionsWindow::breturnf()
  2973. {
  2974. GH.popIntTotally(this);
  2975. }
  2976. void CSystemOptionsWindow::bmainmenuf()
  2977. {
  2978. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, RETURN_TO_MAIN_MENU), 0, false);
  2979. }
  2980. void CSystemOptionsWindow::bloadf()
  2981. {
  2982. GH.popIntTotally(this);
  2983. LOCPLINT->proposeLoadingGame();
  2984. }
  2985. void CSystemOptionsWindow::bsavef()
  2986. {
  2987. GH.popIntTotally(this);
  2988. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  2989. }
  2990. void CSystemOptionsWindow::brestartf()
  2991. {
  2992. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, RESTART_GAME), 0, false);
  2993. }
  2994. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj):
  2995. CWindowObject(PLAYER_COLORED, "TPTAVERN"),
  2996. tavernObj(TavernObj)
  2997. {
  2998. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2999. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3000. assert(h.size() == 2);
  3001. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3002. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3003. if(h[0])
  3004. selected = 0;
  3005. else
  3006. selected = -1;
  3007. oldSelected = -1;
  3008. new CLabel(200, 35, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->jktexts[37]);
  3009. new CLabel(320, 328, FONT_SMALL, CENTER, Colors::Cornsilk, "2500");
  3010. new CTextBox(LOCPLINT->cb->getTavernGossip(tavernObj), Rect(32, 190, 330, 68), 0, FONT_SMALL, CENTER, Colors::Cornsilk);
  3011. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  3012. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3013. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3014. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3015. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  3016. {
  3017. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3018. recruit->block(2);
  3019. }
  3020. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3021. {
  3022. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3023. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3024. recruit->block(2);
  3025. }
  3026. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3027. {
  3028. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3029. recruit->block(2);
  3030. }
  3031. else
  3032. {
  3033. if(!h[0])
  3034. recruit->block(1);
  3035. }
  3036. CCS->videoh->open("TAVERN.BIK");
  3037. }
  3038. void CTavernWindow::recruitb()
  3039. {
  3040. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3041. const CGObjectInstance *obj = tavernObj;
  3042. close();
  3043. LOCPLINT->cb->recruitHero(obj, toBuy);
  3044. }
  3045. void CTavernWindow::thievesguildb()
  3046. {
  3047. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3048. }
  3049. CTavernWindow::~CTavernWindow()
  3050. {
  3051. CCS->videoh->close();
  3052. }
  3053. void CTavernWindow::show(SDL_Surface * to)
  3054. {
  3055. CWindowObject::show(to);
  3056. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3057. if(selected >= 0)
  3058. {
  3059. HeroPortrait *sel = selected ? h2 : h1;
  3060. if (selected != oldSelected && !recruit->isBlocked())
  3061. {
  3062. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3063. oldSelected = selected;
  3064. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3065. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3066. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3067. }
  3068. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,Colors::Cornsilk,to);
  3069. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3070. }
  3071. }
  3072. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3073. {
  3074. if(previousState && !down && h)
  3075. *_sel = _id;
  3076. }
  3077. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3078. {
  3079. if(down && h)
  3080. {
  3081. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3082. }
  3083. }
  3084. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3085. : h(H), _sel(&sel), _id(id)
  3086. {
  3087. addUsedEvents(LCLICK | RCLICK | HOVER);
  3088. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3089. h = H;
  3090. pos.x += x;
  3091. pos.y += y;
  3092. pos.w = 58;
  3093. pos.h = 64;
  3094. if(H)
  3095. {
  3096. hoverName = CGI->generaltexth->tavernInfo[4];
  3097. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3098. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3099. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3100. if(vstd::contains(h->artifactsWorn,i))
  3101. artifs--;
  3102. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3103. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3104. descr[sizeof(descr)-1] = '\0';
  3105. new CAnimImage("portraitsLarge", h->subID);
  3106. }
  3107. }
  3108. void CTavernWindow::HeroPortrait::hover( bool on )
  3109. {
  3110. //Hoverable::hover(on);
  3111. if(on)
  3112. GH.statusbar->print(hoverName);
  3113. else
  3114. GH.statusbar->clear();
  3115. }
  3116. void CInGameConsole::show(SDL_Surface * to)
  3117. {
  3118. int number = 0;
  3119. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3120. boost::unique_lock<boost::mutex> lock(texts_mx);
  3121. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3122. {
  3123. SDL_Color green = {0,0xff,0,0};
  3124. Point leftBottomCorner(0, screen->h);
  3125. if(LOCPLINT->battleInt)
  3126. {
  3127. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3128. }
  3129. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3130. if(SDL_GetTicks() - it->second > defaultTimeout)
  3131. {
  3132. toDel.push_back(it);
  3133. }
  3134. }
  3135. for(int it=0; it<toDel.size(); ++it)
  3136. {
  3137. texts.erase(toDel[it]);
  3138. }
  3139. }
  3140. void CInGameConsole::print(const std::string &txt)
  3141. {
  3142. boost::unique_lock<boost::mutex> lock(texts_mx);
  3143. int lineLen = conf.go()->ac.outputLineLength;
  3144. if(txt.size() < lineLen)
  3145. {
  3146. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3147. if(texts.size() > maxDisplayedTexts)
  3148. {
  3149. texts.pop_front();
  3150. }
  3151. }
  3152. else
  3153. {
  3154. assert(lineLen);
  3155. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3156. {
  3157. std::string part = txt.substr(g * lineLen, lineLen);
  3158. if(part.size() == 0)
  3159. break;
  3160. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3161. if(texts.size() > maxDisplayedTexts)
  3162. {
  3163. texts.pop_front();
  3164. }
  3165. }
  3166. }
  3167. }
  3168. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3169. {
  3170. if(key.type != SDL_KEYDOWN) return;
  3171. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3172. switch(key.keysym.sym)
  3173. {
  3174. case SDLK_TAB:
  3175. case SDLK_ESCAPE:
  3176. {
  3177. if(captureAllKeys)
  3178. {
  3179. captureAllKeys = false;
  3180. endEnteringText(false);
  3181. }
  3182. else if(SDLK_TAB)
  3183. {
  3184. captureAllKeys = true;
  3185. startEnteringText();
  3186. }
  3187. break;
  3188. }
  3189. case SDLK_RETURN: //enter key
  3190. {
  3191. if(enteredText.size() > 0 && captureAllKeys)
  3192. {
  3193. captureAllKeys = false;
  3194. endEnteringText(true);
  3195. }
  3196. break;
  3197. }
  3198. case SDLK_BACKSPACE:
  3199. {
  3200. if(enteredText.size() > 1)
  3201. {
  3202. enteredText.resize(enteredText.size()-1);
  3203. enteredText[enteredText.size()-1] = '_';
  3204. refreshEnteredText();
  3205. }
  3206. break;
  3207. }
  3208. case SDLK_UP: //up arrow
  3209. {
  3210. if(previouslyEntered.size() == 0)
  3211. break;
  3212. if(prevEntDisp == -1)
  3213. {
  3214. prevEntDisp = previouslyEntered.size() - 1;
  3215. enteredText = previouslyEntered[prevEntDisp] + "_";
  3216. refreshEnteredText();
  3217. }
  3218. else if( prevEntDisp > 0)
  3219. {
  3220. --prevEntDisp;
  3221. enteredText = previouslyEntered[prevEntDisp] + "_";
  3222. refreshEnteredText();
  3223. }
  3224. break;
  3225. }
  3226. case SDLK_DOWN: //down arrow
  3227. {
  3228. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3229. {
  3230. ++prevEntDisp;
  3231. enteredText = previouslyEntered[prevEntDisp] + "_";
  3232. refreshEnteredText();
  3233. }
  3234. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3235. {
  3236. prevEntDisp = -1;
  3237. enteredText = "_";
  3238. refreshEnteredText();
  3239. }
  3240. break;
  3241. }
  3242. default:
  3243. {
  3244. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3245. {
  3246. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3247. {
  3248. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3249. enteredText += "_";
  3250. refreshEnteredText();
  3251. }
  3252. }
  3253. break;
  3254. }
  3255. }
  3256. }
  3257. void CInGameConsole::startEnteringText()
  3258. {
  3259. enteredText = "_";
  3260. if(GH.topInt() == adventureInt)
  3261. {
  3262. GH.statusbar->print(enteredText);
  3263. }
  3264. else if(LOCPLINT->battleInt)
  3265. {
  3266. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3267. }
  3268. }
  3269. void CInGameConsole::endEnteringText(bool printEnteredText)
  3270. {
  3271. prevEntDisp = -1;
  3272. if(printEnteredText)
  3273. {
  3274. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3275. LOCPLINT->cb->sendMessage(txt);
  3276. previouslyEntered.push_back(txt);
  3277. //print(txt);
  3278. }
  3279. enteredText = "";
  3280. if(GH.topInt() == adventureInt)
  3281. {
  3282. GH.statusbar->clear();
  3283. }
  3284. else if(LOCPLINT->battleInt)
  3285. {
  3286. LOCPLINT->battleInt->console->ingcAlter = "";
  3287. }
  3288. }
  3289. void CInGameConsole::refreshEnteredText()
  3290. {
  3291. if(GH.topInt() == adventureInt)
  3292. {
  3293. GH.statusbar->print(enteredText);
  3294. }
  3295. else if(LOCPLINT->battleInt)
  3296. {
  3297. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3298. }
  3299. }
  3300. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3301. {
  3302. addUsedEvents(KEYBOARD);
  3303. }
  3304. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits ):
  3305. CWindowObject(PLAYER_COLORED, "GARRISON")
  3306. {
  3307. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3308. garr = new CGarrisonInt(92, 127, 4, Point(0,96), background->bg, Point(93,127), up, down, removableUnits);
  3309. {
  3310. CAdventureMapButton *split = new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3311. removeChild(split);
  3312. garr->addSplitBtn(split);
  3313. }
  3314. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3315. std::string titleText;
  3316. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  3317. titleText = CGI->generaltexth->allTexts[709];
  3318. else
  3319. {
  3320. titleText = CGI->generaltexth->allTexts[35];
  3321. boost::algorithm::replace_first(titleText, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  3322. }
  3323. new CLabel(275, 30, FONT_BIG, CENTER, Colors::Jasmine, titleText);
  3324. new CAnimImage("CREST58", garr->armedObjs[0]->getOwner(), 0, 28, 124);
  3325. new CAnimImage("PortraitsLarge", dynamic_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait, 0, 29, 222);
  3326. }
  3327. IShowActivatable::IShowActivatable()
  3328. {
  3329. type = 0;
  3330. }
  3331. CGarrisonHolder::CGarrisonHolder()
  3332. {
  3333. }
  3334. void CWindowWithGarrison::updateGarrisons()
  3335. {
  3336. garr->recreateSlots();
  3337. }
  3338. CArtPlace::CArtPlace(const CArtifactInstance* Art)
  3339. :picked(false), marked(false), locked(false), ourArt(Art)
  3340. {
  3341. }
  3342. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  3343. picked(false), marked(false), locked(false), ourArt(Art)
  3344. {
  3345. pos += position;
  3346. pos.w = pos.h = 44;
  3347. }
  3348. void CArtPlace::clickLeft(tribool down, bool previousState)
  3349. {
  3350. //LRClickableAreaWTextComp::clickLeft(down);
  3351. bool inBackpack = slotID >= GameConstants::BACKPACK_START,
  3352. srcInBackpack = ourOwner->commonInfo->src.slotID >= GameConstants::BACKPACK_START,
  3353. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  3354. if(ourOwner->highlightModeCallback && ourArt)
  3355. {
  3356. if(down)
  3357. {
  3358. if(ourArt->artType->id < 7) //War Machine or Spellbook
  3359. {
  3360. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  3361. }
  3362. else
  3363. {
  3364. ourOwner->unmarkSlots(false);
  3365. marked = true;
  3366. ourOwner->highlightModeCallback(this);
  3367. }
  3368. }
  3369. return;
  3370. }
  3371. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3372. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  3373. {
  3374. if(ourArt->artType->id == 0)
  3375. {
  3376. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  3377. GH.pushInt(spellWindow);
  3378. }
  3379. }
  3380. if (!down && previousState)
  3381. {
  3382. if(ourArt && ourArt->artType->id == 0) //spellbook
  3383. return; //this is handled separately
  3384. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  3385. {
  3386. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3387. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  3388. {
  3389. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  3390. {
  3391. std::vector<CComponent *> catapult(1, new CComponent(CComponent::artifact, 3, 0));
  3392. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3393. return;
  3394. }
  3395. select();
  3396. }
  3397. }
  3398. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  3399. {
  3400. deselect();
  3401. }
  3402. else //perform artifact transition
  3403. {
  3404. if(inBackpack) // Backpack destination.
  3405. {
  3406. 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
  3407. {
  3408. deselect();
  3409. }
  3410. else
  3411. {
  3412. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  3413. switch(cur->id)
  3414. {
  3415. case 3:
  3416. //should not happen, catapult cannot be selected
  3417. assert(cur->id != 3);
  3418. break;
  3419. case 4: case 5: case 6: //war machines cannot go to backpack
  3420. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  3421. break;
  3422. default:
  3423. setMeAsDest();
  3424. vstd::amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  3425. if(srcInBackpack && srcInSameHero)
  3426. {
  3427. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with vstd::amin above it will guarantee that dest is at most the last artifact
  3428. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  3429. ourOwner->commonInfo->dst.slotID--;
  3430. }
  3431. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  3432. deselect();
  3433. else
  3434. ourOwner->realizeCurrentTransaction();
  3435. break;
  3436. }
  3437. }
  3438. }
  3439. //check if swap is possible
  3440. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  3441. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  3442. {
  3443. setMeAsDest();
  3444. //
  3445. // // Special case when the dest artifact can't be fit into the src slot.
  3446. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  3447. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  3448. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  3449. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  3450. // {
  3451. // // Put dest artifact into owner's backpack.
  3452. // ourOwner->commonInfo->src.AOH = ourOwner;
  3453. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  3454. // }
  3455. ourOwner->realizeCurrentTransaction();
  3456. }
  3457. }
  3458. }
  3459. }
  3460. void CArtPlace::clickRight(tribool down, bool previousState)
  3461. {
  3462. if(down && ourArt && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  3463. {
  3464. if (slotID < GameConstants::BACKPACK_START)
  3465. {
  3466. if(ourOwner->allowedAssembling)
  3467. {
  3468. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  3469. // If the artifact can be assembled, display dialog.
  3470. BOOST_FOREACH(const CArtifact *combination, assemblyPossibilities)
  3471. {
  3472. LOCPLINT->showArtifactAssemblyDialog(
  3473. ourArt->artType->id,
  3474. combination->id,
  3475. true,
  3476. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, true, combination->id),
  3477. 0);
  3478. if(assemblyPossibilities.size() > 2)
  3479. {
  3480. tlog3 << "More than one possibility of assembling... taking only first\n";
  3481. break;
  3482. }
  3483. return;
  3484. }
  3485. // Otherwise if the artifact can be diasassembled, display dialog.
  3486. if(ourArt->canBeDisassembled())
  3487. {
  3488. LOCPLINT->showArtifactAssemblyDialog(
  3489. ourArt->artType->id,
  3490. 0,
  3491. false,
  3492. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, false, 0),
  3493. 0);
  3494. return;
  3495. }
  3496. }
  3497. }
  3498. // Lastly just show the artifact description.
  3499. LRClickableAreaWTextComp::clickRight(down, previousState);
  3500. }
  3501. }
  3502. /**
  3503. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3504. */
  3505. void CArtPlace::select ()
  3506. {
  3507. if (locked)
  3508. return;
  3509. picked = true;
  3510. if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear
  3511. {
  3512. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3513. {
  3514. CArtPlace *ap = ourOwner->getArtPlace(i);
  3515. ap->picked = ourArt->isPart(ap->ourArt);
  3516. }
  3517. }
  3518. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  3519. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->artType->id].bitmap);
  3520. ourOwner->commonInfo->src.setTo(this, false);
  3521. ourOwner->markPossibleSlots(ourArt);
  3522. if(slotID >= GameConstants::BACKPACK_START)
  3523. ourOwner->scrollBackpack(0); //will update slots
  3524. ourOwner->updateParentWindow();
  3525. ourOwner->safeRedraw();
  3526. }
  3527. /**
  3528. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  3529. */
  3530. void CArtPlace::deselect ()
  3531. {
  3532. picked = false;
  3533. if(ourArt && ourArt->canBeDisassembled()) //combined art returned to its slot -> restore locks
  3534. {
  3535. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3536. ourOwner->getArtPlace(i)->picked = false;
  3537. }
  3538. CCS->curh->dragAndDropCursor(NULL);
  3539. ourOwner->unmarkSlots();
  3540. ourOwner->commonInfo->src.clear();
  3541. if(slotID >= GameConstants::BACKPACK_START)
  3542. ourOwner->scrollBackpack(0); //will update slots
  3543. ourOwner->updateParentWindow();
  3544. ourOwner->safeRedraw();
  3545. }
  3546. void CArtPlace::showAll(SDL_Surface * to)
  3547. {
  3548. 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
  3549. {
  3550. int graphic = locked ? 145 : ourArt->artType->id;
  3551. blitAt(graphics->artDefs->ourImages[graphic].bitmap, pos.x, pos.y, to);
  3552. }
  3553. if(marked && active)
  3554. {
  3555. // Draw vertical bars.
  3556. for (int i = 0; i < pos.h; ++i)
  3557. {
  3558. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3559. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3560. }
  3561. // Draw horizontal bars.
  3562. for (int i = 0; i < pos.w; ++i)
  3563. {
  3564. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3565. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3566. }
  3567. }
  3568. }
  3569. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  3570. {
  3571. // You can place 'no artifact' anywhere.
  3572. if(!art)
  3573. return true;
  3574. // Anything can but War Machines can be placed in backpack.
  3575. if (slotID >= GameConstants::BACKPACK_START)
  3576. return !CGI->arth->isBigArtifact(art->id);
  3577. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  3578. }
  3579. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  3580. {
  3581. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  3582. }
  3583. void CArtPlace::setArtifact(const CArtifactInstance *art)
  3584. {
  3585. baseType = -1; //by default we don't store any component
  3586. ourArt = art;
  3587. if(!art)
  3588. {
  3589. text = std::string();
  3590. hoverText = CGI->generaltexth->allTexts[507];
  3591. }
  3592. else
  3593. {
  3594. text = ourArt->artType->Description();
  3595. if(art->artType->id == 1) //spell scroll
  3596. {
  3597. // 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.
  3598. // so we want to replace text in [...] with a spell name
  3599. // however other language versions don't have name placeholder at all, so we have to be careful
  3600. int spellID = art->getGivenSpellID();
  3601. int nameStart = text.find_first_of('[');
  3602. int nameEnd = text.find_first_of(']', nameStart);
  3603. if(spellID >= 0)
  3604. {
  3605. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  3606. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  3607. //add spell component info (used to provide a pic in r-click popup)
  3608. baseType = CComponent::spell;
  3609. type = spellID;
  3610. bonusValue = 0;
  3611. }
  3612. }
  3613. if (locked) // Locks should appear as empty.
  3614. hoverText = CGI->generaltexth->allTexts[507];
  3615. else
  3616. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  3617. }
  3618. }
  3619. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3620. {
  3621. if((!down) && previousState)
  3622. {
  3623. std::vector<CComponent*> comp(1, createComponent());
  3624. LOCPLINT->showInfoDialog(text, comp);
  3625. }
  3626. }
  3627. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  3628. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  3629. {
  3630. }
  3631. CComponent * LRClickableAreaWTextComp::createComponent() const
  3632. {
  3633. if(baseType >= 0)
  3634. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  3635. else
  3636. return NULL;
  3637. }
  3638. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3639. {
  3640. if(down)
  3641. {
  3642. if(CComponent *comp = createComponent())
  3643. {
  3644. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  3645. return;
  3646. }
  3647. }
  3648. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  3649. }
  3650. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  3651. {
  3652. addUsedEvents(LCLICK | RCLICK | HOVER);
  3653. pos.x += x; pos.w = 58;
  3654. pos.y += y; pos.h = 64;
  3655. }
  3656. void CHeroArea::clickLeft(tribool down, bool previousState)
  3657. {
  3658. if((!down) && previousState && hero)
  3659. LOCPLINT->openHeroWindow(hero);
  3660. }
  3661. void CHeroArea::clickRight(tribool down, bool previousState)
  3662. {
  3663. if((!down) && previousState && hero)
  3664. LOCPLINT->openHeroWindow(hero);
  3665. }
  3666. void CHeroArea::hover(bool on)
  3667. {
  3668. if (on && hero)
  3669. GH.statusbar->print(hero->hoverName);
  3670. else
  3671. GH.statusbar->clear();
  3672. }
  3673. void CHeroArea::showAll(SDL_Surface * to)
  3674. {
  3675. if (hero)
  3676. blitAtLoc(graphics->portraitLarge[hero->portrait],0,0,to);
  3677. }
  3678. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  3679. {
  3680. if((!down) && previousState && town)
  3681. {
  3682. LOCPLINT->openTownWindow(town);
  3683. if ( type == 2 )
  3684. LOCPLINT->castleInt->builds->buildingClicked(10);
  3685. else if ( type == 3 && town->fortLevel() )
  3686. LOCPLINT->castleInt->builds->buildingClicked(7);
  3687. }
  3688. }
  3689. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  3690. {
  3691. if((!down) && previousState && town)
  3692. LOCPLINT->openTownWindow(town);//TODO: popup?
  3693. }
  3694. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  3695. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  3696. {
  3697. }
  3698. void CArtifactsOfHero::SCommonPart::reset()
  3699. {
  3700. src.clear();
  3701. dst.clear();
  3702. CCS->curh->dragAndDropCursor(NULL);
  3703. }
  3704. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  3705. {
  3706. // // An update is made, rather than initialization.
  3707. // if (curHero && curHero->id == hero->id)
  3708. // {
  3709. // if(curHero != hero)
  3710. // {
  3711. // //delete curHero;
  3712. // curHero = hero; //was: creating a copy
  3713. // }
  3714. //
  3715. // // Compensate backpack pos if an artifact was insertad before it.
  3716. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  3717. // && commonInfo->dst.slotID - 19 < backpackPos)
  3718. // {
  3719. // backpackPos++;
  3720. // }
  3721. //
  3722. // if (updateState && commonInfo->srcAOH == this)
  3723. // {
  3724. // // A swap was made, make the replaced artifact the current selected.
  3725. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  3726. // {
  3727. // // // Temporarily remove artifact from hero.
  3728. // // if (commonInfo->srcSlotID < 19)
  3729. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  3730. // // else
  3731. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  3732. //
  3733. // updateParentWindow(); //TODO: evil! but does the thing
  3734. //
  3735. // // Source <- Dest
  3736. // commonInfo->srcArtifact = commonInfo->destArtifact;
  3737. //
  3738. // // Reset destination parameters.
  3739. // commonInfo->dst.clear();
  3740. //
  3741. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  3742. // markPossibleSlots(commonInfo->srcArtifact);
  3743. // }
  3744. // else if (commonInfo->destAOH != NULL)
  3745. // {
  3746. // // Reset all parameters.
  3747. // commonInfo->reset();
  3748. // unmarkSlots();
  3749. // }
  3750. // }
  3751. // }
  3752. // else
  3753. // {
  3754. // commonInfo->reset();
  3755. // }
  3756. //
  3757. // if(hero != curHero)
  3758. // {
  3759. // // delete curHero;
  3760. // // curHero = new CGHeroInstance(*hero);
  3761. // curHero = hero; //was: creating a copy
  3762. // }
  3763. curHero = hero;
  3764. if (curHero->artifactsInBackpack.size() > 0)
  3765. backpackPos %= curHero->artifactsInBackpack.size();
  3766. else
  3767. backpackPos = 0;
  3768. // Fill the slots for worn artifacts and backpack.
  3769. for (int g = 0; g < artWorn.size() ; g++)
  3770. setSlotData(artWorn[g], g);
  3771. scrollBackpack(0);
  3772. }
  3773. void CArtifactsOfHero::dispose()
  3774. {
  3775. //vstd::clear_pointer(curHero);
  3776. //unmarkSlots(false);
  3777. CCS->curh->dragAndDropCursor(NULL);
  3778. }
  3779. void CArtifactsOfHero::scrollBackpack(int dir)
  3780. {
  3781. int artsInBackpack = curHero->artifactsInBackpack.size();
  3782. backpackPos += dir;
  3783. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  3784. backpackPos += artsInBackpack;
  3785. if(artsInBackpack)
  3786. backpackPos %= artsInBackpack;
  3787. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  3788. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  3789. toOmit.insert(commonInfo->src.art);
  3790. int omitedSoFar = 0;
  3791. //set new data
  3792. size_t s = 0;
  3793. for( ; s < artsInBackpack; ++s)
  3794. {
  3795. if (s < artsInBackpack)
  3796. {
  3797. int slotID = GameConstants::BACKPACK_START + (s + backpackPos)%artsInBackpack;
  3798. const CArtifactInstance *art = curHero->getArt(slotID);
  3799. assert(art);
  3800. if(!vstd::contains(toOmit, art))
  3801. {
  3802. if(s - omitedSoFar < backpack.size())
  3803. setSlotData(backpack[s-omitedSoFar], slotID);
  3804. }
  3805. else
  3806. {
  3807. toOmit -= art;
  3808. omitedSoFar++;
  3809. continue;
  3810. }
  3811. }
  3812. }
  3813. for( ; s - omitedSoFar < backpack.size(); s++)
  3814. eraseSlotData(backpack[s-omitedSoFar], GameConstants::BACKPACK_START + s);
  3815. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  3816. if(highlightModeCallback)
  3817. {
  3818. for(int i = 0; i < backpack.size(); i++)
  3819. {
  3820. if(backpack[i]->marked)
  3821. {
  3822. highlightModeCallback(backpack[i]);
  3823. break;
  3824. }
  3825. }
  3826. }
  3827. //blocking scrolling if there is not enough artifacts to scroll
  3828. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  3829. leftArtRoll->block(!scrollingPossible);
  3830. rightArtRoll->block(!scrollingPossible);
  3831. safeRedraw();
  3832. }
  3833. /**
  3834. * Marks possible slots where a given artifact can be placed, except backpack.
  3835. *
  3836. * @param art Artifact checked against.
  3837. */
  3838. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  3839. {
  3840. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  3841. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  3842. place->marked = art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true);
  3843. safeRedraw();
  3844. }
  3845. /**
  3846. * Unamarks all slots.
  3847. */
  3848. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  3849. {
  3850. if(commonInfo)
  3851. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  3852. aoh->unmarkLocalSlots(false);
  3853. else
  3854. unmarkLocalSlots(false);\
  3855. if(withRedraw)
  3856. safeRedraw();
  3857. }
  3858. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw /*= true*/)
  3859. {
  3860. BOOST_FOREACH(CArtPlace *place, artWorn)
  3861. place->marked = false;
  3862. BOOST_FOREACH(CArtPlace *place, backpack)
  3863. place->marked = false;
  3864. if(withRedraw)
  3865. safeRedraw();
  3866. }
  3867. /**
  3868. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  3869. */
  3870. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, int slotID)
  3871. {
  3872. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  3873. {
  3874. return;
  3875. }
  3876. artPlace->picked = false;
  3877. artPlace->slotID = slotID;
  3878. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  3879. {
  3880. artPlace->setArtifact(asi->artifact);
  3881. artPlace->locked = asi->locked;
  3882. }
  3883. else
  3884. artPlace->setArtifact(NULL);
  3885. }
  3886. /**
  3887. * Makes given artifact slot appear as empty with a certain slot ID.
  3888. */
  3889. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, int slotID)
  3890. {
  3891. artPlace->picked = false;
  3892. artPlace->slotID = slotID;
  3893. artPlace->setArtifact(NULL);
  3894. }
  3895. CArtifactsOfHero::CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  3896. CAdventureMapButton *leftScroll, CAdventureMapButton *rightScroll, bool createCommonPart):
  3897. curHero(NULL),
  3898. artWorn(ArtWorn), backpack(Backpack),
  3899. backpackPos(0), commonInfo(NULL), updateState(false),
  3900. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  3901. allowedAssembling(true), highlightModeCallback(0)
  3902. {
  3903. if(createCommonPart)
  3904. {
  3905. commonInfo = new CArtifactsOfHero::SCommonPart;
  3906. commonInfo->participants.insert(this);
  3907. }
  3908. // Init slots for worn artifacts.
  3909. for (size_t g = 0; g < artWorn.size() ; g++)
  3910. {
  3911. artWorn[g]->ourOwner = this;
  3912. eraseSlotData(artWorn[g], g);
  3913. }
  3914. // Init slots for the backpack.
  3915. for(size_t s=0; s<backpack.size(); ++s)
  3916. {
  3917. backpack[s]->ourOwner = this;
  3918. eraseSlotData(backpack[s], GameConstants::BACKPACK_START + s);
  3919. }
  3920. leftArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1);
  3921. rightArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1);
  3922. }
  3923. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  3924. : curHero(NULL), backpackPos(0), commonInfo(NULL), updateState(false), allowedAssembling(true), highlightModeCallback(0)
  3925. {
  3926. if(createCommonPart)
  3927. {
  3928. commonInfo = new CArtifactsOfHero::SCommonPart;
  3929. commonInfo->participants.insert(this);
  3930. }
  3931. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3932. pos += position;
  3933. artWorn.resize(19);
  3934. std::vector<Rect> slotPos;
  3935. slotPos += genRect(44,44,509,30), genRect(44,44,567,240), genRect(44,44,509,80),
  3936. genRect(44,44,383,68), genRect(44,44,564,183), genRect(44,44,509,130),
  3937. genRect(44,44,431,68), genRect(44,44,610,183), genRect(44,44,515,295),
  3938. genRect(44,44,383,143), genRect(44,44,399,194), genRect(44,44,415,245),
  3939. genRect(44,44,431,296), genRect(44,44,564,30), genRect(44,44,610,30),
  3940. genRect(44,44,610,76), genRect(44,44,610,122), genRect(44,44,610,310),
  3941. genRect(44,44,381,296);
  3942. // Create slots for worn artifacts.
  3943. for (size_t g = 0; g < 19 ; g++)
  3944. {
  3945. artWorn[g] = new CArtPlace(NULL);
  3946. artWorn[g]->pos = slotPos[g] + pos;
  3947. artWorn[g]->ourOwner = this;
  3948. eraseSlotData(artWorn[g], g);
  3949. }
  3950. // Create slots for the backpack.
  3951. for(size_t s=0; s<5; ++s)
  3952. {
  3953. CArtPlace * add = new CArtPlace(NULL);
  3954. add->ourOwner = this;
  3955. add->pos.x = pos.x + 403 + 46*s;
  3956. add->pos.y = pos.y + 365;
  3957. add->pos.h = add->pos.w = 44;
  3958. eraseSlotData(add, 19 + s);
  3959. backpack.push_back(add);
  3960. }
  3961. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  3962. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  3963. }
  3964. CArtifactsOfHero::~CArtifactsOfHero()
  3965. {
  3966. dispose();
  3967. }
  3968. void CArtifactsOfHero::updateParentWindow()
  3969. {
  3970. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  3971. {
  3972. if(updateState)
  3973. chw->curHero = curHero;
  3974. else
  3975. chw->update(curHero, true);
  3976. }
  3977. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  3978. {
  3979. //use our copy of hero to draw window
  3980. if(cew->heroInst[0]->id == curHero->id)
  3981. cew->heroInst[0] = curHero;
  3982. else
  3983. cew->heroInst[1] = curHero;
  3984. if(!updateState)
  3985. {
  3986. cew->deactivate();
  3987. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  3988. // {
  3989. // if(cew->heroInst[g] == curHero)
  3990. // {
  3991. // cew->artifs[g]->setHero(curHero);
  3992. // }
  3993. // }
  3994. cew->prepareBackground();
  3995. cew->redraw();
  3996. cew->activate();
  3997. }
  3998. }
  3999. }
  4000. void CArtifactsOfHero::safeRedraw()
  4001. {
  4002. if (active)
  4003. {
  4004. if(parent)
  4005. parent->redraw();
  4006. else
  4007. redraw();
  4008. }
  4009. }
  4010. void CArtifactsOfHero::realizeCurrentTransaction()
  4011. {
  4012. assert(commonInfo->src.AOH);
  4013. assert(commonInfo->dst.AOH);
  4014. LOCPLINT->cb->swapArtifacts(ArtifactLocation(commonInfo->src.AOH->curHero, commonInfo->src.slotID),
  4015. ArtifactLocation(commonInfo->dst.AOH->curHero, commonInfo->dst.slotID));
  4016. }
  4017. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4018. {
  4019. bool isCurHeroSrc = src.isHolder(curHero),
  4020. isCurHeroDst = dst.isHolder(curHero);
  4021. if(isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  4022. updateSlot(src.slot);
  4023. if(isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START)
  4024. updateSlot(dst.slot);
  4025. if(isCurHeroSrc || isCurHeroDst) //we need to update all slots, artifact might be combined and affect more slots
  4026. updateWornSlots(false);
  4027. if (!src.isHolder(curHero) && !isCurHeroDst)
  4028. return;
  4029. if(commonInfo->src == src) //artifact was taken from us
  4030. {
  4031. assert(commonInfo->dst == dst //expected movement from slot ot slot
  4032. || dst.slot == dst.getHolderArtSet()->artifactsInBackpack.size() + GameConstants::BACKPACK_START //artifact moved back to backpack (eg. to make place for art we are moving)
  4033. || dst.getHolderArtSet()->bearerType() != ArtBearer::HERO);
  4034. commonInfo->reset();
  4035. unmarkSlots();
  4036. }
  4037. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4038. {
  4039. assert(dst.slot >= GameConstants::BACKPACK_START);
  4040. commonInfo->reset();
  4041. CArtPlace *ap = NULL;
  4042. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4043. {
  4044. if(dst.isHolder(aoh->curHero))
  4045. {
  4046. commonInfo->src.AOH = aoh;
  4047. if((ap = aoh->getArtPlace(dst.slot)))
  4048. break;
  4049. }
  4050. }
  4051. if(ap)
  4052. {
  4053. ap->select();
  4054. }
  4055. else
  4056. {
  4057. commonInfo->src.art = dst.getArt();
  4058. commonInfo->src.slotID = dst.slot;
  4059. assert(commonInfo->src.AOH);
  4060. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[dst.getArt()->artType->id].bitmap);
  4061. markPossibleSlots(dst.getArt());
  4062. }
  4063. }
  4064. else if(src.slot >= GameConstants::BACKPACK_START &&
  4065. src.slot < commonInfo->src.slotID &&
  4066. src.isHolder(commonInfo->src.AOH->curHero)) //artifact taken from before currently picked one
  4067. {
  4068. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4069. commonInfo->src.slotID--;
  4070. assert(commonInfo->src.valid());
  4071. }
  4072. else
  4073. {
  4074. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  4075. // however after first movement we pick the art from backpack and the second movement coming when
  4076. // we have a different artifact may look surprising... but it's valid.
  4077. //tlog1 << "Unexpected artifact movement...\n";
  4078. }
  4079. updateParentWindow();
  4080. int shift = 0;
  4081. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4082. // shift++;
  4083. //
  4084. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  4085. shift++;
  4086. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  4087. shift--;
  4088. if( (isCurHeroSrc && src.slot >= GameConstants::BACKPACK_START)
  4089. || (isCurHeroDst && dst.slot >= GameConstants::BACKPACK_START) )
  4090. scrollBackpack(shift); //update backpack slots
  4091. }
  4092. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  4093. {
  4094. if(al.isHolder(curHero))
  4095. {
  4096. if(al.slot < GameConstants::BACKPACK_START)
  4097. updateWornSlots(0);
  4098. else
  4099. scrollBackpack(0); //update backpack slots
  4100. }
  4101. }
  4102. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4103. {
  4104. if(slot < GameConstants::BACKPACK_START)
  4105. {
  4106. return artWorn[slot];
  4107. }
  4108. else
  4109. {
  4110. BOOST_FOREACH(CArtPlace *ap, backpack)
  4111. if(ap->slotID == slot)
  4112. return ap;
  4113. }
  4114. return NULL;
  4115. }
  4116. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4117. {
  4118. if(al.isHolder(curHero))
  4119. updateWornSlots();
  4120. }
  4121. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4122. {
  4123. if(al.isHolder(curHero))
  4124. updateWornSlots();
  4125. }
  4126. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4127. {
  4128. for(int i = 0; i < artWorn.size(); i++)
  4129. updateSlot(i);
  4130. if(redrawParent)
  4131. updateParentWindow();
  4132. }
  4133. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4134. {
  4135. return curHero;
  4136. }
  4137. void CArtifactsOfHero::updateSlot(int slotID)
  4138. {
  4139. setSlotData(getArtPlace(slotID), slotID);
  4140. }
  4141. void CExchangeWindow::questlog(int whichHero)
  4142. {
  4143. CCS->curh->dragAndDropCursor(NULL);
  4144. }
  4145. void CExchangeWindow::prepareBackground()
  4146. {
  4147. //printing heroes' names and levels
  4148. auto genTitle = [](const CGHeroInstance *h)
  4149. {
  4150. return boost::str(boost::format(CGI->generaltexth->allTexts[138])
  4151. % h->name % h->level % h->type->heroClass->name);
  4152. };
  4153. new CLabel(147, 25, FONT_SMALL, CENTER, Colors::Cornsilk, genTitle(heroInst[0]));
  4154. new CLabel(653, 25, FONT_SMALL, CENTER, Colors::Cornsilk, genTitle(heroInst[1]));
  4155. //printing primary skills
  4156. for(int g=0; g<4; ++g)
  4157. new CAnimImage("PSKIL32", g, 0, 385, 19 + 36*g);
  4158. //heroes related thing
  4159. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4160. {
  4161. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  4162. //printing primary skills' amounts
  4163. for(int m=0; m<4; ++m)
  4164. new CLabel(352 + 93 * b, 35 + 36 * m, FONT_SMALL, CENTER, Colors::Cornsilk,
  4165. boost::lexical_cast<std::string>(heroWArt.getPrimSkillLevel(m)));
  4166. //printing secondary skills
  4167. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4168. {
  4169. int id = heroInst[b]->secSkills[m].first;
  4170. int level = heroInst[b]->secSkills[m].second;
  4171. new CAnimImage("SECSK32", id*3 + level + 2 , 0, 32 + 36 * m + 454 * b, 88);
  4172. }
  4173. //hero's specialty
  4174. new CAnimImage("UN32", heroInst[b]->subID, 0, 67 + 490*b, 45);
  4175. //experience
  4176. new CAnimImage("PSKIL32", 4, 0, 103 + 490*b, 45);
  4177. new CLabel(119 + 490*b, 71, FONT_SMALL, CENTER, Colors::Cornsilk, makeNumberShort(heroInst[b]->exp));
  4178. //mana points
  4179. new CAnimImage("PSKIL32", 5, 0, 139 + 490*b, 45);
  4180. new CLabel(155 + 490*b, 71, FONT_SMALL, CENTER, Colors::Cornsilk, makeNumberShort(heroInst[b]->mana));
  4181. }
  4182. //printing portraits
  4183. new CAnimImage("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  4184. new CAnimImage("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  4185. }
  4186. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2):
  4187. CWindowObject(PLAYER_COLORED | BORDERED, "TRADE2")
  4188. {
  4189. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4190. char bufor[400];
  4191. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  4192. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  4193. prepareBackground();
  4194. artifs[0] = new CArtifactsOfHero(Point(-334, 150));
  4195. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4196. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4197. artifs[0]->setHero(heroInst[0]);
  4198. artifs[1] = new CArtifactsOfHero(Point(96, 150));
  4199. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4200. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4201. artifs[1]->setHero(heroInst[1]);
  4202. artSets.push_back(artifs[0]);
  4203. artSets.push_back(artifs[1]);
  4204. //primary skills
  4205. for(int g=0; g<4; ++g)
  4206. {
  4207. //primary skill's clickable areas
  4208. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4209. primSkillAreas[g]->pos = genRect(32, 140, pos.x + 329, pos.y + 19 + 36 * g);
  4210. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4211. primSkillAreas[g]->type = g;
  4212. primSkillAreas[g]->bonusValue = -1;
  4213. primSkillAreas[g]->baseType = 0;
  4214. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4215. primSkillAreas[g]->hoverText = std::string(bufor);
  4216. }
  4217. //heroes related thing
  4218. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4219. {
  4220. //secondary skill's clickable areas
  4221. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4222. {
  4223. int skill = heroInst[b]->secSkills[g].first,
  4224. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4225. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4226. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4227. secSkillAreas[b][g]->baseType = 1;
  4228. secSkillAreas[b][g]->type = skill;
  4229. secSkillAreas[b][g]->bonusValue = level;
  4230. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4231. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4232. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4233. }
  4234. portrait[b] = new CHeroArea(257 + 228*b, 13, heroInst[b]);
  4235. speciality[b] = new LRClickableAreaWText();
  4236. speciality[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4237. speciality[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4238. speciality[b]->text = CGI->generaltexth->hTxts[heroInst[b]->subID].longBonus;
  4239. experience[b] = new LRClickableAreaWText();
  4240. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4241. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4242. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4243. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4244. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4245. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4246. spellPoints[b] = new LRClickableAreaWText();
  4247. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4248. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4249. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4250. spellPoints[b]->text = std::string(bufor);
  4251. //setting morale
  4252. morale[b] = new MoraleLuckBox(true, genRect(32, 32, 176 + 490*b, 39), true);
  4253. morale[b]->set(heroInst[b]);
  4254. //setting luck
  4255. luck[b] = new MoraleLuckBox(false, genRect(32, 32, 212 + 490*b, 39), true);
  4256. luck[b]->set(heroInst[b]);
  4257. }
  4258. //buttons
  4259. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), 732, 567, "IOKAY.DEF", SDLK_RETURN);
  4260. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 0), 10, 44, "hsbtns4.def");
  4261. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], "", boost::bind(&CExchangeWindow::questlog,this, 1), 740, 44, "hsbtns4.def");
  4262. Rect barRect(5, 578, 725, 18);
  4263. ourBar = new CGStatusBar(new CPicture(*background, barRect, 5, 578, false));
  4264. //garrison interface
  4265. garr = new CGarrisonInt(69, 131, 4, Point(418,0), *background, Point(69,131), heroInst[0],heroInst[1], true, true);
  4266. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 10, 132, "TSBTNS.DEF"));
  4267. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3], "", boost::bind(&CGarrisonInt::splitClick, garr), 740, 132, "TSBTNS.DEF"));
  4268. }
  4269. CExchangeWindow::~CExchangeWindow() //d-tor
  4270. {
  4271. delete artifs[0]->commonInfo;
  4272. artifs[0]->commonInfo = NULL;
  4273. artifs[1]->commonInfo = NULL;
  4274. }
  4275. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy):
  4276. CWindowObject(PLAYER_COLORED, "TPSHIP")
  4277. {
  4278. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4279. bgWater = new CPicture("TPSHIPBK", 100, 69);
  4280. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  4281. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  4282. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  4283. bgShip->center(waterCenter);
  4284. // Create resource icons and costs.
  4285. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  4286. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  4287. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::Cornsilk, goldValue);
  4288. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::Cornsilk, woodValue);
  4289. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  4290. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  4291. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CShipyardWindow::close, this), 224, 312, "ICANCEL", SDLK_RETURN);
  4292. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CShipyardWindow::close, this), 42, 312, "IBUY30", SDLK_RETURN);
  4293. build->callback += onBuy;
  4294. for(size_t i = 0; i < cost.size(); i++)
  4295. {
  4296. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4297. {
  4298. build->block(true);
  4299. break;
  4300. }
  4301. }
  4302. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4303. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->jktexts[13]);
  4304. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::Cornsilk, CGI->generaltexth->jktexts[14]);
  4305. }
  4306. CPuzzleWindow::CPuzzleWindow(const int3 &GrailPos, double discoveredRatio):
  4307. CWindowObject(PLAYER_COLORED | BORDERED, "PUZZLE"),
  4308. grailPos(GrailPos),
  4309. currentAlpha(SDL_ALPHA_OPAQUE)
  4310. {
  4311. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4312. CCS->soundh->playSound(soundBase::OBELISK);
  4313. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CPuzzleWindow::close, this), 670, 538, "IOK6432.DEF", SDLK_RETURN);
  4314. quitb->assignedKeys.insert(SDLK_ESCAPE);
  4315. quitb->borderColor = Colors::MetallicGold;
  4316. quitb->borderEnabled = true;
  4317. new CPicture("PUZZLOGO", 607, 3);
  4318. new CLabel(700, 95, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[463]);
  4319. new CResDataBar("ZRESBAR.bmp", 3, 575, 32, 2, 85, 85);
  4320. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  4321. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  4322. {
  4323. const SPuzzleInfo & info = CGI->heroh->puzzleInfo[faction][g];
  4324. auto piece = new CPicture(info.filename, info.x, info.y);
  4325. //piece that will slowly disappear
  4326. if(info.whenUncovered < PUZZLES_PER_FACTION * discoveredRatio)
  4327. {
  4328. piecesToRemove.push_back(piece);
  4329. piece->needRefresh = true;
  4330. piece->recActions = piece->recActions & ~SHOWALL;
  4331. }
  4332. }
  4333. }
  4334. void CPuzzleWindow::showAll(SDL_Surface * to)
  4335. {
  4336. int3 moveInt = int3(8, 9, 0);
  4337. Rect mapRect = genRect(544, 591, pos.x + 8, pos.y + 7);
  4338. CGI->mh->terrainRect
  4339. (grailPos - moveInt, adventureInt->anim,
  4340. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  4341. to, &mapRect, 0, 0, true, moveInt);
  4342. CWindowObject::showAll(to);
  4343. }
  4344. void CPuzzleWindow::show(SDL_Surface * to)
  4345. {
  4346. static int animSpeed = 2;
  4347. if (currentAlpha < animSpeed)
  4348. {
  4349. //animation done
  4350. BOOST_FOREACH(auto & piece, piecesToRemove)
  4351. delete piece;
  4352. piecesToRemove.clear();
  4353. }
  4354. else
  4355. {
  4356. //update disappearing puzzles
  4357. BOOST_FOREACH(auto & piece, piecesToRemove)
  4358. piece->setAlpha(currentAlpha);
  4359. currentAlpha -= animSpeed;
  4360. }
  4361. CWindowObject::show(to);
  4362. }
  4363. void CTransformerWindow::CItem::move()
  4364. {
  4365. if (left)
  4366. moveBy(Point(289, 0));
  4367. else
  4368. moveBy(Point(-289, 0));
  4369. left = !left;
  4370. }
  4371. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  4372. {
  4373. if(previousState && (!down))
  4374. {
  4375. move();
  4376. parent->showAll(screen2);
  4377. }
  4378. }
  4379. void CTransformerWindow::CItem::update()
  4380. {
  4381. icon->setFrame(parent->army->getCreature(id)->idNumber + 2);
  4382. }
  4383. CTransformerWindow::CItem::CItem(CTransformerWindow * _parent, int _size, int _id):
  4384. id(_id), size(_size), parent(_parent)
  4385. {
  4386. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4387. addUsedEvents(LCLICK);
  4388. left = true;
  4389. pos.w = 58;
  4390. pos.h = 64;
  4391. pos.x += 45 + (id%3)*83 + id/6*83;
  4392. pos.y += 109 + (id/3)*98;
  4393. icon = new CAnimImage("TWCRPORT", parent->army->getCreature(id)->idNumber + 2);
  4394. new CLabel(28, 76,FONT_SMALL, CENTER, Colors::Cornsilk, boost::lexical_cast<std::string>(size));//stack size
  4395. }
  4396. void CTransformerWindow::makeDeal()
  4397. {
  4398. for (int i=0; i<items.size(); i++)
  4399. if (!items[i]->left)
  4400. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, items[i]->id, 0, 0, hero);
  4401. }
  4402. void CTransformerWindow::addAll()
  4403. {
  4404. for (int i=0; i<items.size(); i++)
  4405. if (items[i]->left)
  4406. items[i]->move();
  4407. showAll(screen2);
  4408. }
  4409. void CTransformerWindow::updateGarrisons()
  4410. {
  4411. BOOST_FOREACH(auto & item, items)
  4412. {
  4413. item->update();
  4414. }
  4415. }
  4416. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):
  4417. CWindowObject(PLAYER_COLORED, "SKTRNBK"),
  4418. hero(_hero),
  4419. town(_town)
  4420. {
  4421. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4422. if (hero)
  4423. army = hero;
  4424. else
  4425. army = town;
  4426. for (int i=0; i<7; i++ )
  4427. if ( army->getCreature(i) )
  4428. items.push_back(new CItem(this, army->getStackCount(i), i));
  4429. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  4430. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  4431. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CTransformerWindow::close, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  4432. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4433. new CLabel(153, 29,FONT_SMALL, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[485]);//holding area
  4434. new CLabel(153+295, 29, FONT_SMALL, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[486]);//transformer
  4435. new CTextBox(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::Jasmine);//move creatures to create skeletons
  4436. new CTextBox(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::Jasmine);//creatures here will become skeletons
  4437. }
  4438. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  4439. {
  4440. if(previousState && (!down))
  4441. {
  4442. if ( state() != 2 )
  4443. return;
  4444. CUnivConfirmWindow *win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(6) >= 2000);
  4445. GH.pushInt(win);
  4446. }
  4447. }
  4448. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  4449. {
  4450. if(down)
  4451. {
  4452. CInfoPopup *message = new CInfoPopup();
  4453. message->free = true;
  4454. message->bitmap = CMessage::drawBoxTextBitmapSub
  4455. (LOCPLINT->playerID,
  4456. CGI->generaltexth->skillInfoTexts[ID][0],
  4457. graphics->abils82->ourImages[ID*3+3].bitmap,
  4458. CGI->generaltexth->skillName[ID]);
  4459. message->pos.x = screen->w/2 - message->bitmap->w/2;
  4460. message->pos.y = screen->h/2 - message->bitmap->h/2;
  4461. GH.pushInt(message);
  4462. }
  4463. }
  4464. void CUniversityWindow::CItem::hover(bool on)
  4465. {
  4466. if (on)
  4467. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  4468. else
  4469. GH.statusbar->clear();
  4470. }
  4471. int CUniversityWindow::CItem::state()
  4472. {
  4473. if (parent->hero->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ID)))//hero know this skill
  4474. return 1;
  4475. if (parent->hero->secSkills.size() >= GameConstants::SKILL_PER_HERO)//can't learn more skills
  4476. return 0;
  4477. if (parent->hero->type->heroClass->proSec[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  4478. return 0;
  4479. return 2;
  4480. }
  4481. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  4482. {
  4483. CPicture * bar;
  4484. switch (state())
  4485. {
  4486. case 0: bar = parent->red;
  4487. break;
  4488. case 1: bar = parent->yellow;
  4489. break;
  4490. case 2: bar = parent->green;
  4491. break;
  4492. default:bar = NULL;
  4493. break;
  4494. }
  4495. assert(bar);
  4496. blitAtLoc(bar->bg, -28, -22, to);
  4497. blitAtLoc(bar->bg, -28, 48, to);
  4498. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::Cornsilk,to);//Name
  4499. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::Cornsilk,to);//Level(always basic)
  4500. CAnimImage::showAll(to);
  4501. }
  4502. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  4503. CAnimImage ("SECSKILL", _ID*3+3, 0, X, Y),
  4504. ID(_ID),
  4505. parent(_parent)
  4506. {
  4507. addUsedEvents(LCLICK | RCLICK | HOVER);
  4508. }
  4509. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):
  4510. CWindowObject(PLAYER_COLORED, "UNIVERS1"),
  4511. hero(_hero),
  4512. market(_market)
  4513. {
  4514. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4515. green = new CPicture("UNIVGREN.PCX");
  4516. yellow = new CPicture("UNIVGOLD.PCX");//bars
  4517. red = new CPicture("UNIVRED.PCX");
  4518. green->recActions =
  4519. yellow->recActions =
  4520. red->recActions = DISPOSE;
  4521. CIntObject * titlePic = nullptr;
  4522. if ( market->o->ID == Obj::UNIVERSITY ) // this is adventure map university
  4523. titlePic = new CPicture("UNIVBLDG");
  4524. else
  4525. if (market->o->ID == GameConstants::TOWNI_TYPE)
  4526. titlePic = new CAnimImage(CGI->townh->towns[ETownType::CONFLUX].clientInfo.buildingsIcons, EBuilding::MAGIC_UNIVERSITY);
  4527. else
  4528. tlog0<<"Error: Image for university was not found!\n";//This should not happen
  4529. if (titlePic)
  4530. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  4531. //Clerk speech
  4532. new CTextBox(CGI->generaltexth->allTexts[603], Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::Cornsilk);
  4533. //University
  4534. new CLabel(231, 26, FONT_MEDIUM, CENTER, Colors::Jasmine, CGI->generaltexth->allTexts[602]);
  4535. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  4536. assert(list.size() == 4);
  4537. for (int i=0; i<list.size(); i++)//prepare clickable items
  4538. items.push_back(new CItem(this, list[i], 54+i*104, 234));
  4539. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  4540. boost::bind(&CUniversityWindow::close, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  4541. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4542. }
  4543. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):
  4544. CWindowObject(PLAYER_COLORED, "UNIVERS2.PCX"),
  4545. parent(PARENT)
  4546. {
  4547. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4548. std::string text = CGI->generaltexth->allTexts[608];
  4549. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4550. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4551. boost::replace_first(text, "%d", "2000");
  4552. new CTextBox(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::Cornsilk);//Clerk speech
  4553. new CLabel(230, 37, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth-> skillName[SKILL]);//Skill name
  4554. new CAnimImage("SECSKILL", SKILL*3+3, 0, 211, 51);//skill
  4555. new CLabel(230, 107, FONT_SMALL, CENTER, Colors::Cornsilk, CGI->generaltexth->levels[1]);//Skill level
  4556. new CAnimImage("RESOURCE", Res::GOLD, 0, 210, 210);//gold
  4557. new CLabel(230, 267, FONT_SMALL, CENTER, Colors::Cornsilk, "2000");//Cost
  4558. std::string hoverText = CGI->generaltexth->allTexts[609];
  4559. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  4560. text = CGI->generaltexth->zelp[633].second;
  4561. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4562. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4563. boost::replace_first(text, "%d", "2000");
  4564. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  4565. 148,299,"IBY6432.DEF",SDLK_RETURN);
  4566. confirm->block(!available);
  4567. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CUnivConfirmWindow::close, this),
  4568. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  4569. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4570. }
  4571. void CUnivConfirmWindow::makeDeal(int skill)
  4572. {
  4573. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  4574. close();
  4575. }
  4576. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  4577. CWindowObject(PLAYER_COLORED, "APHLFTBK"),
  4578. fort(object),
  4579. hero(visitor)
  4580. {
  4581. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4582. slotsCount=7;
  4583. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  4584. new CLabel(325, 32, FONT_BIG, CENTER, Colors::Jasmine, fort->hoverName);//Hill Fort
  4585. heroPic = new CHeroArea(30, 60, hero);
  4586. currState.resize(slotsCount+1);
  4587. costs.resize(slotsCount);
  4588. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  4589. std::vector<std::string> files;
  4590. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  4591. for (int i=0; i<slotsCount; i++)
  4592. {
  4593. currState[i] = getState(i);
  4594. upgrade[i] = new CAdventureMapButton(getTextForSlot(i),"",boost::bind(&CHillFortWindow::makeDeal, this, i),
  4595. 107+i*76, 171, "", SDLK_1+i, &files);
  4596. upgrade[i]->block(currState[i] == -1);
  4597. }
  4598. files.clear();
  4599. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  4600. currState[slotsCount] = getState(slotsCount);
  4601. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, slotsCount),
  4602. 30, 231, "", SDLK_0, &files);
  4603. quit = new CAdventureMapButton("","",boost::bind(&CHillFortWindow::close, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  4604. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  4605. garr = new CGarrisonInt(108, 60, 18, Point(),background->bg,Point(108,60),hero,NULL);
  4606. updateGarrisons();
  4607. }
  4608. void CHillFortWindow::updateGarrisons()
  4609. {
  4610. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  4611. totalSumm[i]=0;
  4612. for (int i=0; i<slotsCount; i++)
  4613. {
  4614. costs[i].clear();
  4615. int newState = getState(i);
  4616. if (newState != -1)
  4617. {
  4618. UpgradeInfo info;
  4619. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  4620. if (info.newID.size())//we have upgrades here - update costs
  4621. {
  4622. costs[i] = info.cost[0] * hero->getStackCount(i);
  4623. totalSumm += costs[i];
  4624. }
  4625. }
  4626. currState[i] = newState;
  4627. upgrade[i]->setIndex(newState);
  4628. upgrade[i]->block(currState[i] == -1);
  4629. upgrade[i]->hoverTexts[0] = getTextForSlot(i);
  4630. }
  4631. int newState = getState(slotsCount);
  4632. currState[slotsCount] = newState;
  4633. upgradeAll->setIndex(newState);
  4634. garr->recreateSlots();
  4635. }
  4636. void CHillFortWindow::makeDeal(int slot)
  4637. {
  4638. int offset = (slot == slotsCount)?2:0;
  4639. switch (currState[slot])
  4640. {
  4641. case 0:
  4642. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  4643. std::vector<CComponent*>(), soundBase::sound_todo);
  4644. break;
  4645. case 1:
  4646. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  4647. std::vector<CComponent*>(), soundBase::sound_todo);
  4648. break;
  4649. case 2:
  4650. for (int i=0; i<slotsCount; i++)
  4651. if ( slot ==i || ( slot == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  4652. {
  4653. UpgradeInfo info;
  4654. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  4655. LOCPLINT->cb->upgradeCreature(hero, i, info.newID[0]);
  4656. }
  4657. break;
  4658. }
  4659. }
  4660. void CHillFortWindow::showAll (SDL_Surface *to)
  4661. {
  4662. CWindowObject::showAll(to);
  4663. for ( int i=0; i<slotsCount; i++)
  4664. {
  4665. if ( currState[i] == 0 || currState[i] == 2 )
  4666. {
  4667. if ( costs[i].size() )//we have several elements
  4668. {
  4669. int curY = 128;//reverse iterator is used to display gold as first element
  4670. for(int j = costs[i].size()-1; j >= 0; j--)
  4671. {
  4672. int val = costs[i][j];
  4673. if(!val) continue;
  4674. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  4675. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::Cornsilk, to);
  4676. curY += 20;
  4677. }
  4678. }
  4679. else//free upgrade - print gold image and "Free" text
  4680. {
  4681. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  4682. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::Cornsilk, to);
  4683. }
  4684. }
  4685. }
  4686. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  4687. {
  4688. if (totalSumm[i])//this resource is used - display it
  4689. {
  4690. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  4691. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::Cornsilk, to);
  4692. }
  4693. }
  4694. }
  4695. std::string CHillFortWindow::getTextForSlot(int slot)
  4696. {
  4697. if ( !hero->getCreature(slot) )//we dont have creature here
  4698. return "";
  4699. std::string str = CGI->generaltexth->allTexts[318];
  4700. int amount = hero->getStackCount(slot);
  4701. if ( amount == 1 )
  4702. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  4703. else
  4704. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  4705. return str;
  4706. }
  4707. int CHillFortWindow::getState(int slot)
  4708. {
  4709. TResources myRes = LOCPLINT->cb->getResourceAmount();
  4710. if ( slot == slotsCount )//"Upgrade all" slot
  4711. {
  4712. bool allUpgraded = true;//All creatures are upgraded?
  4713. for (int i=0; i<slotsCount; i++)
  4714. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  4715. if (allUpgraded)
  4716. return 1;
  4717. if(!totalSumm.canBeAfforded(myRes))
  4718. return 0;
  4719. return 2;
  4720. }
  4721. if (hero->slotEmpty(slot))//no creature here
  4722. return -1;
  4723. UpgradeInfo info;
  4724. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  4725. if (!info.newID.size())//already upgraded
  4726. return 1;
  4727. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  4728. return 0;
  4729. return 2;//can upgrade
  4730. }
  4731. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  4732. CWindowObject(PLAYER_COLORED | BORDERED, "TpRank"),
  4733. owner(_owner)
  4734. {
  4735. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4736. type |= BLOCK_ADV_HOTKEYS;
  4737. SThievesGuildInfo tgi; //info to be displayed
  4738. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  4739. exitb = new CAdventureMapButton (CGI->generaltexth->allTexts[600], "", boost::bind(&CThievesGuildWindow::close,this), 748, 556, "TPMAGE1", SDLK_RETURN);
  4740. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  4741. resdatabar = new CMinorResDataBar();
  4742. resdatabar->pos.x += pos.x;
  4743. resdatabar->pos.y += pos.y;
  4744. //data for information table:
  4745. // fields[row][column] = list of id's of players for this box
  4746. static std::vector< std::vector< ui8 > > SThievesGuildInfo::* fields[] =
  4747. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  4748. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  4749. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  4750. //printing texts & descriptions to background
  4751. for(int g=0; g<12; ++g)
  4752. {
  4753. int posY[] = {400, 460, 510};
  4754. int y;
  4755. if(g < 9)
  4756. y = 52 + 32*g;
  4757. else
  4758. y = posY[g-9];
  4759. std::string text = CGI->generaltexth->jktexts[24+g];
  4760. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  4761. new CLabel(135, y, FONT_MEDIUM, CENTER, Colors::Jasmine, text);
  4762. }
  4763. for(int g=1; g<tgi.playerColors.size(); ++g)
  4764. new CAnimImage("PRSTRIPS", g-1, 0, 250 + 66*g, 7);
  4765. for(int g=0; g<tgi.playerColors.size(); ++g)
  4766. new CLabel(283 + 66*g, 24, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->jktexts[16+g]);
  4767. //printing flags
  4768. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  4769. {
  4770. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  4771. {
  4772. std::vector<ui8> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  4773. //position of box
  4774. int xpos = 259 + 66 * b;
  4775. int ypos = 41 + 32 * g;
  4776. size_t rowLength[2]; //size of each row
  4777. rowLength[0] = std::min<size_t>(players.size(), 4);
  4778. rowLength[1] = players.size() - rowLength[0];
  4779. for (size_t j=0; j< 2; j++)
  4780. {
  4781. // origin of this row | offset for 2nd row| shift right for short rows
  4782. int rowStartX = xpos + (j ? 6 : 0) + (rowLength[j] < 3 ? 12 : 0);
  4783. int rowStartY = ypos + (j ? 4 : 0);
  4784. for (size_t i=0; i< rowLength[j]; i++)
  4785. {
  4786. new CAnimImage("itgflags", players[i + j*4], 0, rowStartX + i*12, rowStartY);
  4787. }
  4788. }
  4789. }
  4790. }
  4791. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  4792. //printing best hero
  4793. int counter = 0;
  4794. BOOST_FOREACH(auto & iter, tgi.colorToBestHero)
  4795. {
  4796. if(iter.second.portrait >= 0)
  4797. {
  4798. new CPicture(colorToBox[iter.first], 253 + 66 * counter, 334);
  4799. new CAnimImage("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360);
  4800. //TODO: r-click info:
  4801. // - r-click on hero
  4802. // - r-click on primary skill label
  4803. if(iter.second.details)
  4804. {
  4805. new CTextBox(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  4806. 0, FONT_TINY, TOPLEFT, Colors::Cornsilk);
  4807. for (int i=0; i<iter.second.details->primskills.size(); ++i)
  4808. {
  4809. new CLabel(310 + 66 * counter, 407 + 11*i, FONT_TINY, BOTTOMRIGHT, Colors::Cornsilk,
  4810. boost::lexical_cast<std::string>(iter.second.details->primskills[i]));
  4811. }
  4812. }
  4813. }
  4814. counter++;
  4815. }
  4816. //printing best creature
  4817. counter = 0;
  4818. BOOST_FOREACH(auto & it, tgi.bestCreature)
  4819. {
  4820. if(it.second >= 0)
  4821. new CAnimImage("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479);
  4822. counter++;
  4823. }
  4824. //printing personality
  4825. counter = 0;
  4826. BOOST_FOREACH(auto & it, tgi.personality)
  4827. {
  4828. std::string text;
  4829. if(it.second == -1)
  4830. text = CGI->generaltexth->arraytxt[172];
  4831. else
  4832. if (it.second)
  4833. text = CGI->generaltexth->arraytxt[168 + it.second];
  4834. new CLabel(283 + 66*counter, 459, FONT_SMALL, CENTER, Colors::Cornsilk, text);
  4835. counter++;
  4836. }
  4837. }
  4838. void MoraleLuckBox::set(const IBonusBearer *node)
  4839. {
  4840. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4841. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  4842. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  4843. 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.
  4844. const int componentType[] = {CComponent::luck, CComponent::morale};
  4845. const int hoverTextBase[] = {7, 4};
  4846. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  4847. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  4848. int mrlt = -9;
  4849. TModDescr mrl;
  4850. if (node)
  4851. {
  4852. node->getModifiersWDescr(mrl, bonusType[morale]);
  4853. bonusValue = (node->*getValue[morale])();
  4854. }
  4855. else
  4856. bonusValue = 0;
  4857. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  4858. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  4859. baseType = componentType[morale];
  4860. text = CGI->generaltexth->arraytxt[textId[morale]];
  4861. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  4862. if (!mrl.size())
  4863. text += CGI->generaltexth->arraytxt[noneTxtId];
  4864. else
  4865. {
  4866. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  4867. {
  4868. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  4869. }
  4870. else
  4871. {
  4872. for(int it=0; it < mrl.size(); it++)
  4873. text += "\n" + mrl[it].second;
  4874. }
  4875. }
  4876. std::string imageName;
  4877. if (small)
  4878. imageName = morale ? "IMRL30": "ILCK30";
  4879. else
  4880. imageName = morale ? "IMRL42" : "ILCK42";
  4881. delete image;
  4882. image = new CAnimImage(imageName, bonusValue + 3);
  4883. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  4884. }
  4885. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small):
  4886. image(NULL),
  4887. morale(Morale),
  4888. small(Small)
  4889. {
  4890. bonusValue = 0;
  4891. pos = r + pos;
  4892. }
  4893. CArtifactHolder::CArtifactHolder()
  4894. {
  4895. }
  4896. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  4897. {
  4898. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  4899. aoh->artifactRemoved(artLoc);
  4900. }
  4901. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  4902. {
  4903. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  4904. aoh->artifactMoved(artLoc, destLoc);
  4905. }
  4906. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  4907. {
  4908. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  4909. aoh->artifactDisassembled(artLoc);
  4910. }
  4911. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  4912. {
  4913. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  4914. aoh->artifactAssembled(artLoc);
  4915. }
  4916. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  4917. {
  4918. slotID = -1;
  4919. AOH = NULL;
  4920. art = NULL;
  4921. }
  4922. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  4923. {
  4924. clear();
  4925. }
  4926. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  4927. {
  4928. slotID = place->slotID;
  4929. AOH = place->ourOwner;
  4930. if(slotID >= 19 && dontTakeBackpack)
  4931. art = NULL;
  4932. else
  4933. art = place->ourArt;
  4934. }
  4935. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  4936. {
  4937. if(!AOH)
  4938. return false;
  4939. bool ret = al.isHolder(AOH->curHero) && al.slot == slotID;
  4940. //assert(al.getArt() == art);
  4941. return ret;
  4942. }
  4943. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  4944. {
  4945. assert(AOH && art);
  4946. return art == AOH->curHero->getArt(slotID);
  4947. }
  4948. void CRClickPopup::clickRight(tribool down, bool previousState)
  4949. {
  4950. if(down)
  4951. return;
  4952. close();
  4953. }
  4954. void CRClickPopup::close()
  4955. {
  4956. GH.popIntTotally(this);
  4957. }
  4958. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  4959. {
  4960. int player = LOCPLINT ? LOCPLINT->playerID : 1; //if no player, then use blue
  4961. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  4962. temp->center(Point(GH.current->motion)); //center on mouse
  4963. temp->fitToScreen(10);
  4964. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  4965. GH.pushInt(rcpi);
  4966. }
  4967. Point CInfoBoxPopup::toScreen(Point p)
  4968. {
  4969. vstd::abetween(p.x, adventureInt->terrain.pos.x + 100, adventureInt->terrain.pos.x + adventureInt->terrain.pos.w - 100);
  4970. vstd::abetween(p.y, adventureInt->terrain.pos.y + 100, adventureInt->terrain.pos.y + adventureInt->terrain.pos.h - 100);
  4971. return p;
  4972. }
  4973. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGTownInstance * town):
  4974. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  4975. {
  4976. InfoAboutTown iah;
  4977. LOCPLINT->cb->getTownInfo(town, iah);
  4978. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4979. new CTownTooltip(Point(9, 10), iah);
  4980. }
  4981. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGHeroInstance * hero):
  4982. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "HEROQVBK", toScreen(position))
  4983. {
  4984. InfoAboutHero iah;
  4985. LOCPLINT->cb->getHeroInfo(hero, iah);
  4986. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4987. new CHeroTooltip(Point(9, 10), iah);
  4988. }
  4989. CInfoBoxPopup::CInfoBoxPopup(Point position, const CGGarrison * garr):
  4990. CWindowObject(RCLICK_POPUP | PLAYER_COLORED, "TOWNQVBK", toScreen(position))
  4991. {
  4992. InfoAboutTown iah;
  4993. LOCPLINT->cb->getTownInfo(garr, iah);
  4994. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4995. new CArmyTooltip(Point(9, 10), iah);
  4996. }
  4997. CIntObject * CRClickPopup::createInfoWin(Point position, const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  4998. {
  4999. if(!specific)
  5000. specific = adventureInt->selection;
  5001. assert(specific);
  5002. switch(specific->ID)
  5003. {
  5004. case GameConstants::HEROI_TYPE:
  5005. return new CInfoBoxPopup(position, dynamic_cast<const CGHeroInstance *>(specific));
  5006. case GameConstants::TOWNI_TYPE:
  5007. return new CInfoBoxPopup(position, dynamic_cast<const CGTownInstance *>(specific));
  5008. case Obj::GARRISON:
  5009. case Obj::GARRISON2:
  5010. return new CInfoBoxPopup(position, dynamic_cast<const CGGarrison *>(specific));
  5011. default:
  5012. return NULL;
  5013. }
  5014. }
  5015. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  5016. {
  5017. CIntObject *iWin = createInfoWin(p, obj); //try get custom infowindow for this obj
  5018. if(iWin)
  5019. GH.pushInt(iWin);
  5020. else
  5021. CRClickPopup::createAndPush(obj->getHoverText());
  5022. }
  5023. CRClickPopup::CRClickPopup()
  5024. {
  5025. addUsedEvents(RCLICK);
  5026. }
  5027. CRClickPopup::~CRClickPopup()
  5028. {
  5029. }
  5030. void CRClickPopupInt::show(SDL_Surface * to)
  5031. {
  5032. inner->show(to);
  5033. }
  5034. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  5035. {
  5036. CCS->curh->hide();
  5037. inner = our;
  5038. delInner = deleteInt;
  5039. }
  5040. CRClickPopupInt::~CRClickPopupInt()
  5041. {
  5042. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  5043. // //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...?)
  5044. // if(LOCPLINT && inner == adventureInt->heroWindow)
  5045. // adventureInt->heroWindow->dispose();
  5046. if(delInner)
  5047. delete inner;
  5048. CCS->curh->show();
  5049. }
  5050. void CRClickPopupInt::showAll(SDL_Surface * to)
  5051. {
  5052. inner->showAll(to);
  5053. }