GUIClasses.cpp 172 KB

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