GUIClasses.cpp 172 KB

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