GUIClasses.cpp 170 KB

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