GUIClasses.cpp 172 KB

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