GUIClasses.cpp 179 KB

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