CObjectHandler.cpp 159 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CObjectHandler.h"
  4. #include "CDefObjInfoHandler.h"
  5. #include "CLodHandler.h"
  6. #include "CGeneralTextHandler.h"
  7. #include "CDefObjInfoHandler.h"
  8. #include "CHeroHandler.h"
  9. #include "CSpellHandler.h"
  10. #include <boost/bind.hpp>
  11. #include <boost/algorithm/string/replace.hpp>
  12. #include <boost/assign/std/vector.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/random/linear_congruential.hpp>
  15. #include "CTownHandler.h"
  16. #include "CArtHandler.h"
  17. #include "CSoundBase.h"
  18. #include "../lib/VCMI_Lib.h"
  19. #include "../lib/IGameCallback.h"
  20. #include "../lib/CGameState.h"
  21. #include "../lib/NetPacks.h"
  22. #include "../StartInfo.h"
  23. #include "../lib/map.h"
  24. #include <sstream>
  25. #include <SDL_stdinc.h>
  26. #include <boost/foreach.hpp>
  27. #include <boost/format.hpp>
  28. using namespace boost::assign;
  29. /*
  30. * CObjectHandler.cpp, part of VCMI engine
  31. *
  32. * Authors: listed in file AUTHORS in main folder
  33. *
  34. * License: GNU General Public License v2.0 or later
  35. * Full text of license available in license.txt file, in main folder
  36. *
  37. */
  38. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  39. std::vector<std::pair<int, int> > CGTeleport::gates;
  40. IGameCallback * IObjectInterface::cb = NULL;
  41. DLL_EXPORT void loadToIt(std::string &dest, const std::string &src, int &iter, int mode);
  42. extern CLodHandler * bitmaph;
  43. extern boost::rand48 ran;
  44. std::map <ui8, std::set <ui8> > CGKeys::playerKeyMap;
  45. std::map <si32, std::vector<si32> > CGMagi::eyelist;
  46. BankConfig CGPyramid::pyramidConfig;
  47. std::map<ui16, Component*> CGArtMerchant::advMapArts;
  48. std::map<ui16, Component*> CGArtMerchant::townArts;
  49. ui8 CGObelisk::obeliskCount; //how many obelisks are on map
  50. std::map<ui8, ui8> CGObelisk::visited; //map: color_id => how many obelisks has been visited
  51. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  52. {};
  53. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  54. {};
  55. void IObjectInterface::newTurn () const
  56. {};
  57. IObjectInterface::~IObjectInterface()
  58. {}
  59. IObjectInterface::IObjectInterface()
  60. {}
  61. void IObjectInterface::initObj()
  62. {}
  63. void IObjectInterface::setProperty( ui8 what, ui32 val )
  64. {}
  65. void IObjectInterface::postInit()
  66. {}
  67. void IObjectInterface::preInit()
  68. {}
  69. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  70. {
  71. if(what == 10)
  72. players.insert((ui8)val);
  73. }
  74. bool CPlayersVisited::hasVisited( ui8 player ) const
  75. {
  76. return vstd::contains(players,player);
  77. }
  78. static void readCreatures(std::istream & is, BankConfig & bc, bool guards) //helper function for void CObjectHandler::loadObjects()
  79. {
  80. const int MAX_BUF = 5000;
  81. char buffer[MAX_BUF + 1];
  82. std::pair<si16, si32> guardInfo = std::make_pair(0, 0);
  83. std::string creName;
  84. is >> guardInfo.second;
  85. //one getline just does not work... probably a kind of left whitespace
  86. is.getline(buffer, MAX_BUF, '\t');
  87. is.getline(buffer, MAX_BUF, '\t');
  88. creName = buffer;
  89. if( std::string(buffer) == "None" ) //no creature to be added
  90. return;
  91. //look for the best creature that is described by given name
  92. if( vstd::contains(VLC->creh->nameToID, creName) )
  93. {
  94. guardInfo.first = VLC->creh->nameToID[creName];
  95. }
  96. else
  97. {
  98. for(int g=0; g<VLC->creh->creatures.size(); ++g)
  99. {
  100. if(VLC->creh->creatures[g]->namePl == creName
  101. || VLC->creh->creatures[g]->nameRef == creName
  102. || VLC->creh->creatures[g]->nameSing == creName)
  103. {
  104. guardInfo.first = VLC->creh->creatures[g]->idNumber;
  105. }
  106. }
  107. }
  108. if(guards)
  109. bc.guards.push_back(guardInfo);
  110. else //given creatures
  111. bc.creatures.push_back(guardInfo);
  112. }
  113. void CObjectHandler::loadObjects()
  114. {
  115. {
  116. tlog5 << "\t\tReading cregens \n";
  117. cregens.resize(110); //TODO: hardcoded value - change
  118. for(size_t i=0; i < cregens.size(); ++i)
  119. {
  120. cregens[i]=-1;
  121. }
  122. std::ifstream ifs(DATA_DIR "/config/cregens.txt");
  123. while(!ifs.eof())
  124. {
  125. int dw, cr;
  126. ifs >> dw >> cr;
  127. cregens[dw]=cr;
  128. }
  129. tlog5 << "\t\tDone loading objects!\n";
  130. ifs.close();
  131. ifs.clear();
  132. int k = -1;
  133. ifs.open(DATA_DIR "/config/resources.txt");
  134. ifs >> k;
  135. int pom;
  136. for(int i=0;i<k;i++)
  137. {
  138. ifs >> pom;
  139. resVals.push_back(pom);
  140. }
  141. tlog5 << "\t\tDone loading resource prices!\n";
  142. }
  143. std::ifstream istr;
  144. istr.open(DATA_DIR "/config/bankconfig.txt", std::ios_base::binary);
  145. if(!istr.is_open())
  146. {
  147. tlog1 << "No config/bankconfig.txt file !!!\n";
  148. }
  149. const int MAX_BUF = 5000;
  150. char buffer[MAX_BUF + 1];
  151. //omitting unnecessary lines
  152. istr.getline(buffer, MAX_BUF);
  153. istr.getline(buffer, MAX_BUF);
  154. for(int g=0; g<21; ++g) //TODO: remove hardcoded value
  155. {
  156. //reading name
  157. istr.getline(buffer, MAX_BUF, '\t');
  158. creBanksNames[g] = std::string(buffer);
  159. //remove trailing new line characters
  160. while(creBanksNames[g][0] == 10 || creBanksNames[g][0] == 13)
  161. creBanksNames[g].erase(creBanksNames[g].begin());
  162. for(int i=0; i<4; ++i) //reading levels
  163. {
  164. banksInfo[g].push_back(new BankConfig);
  165. BankConfig &bc = *banksInfo[g].back();
  166. std::string buf;
  167. char dump;
  168. //bc.level is of type char and thus we cannot read directly to it; same for some othre variables
  169. istr >> buf;
  170. bc.level = atoi(buf.c_str());
  171. istr >> buf;
  172. bc.chance = atoi(buf.c_str());
  173. readCreatures(istr, bc, true);
  174. istr >> buf;
  175. bc.upgradeChance = atoi(buf.c_str());
  176. for(int b=0; b<3; ++b)
  177. readCreatures(istr, bc, true);
  178. istr >> bc.combatValue;
  179. bc.resources.resize(RESOURCE_QUANTITY);
  180. //a dirty trick to make it work if there is no 0 for 0 quantity (like in grotto - last entry)
  181. char buft[52];
  182. istr.getline(buft, 50, '\t');
  183. for(int h=0; h<7; ++h)
  184. {
  185. istr.getline(buft, 50, '\t');
  186. if(buft[0] == '\0')
  187. bc.resources[h] = 0;
  188. else
  189. bc.resources[h] = SDL_atoi(buft);
  190. }
  191. readCreatures(istr, bc, false);
  192. bc.artifacts.resize(4);
  193. for(int b=0; b<4; ++b)
  194. {
  195. istr >> bc.artifacts[b];
  196. }
  197. istr >> bc.value;
  198. istr >> bc.rewardDifficulty;
  199. istr >> buf;
  200. bc.easiest = atoi(buf.c_str());
  201. }
  202. }
  203. }
  204. int CGObjectInstance::getOwner() const
  205. {
  206. //if (state)
  207. // return state->owner;
  208. //else
  209. return tempOwner; //won't have owner
  210. }
  211. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  212. {
  213. pos = int3(-1,-1,-1);
  214. //std::cout << "Tworze obiekt "<<this<<std::endl;
  215. //state = new CLuaObjectScript();
  216. ID = subID = id = -1;
  217. defInfo = NULL;
  218. tempOwner = 254;
  219. blockVisit = false;
  220. }
  221. CGObjectInstance::~CGObjectInstance()
  222. {
  223. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  224. //if (state)
  225. // delete state;
  226. //state=NULL;
  227. }
  228. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  229. //{
  230. // pos = right.pos;
  231. // ID = right.ID;
  232. // subID = right.subID;
  233. // id = right.id;
  234. // defInfo = right.defInfo;
  235. // info = right.info;
  236. // blockVisit = right.blockVisit;
  237. // //state = new CLuaObjectScript(right.state->);
  238. // //*state = *right.state;
  239. // //state = right.state;
  240. // tempOwner = right.tempOwner;
  241. //}
  242. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  243. //{
  244. // pos = right.pos;
  245. // ID = right.ID;
  246. // subID = right.subID;
  247. // id = right.id;
  248. // defInfo = right.defInfo;
  249. // info = right.info;
  250. // blockVisit = right.blockVisit;
  251. // //state = new CLuaObjectScript();
  252. // //*state = *right.state;
  253. // tempOwner = right.tempOwner;
  254. // return *this;
  255. //}
  256. const std::string & CGObjectInstance::getHoverText() const
  257. {
  258. return hoverName;
  259. }
  260. void CGObjectInstance::setOwner(int ow)
  261. {
  262. //if (state)
  263. // state->owner = ow;
  264. //else
  265. tempOwner = ow;
  266. }
  267. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  268. {
  269. return defInfo->width;
  270. }
  271. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  272. {
  273. return defInfo->height;
  274. }
  275. bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
  276. {
  277. if(defInfo==NULL)
  278. {
  279. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  280. return false;
  281. }
  282. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  283. {
  284. return true;
  285. }
  286. return false;
  287. }
  288. bool CGObjectInstance::blockingAt(int x, int y) const
  289. {
  290. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  291. return false;
  292. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  293. return false;
  294. return true;
  295. }
  296. bool CGObjectInstance::coveringAt(int x, int y) const
  297. {
  298. if((defInfo->coverageMap[y] >> (7-(x) )) & 1
  299. || (defInfo->shadowCoverage[y] >> (7-(x) )) & 1)
  300. return true;
  301. return false;
  302. }
  303. std::set<int3> CGObjectInstance::getBlockedPos() const
  304. {
  305. std::set<int3> ret;
  306. for(int w=0; w<getWidth(); ++w)
  307. {
  308. for(int h=0; h<getHeight(); ++h)
  309. {
  310. if(blockingAt(w, h))
  311. ret.insert(int3(pos.x - getWidth() + w + 1, pos.y - getHeight() + h + 1, pos.z));
  312. }
  313. }
  314. return ret;
  315. }
  316. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  317. {
  318. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  319. return true;
  320. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  321. return false;
  322. if(this->pos.y<cmp.pos.y)
  323. return true;
  324. if(this->pos.y>cmp.pos.y)
  325. return false;
  326. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  327. return true;
  328. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  329. return false;
  330. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  331. return true;
  332. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  333. return false;
  334. if(this->pos.x<cmp.pos.x)
  335. return true;
  336. return false;
  337. }
  338. void CGObjectInstance::initObj()
  339. {
  340. }
  341. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  342. {
  343. switch(what)
  344. {
  345. case ObjProperty::OWNER:
  346. tempOwner = val;
  347. break;
  348. case ObjProperty::BLOCKVIS:
  349. blockVisit = val;
  350. break;
  351. case ObjProperty::ID:
  352. ID = val;
  353. break;
  354. }
  355. setPropertyDer(what, val);
  356. }
  357. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  358. {}
  359. int3 CGObjectInstance::getSightCenter() const
  360. {
  361. //return vistiable tile if possible
  362. for(int i=0; i < 8; i++)
  363. for(int j=0; j < 6; j++)
  364. if(visitableAt(i,j))
  365. return(pos + int3(i-7, j-5, 0));
  366. return pos;
  367. }
  368. int CGObjectInstance::getSightRadious() const
  369. {
  370. return 3;
  371. }
  372. void CGObjectInstance::getSightTiles(std::set<int3> &tiles) const //returns reference to the set
  373. {
  374. cb->getTilesInRange(tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  375. }
  376. int3 CGObjectInstance::getVisitableOffset() const
  377. {
  378. for(int y = 0; y < 6; y++)
  379. for (int x = 0; x < 8; x++)
  380. if((defInfo->visitMap[5-y] >> x) & 1)
  381. return int3(x,y,0);
  382. tlog2 << "Warning: getVisitableOffset called on non-visitable obj!\n";
  383. return int3(-1,-1,-1);
  384. }
  385. void CGObjectInstance::getNameVis( std::string &hname ) const
  386. {
  387. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  388. hname = VLC->generaltexth->names[ID];
  389. if(h)
  390. {
  391. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  392. hname += " " + VLC->generaltexth->allTexts[353]; //not visited
  393. else
  394. hname += " " + VLC->generaltexth->allTexts[352]; //visited
  395. }
  396. }
  397. void CGObjectInstance::giveDummyBonus(int heroID, ui8 duration) const
  398. {
  399. GiveBonus gbonus;
  400. gbonus.bonus.type = Bonus::NONE;
  401. gbonus.id = heroID;
  402. gbonus.bonus.duration = duration;
  403. gbonus.bonus.source = Bonus::OBJECT;
  404. gbonus.bonus.id = ID;
  405. cb->giveHeroBonus(&gbonus);
  406. }
  407. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  408. {
  409. }
  410. ui8 CGObjectInstance::getPassableness() const
  411. {
  412. return 0;
  413. }
  414. bool CGObjectInstance::hasShadowAt( int x, int y ) const
  415. {
  416. if( (defInfo->shadowCoverage[y] >> (7-(x) )) & 1 )
  417. return true;
  418. return false;
  419. }
  420. int3 CGObjectInstance::visitablePos() const
  421. {
  422. return pos - getVisitableOffset();
  423. }
  424. static int lowestSpeed(const CGHeroInstance * chi)
  425. {
  426. if(!chi->Slots().size())
  427. {
  428. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  429. return 20;
  430. }
  431. TSlots::const_iterator i = chi->Slots().begin();
  432. //TODO? should speed modifiers (eg from artifacts) affect hero movement?
  433. int ret = (i++)->second.valOfBonuses(Bonus::STACKS_SPEED);
  434. for (;i!=chi->Slots().end();i++)
  435. {
  436. ret = std::min(ret, i->second.valOfBonuses(Bonus::STACKS_SPEED));
  437. }
  438. return ret;
  439. }
  440. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  441. {
  442. //TODO: check if all creatures are on its native terrain and change cost appropriately
  443. //base move cost
  444. unsigned ret = 100;
  445. //if there is road both on dest and src tiles - use road movement cost
  446. if(dest.malle && from.malle)
  447. {
  448. int road = std::min(dest.malle,from.malle); //used road ID
  449. switch(road)
  450. {
  451. case TerrainTile::dirtRoad:
  452. ret = 75;
  453. break;
  454. case TerrainTile::grazvelRoad:
  455. ret = 65;
  456. break;
  457. case TerrainTile::cobblestoneRoad:
  458. ret = 50;
  459. break;
  460. default:
  461. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  462. break;
  463. }
  464. }
  465. else
  466. {
  467. ret = type->heroClass->terrCosts[from.tertype];
  468. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  469. }
  470. return ret;
  471. }
  472. #if 0
  473. // Unused and buggy method.
  474. // - for loop is wrong. will not find all creatures. must use iterator instead.
  475. // - -> is the slot number. use second->first for creature index
  476. // Is lowestSpeed() the correct equivalent ?
  477. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  478. {
  479. unsigned int sl = 100;
  480. for(size_t h=0; h < stacksCount(); ++h)
  481. {
  482. if(VLC->creh->creatures[Slots().find(h)->first]->speed<sl)
  483. sl = VLC->creh->creatures[Slots().find(h)->first]->speed;
  484. }
  485. return sl;
  486. }
  487. #endif
  488. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  489. {
  490. if (toh3m)
  491. {
  492. src.x+=1;
  493. return src;
  494. }
  495. else
  496. {
  497. src.x-=1;
  498. return src;
  499. }
  500. }
  501. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  502. {
  503. if (h3m)
  504. {
  505. return pos;
  506. }
  507. else
  508. {
  509. return convertPosition(pos,false);
  510. }
  511. }
  512. si32 CGHeroInstance::manaLimit() const
  513. {
  514. double modifier = 1.0;
  515. switch(getSecSkillLevel(24)) //intelligence level
  516. {
  517. case 1: modifier+=0.25; break;
  518. case 2: modifier+=0.5; break;
  519. case 3: modifier+=1.0; break;
  520. }
  521. return si32(10*getPrimSkillLevel(3)*modifier);
  522. }
  523. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  524. //{
  525. // if (h3m)
  526. // pos = Pos;
  527. // else
  528. // pos = convertPosition(Pos,true);
  529. //}
  530. bool CGHeroInstance::canWalkOnSea() const
  531. {
  532. return hasBonusOfType(Bonus::FLYING_MOVEMENT) || hasBonusOfType(Bonus::WATER_WALKING);
  533. }
  534. int CGHeroInstance::getPrimSkillLevel(int id) const
  535. {
  536. int ret = valOfBonuses(Bonus::PRIMARY_SKILL, id);
  537. amax(ret, id/2); //minimal value is 0 for attack and defense and 1 for spell power and knowledge
  538. return ret;
  539. }
  540. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  541. {
  542. for(size_t i=0; i < secSkills.size(); ++i)
  543. if(secSkills[i].first==ID)
  544. return secSkills[i].second;
  545. return 0;
  546. }
  547. int CGHeroInstance::maxMovePoints(bool onLand) const
  548. {
  549. int base = -1;
  550. if(onLand)
  551. {
  552. static const int moveForSpeed[] = { 1500, 1560, 1630, 1700, 1760, 1830, 1900, 1960, 2000 }; //first element for 3 and lower; last for 11 and more
  553. int index = lowestSpeed(this) - 3;
  554. amin(index, ARRAY_COUNT(moveForSpeed)-1);
  555. amax(index, 0);
  556. base = moveForSpeed[index];
  557. }
  558. else
  559. {
  560. base = 1500; //on water base movement is always 1500 (speed of army doesn't matter)
  561. }
  562. int bonus = valOfBonuses(Bonus::MOVEMENT) + (onLand ? valOfBonuses(Bonus::LAND_MOVEMENT) : valOfBonuses(Bonus::SEA_MOVEMENT));
  563. double modifier = 0;
  564. if(onLand)
  565. {
  566. //logistics:
  567. switch(getSecSkillLevel(2))
  568. {
  569. case 1:
  570. modifier = 0.1;
  571. break;
  572. case 2:
  573. modifier = 0.2;
  574. break;
  575. case 3:
  576. modifier = 0.3;
  577. break;
  578. }
  579. }
  580. else
  581. {
  582. //navigation:
  583. switch(getSecSkillLevel(5))
  584. {
  585. case 1:
  586. modifier = 0.5;
  587. break;
  588. case 2:
  589. modifier = 1.0;
  590. break;
  591. case 3:
  592. modifier = 1.5;
  593. break;
  594. }
  595. }
  596. return int(base + base*modifier) + bonus;
  597. }
  598. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  599. {
  600. if(pos<19)
  601. if(vstd::contains(artifWorn,pos))
  602. return artifWorn.find(pos)->second;
  603. else
  604. return -1;
  605. else
  606. if(pos-19 < artifacts.size())
  607. return artifacts[pos-19];
  608. else
  609. return -1;
  610. }
  611. const CArtifact * CGHeroInstance::getArt(int pos) const
  612. {
  613. int id = getArtAtPos(pos);
  614. if(id>=0)
  615. return &VLC->arth->artifacts[id];
  616. else
  617. return NULL;
  618. }
  619. int CGHeroInstance::getSpellSecLevel(int spell) const
  620. {
  621. int bestslvl = 0;
  622. if(VLC->spellh->spells[spell].air)
  623. if(getSecSkillLevel(15) >= bestslvl)
  624. {
  625. bestslvl = getSecSkillLevel(15);
  626. }
  627. if(VLC->spellh->spells[spell].fire)
  628. if(getSecSkillLevel(14) >= bestslvl)
  629. {
  630. bestslvl = getSecSkillLevel(14);
  631. }
  632. if(VLC->spellh->spells[spell].water)
  633. if(getSecSkillLevel(16) >= bestslvl)
  634. {
  635. bestslvl = getSecSkillLevel(16);
  636. }
  637. if(VLC->spellh->spells[spell].earth)
  638. if(getSecSkillLevel(17) >= bestslvl)
  639. {
  640. bestslvl = getSecSkillLevel(17);
  641. }
  642. return bestslvl;
  643. }
  644. CGHeroInstance::CGHeroInstance()
  645. : IBoatGenerator(this)
  646. {
  647. ID = HEROI_TYPE;
  648. tacticFormationEnabled = inTownGarrison = false;
  649. mana = movement = portrait = level = -1;
  650. isStanding = true;
  651. moveDir = 4;
  652. exp = 0xffffffff;
  653. visitedTown = NULL;
  654. type = NULL;
  655. boat = NULL;
  656. secSkills.push_back(std::make_pair(-1, -1));
  657. }
  658. void CGHeroInstance::initHero(int SUBID)
  659. {
  660. subID = SUBID;
  661. initHero();
  662. }
  663. void CGHeroInstance::initHero()
  664. {
  665. assert(validTypes(true));
  666. if(ID == HEROI_TYPE)
  667. initHeroDefInfo();
  668. if(!type)
  669. type = VLC->heroh->heroes[subID];
  670. if(!vstd::contains(spells, 0xffffffff) && type->startingSpell >= 0) //hero starts with a spell
  671. spells.insert(type->startingSpell);
  672. else //remove placeholder
  673. spells -= 0xffffffff;
  674. if(!vstd::contains(artifWorn, 16) && type->startingSpell >= 0) //no catapult means we haven't read pre-existant set
  675. {
  676. VLC->arth->equipArtifact(artifWorn, 17, 0, &bonuses); //give spellbook
  677. }
  678. VLC->arth->equipArtifact(artifWorn, 16, 3, &bonuses); //everyone has a catapult
  679. if(portrait < 0 || portrait == 255)
  680. portrait = subID;
  681. if(!hasBonus(Selector::sourceType(Bonus::HERO_BASE_SKILL)))
  682. {
  683. pushPrimSkill(PrimarySkill::ATTACK, type->heroClass->initialAttack);
  684. pushPrimSkill(PrimarySkill::DEFENSE, type->heroClass->initialDefence);
  685. pushPrimSkill(PrimarySkill::SPELL_POWER, type->heroClass->initialPower);
  686. pushPrimSkill(PrimarySkill::KNOWLEDGE, type->heroClass->initialKnowledge);
  687. }
  688. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  689. secSkills = type->secSkillsInit;
  690. if (!name.length())
  691. name = type->name;
  692. if (exp == 0xffffffff)
  693. {
  694. exp=40+ (ran()) % 50;
  695. level = 1;
  696. }
  697. else
  698. {
  699. level = VLC->heroh->level(exp);
  700. }
  701. setFormation(false);
  702. if (!stacksCount()) //standard army//initial army
  703. {
  704. int howManyStacks = 0; //how many stacks will hero receives <1 - 3>
  705. int pom = ran()%100;
  706. int warMachinesGiven = 0;
  707. if(pom < 9)
  708. howManyStacks = 1;
  709. else if(pom < 79)
  710. howManyStacks = 2;
  711. else
  712. howManyStacks = 3;
  713. for(int stackNo=0; stackNo<3; stackNo++)
  714. {
  715. int creID = (VLC->creh->nameToID[type->refTypeStack[stackNo]]);
  716. int range = type->highStack[stackNo] - type->lowStack[stackNo];
  717. int count = ran()%(range+1) + type->lowStack[stackNo];
  718. if(creID>=145 && creID<=149) //war machine
  719. {
  720. warMachinesGiven++;
  721. switch (creID)
  722. {
  723. case 145: //catapult
  724. VLC->arth->equipArtifact(artifWorn, 16, 3, &bonuses);
  725. break;
  726. default:
  727. VLC->arth->equipArtifact(
  728. artifWorn,
  729. 9+CArtHandler::convertMachineID(creID,true),
  730. CArtHandler::convertMachineID(creID,true),
  731. &bonuses);
  732. break;
  733. }
  734. }
  735. else
  736. addStack(stackNo-warMachinesGiven, CStackInstance(creID, count));
  737. }
  738. }
  739. assert(validTypes());
  740. hoverName = VLC->generaltexth->allTexts[15];
  741. boost::algorithm::replace_first(hoverName,"%s",name);
  742. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  743. recreateArtBonuses();
  744. if(mana < 0)
  745. mana = manaLimit(); //after all bonuses are taken into account
  746. }
  747. void CGHeroInstance::initHeroDefInfo()
  748. {
  749. if(!defInfo || defInfo->id != HEROI_TYPE)
  750. {
  751. defInfo = new CGDefInfo();
  752. defInfo->id = HEROI_TYPE;
  753. defInfo->subid = subID;
  754. defInfo->printPriority = 0;
  755. defInfo->visitDir = 0xff;
  756. }
  757. for(int i=0;i<6;i++)
  758. {
  759. defInfo->blockMap[i] = 255;
  760. defInfo->visitMap[i] = 0;
  761. defInfo->coverageMap[i] = 0;
  762. defInfo->shadowCoverage[i] = 0;
  763. }
  764. defInfo->handler=NULL;
  765. defInfo->blockMap[5] = 253;
  766. defInfo->visitMap[5] = 2;
  767. defInfo->coverageMap[4] = defInfo->coverageMap[5] = 224;
  768. }
  769. CGHeroInstance::~CGHeroInstance()
  770. {
  771. }
  772. bool CGHeroInstance::needsLastStack() const
  773. {
  774. return true;
  775. }
  776. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  777. {
  778. if(h == this) return; //exclude potential self-visiting
  779. if (ID == HEROI_TYPE) //hero
  780. {
  781. //TODO: check for allies
  782. if(tempOwner == h->tempOwner) //our hero
  783. {
  784. //exchange
  785. cb->heroExchange(id, h->id);
  786. }
  787. else //battle
  788. {
  789. if(visitedTown) //we're in town
  790. visitedTown->onHeroVisit(h); //town will handle attacking
  791. else
  792. cb->startBattleI(h, this);
  793. }
  794. }
  795. else if(ID == 62) //prison
  796. {
  797. InfoWindow iw;
  798. iw.player = h->tempOwner;
  799. iw.soundID = soundBase::ROGUE;
  800. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  801. {
  802. cb->changeObjPos(id,pos+int3(1,0,0),0);
  803. cb->setObjProperty(id, ObjProperty::ID, HEROI_TYPE); //set ID to 34
  804. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  805. iw.text << std::pair<ui8,ui32>(11,102);
  806. }
  807. else //already 8 wandering heroes
  808. {
  809. iw.text << std::pair<ui8,ui32>(11,103);
  810. }
  811. cb->showInfoDialog(&iw);
  812. }
  813. }
  814. const std::string & CGHeroInstance::getBiography() const
  815. {
  816. if (biography.length())
  817. return biography;
  818. else
  819. return VLC->generaltexth->hTxts[subID].biography;
  820. }
  821. void CGHeroInstance::initObj()
  822. {
  823. blockVisit = true;
  824. }
  825. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  826. {
  827. if(what == ObjProperty::PRIMARY_STACK_COUNT)
  828. setStackCount(0, val);
  829. }
  830. double CGHeroInstance::getHeroStrength() const
  831. {
  832. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  833. }
  834. int CGHeroInstance::getTotalStrength() const
  835. {
  836. double ret = getHeroStrength() * getArmyStrength();
  837. return (int) ret;
  838. }
  839. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  840. {
  841. ui8 skill = 0; //skill level
  842. if(spell->fire)
  843. skill = std::max(skill,getSecSkillLevel(14));
  844. if(spell->air)
  845. skill = std::max(skill,getSecSkillLevel(15));
  846. if(spell->water)
  847. skill = std::max(skill,getSecSkillLevel(16));
  848. if(spell->earth)
  849. skill = std::max(skill,getSecSkillLevel(17));
  850. //bonuses (eg. from special terrains)
  851. skill = std::max<ui8>(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  852. if(spell->fire)
  853. skill = std::max<ui8>(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 1));
  854. if(spell->air)
  855. skill = std::max<ui8>(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 2));
  856. if(spell->water)
  857. skill = std::max<ui8>(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 4));
  858. if(spell->earth)
  859. skill = std::max<ui8>(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 8));
  860. return skill;
  861. }
  862. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  863. {
  864. if(!getArt(17)) //if hero has no spellbook
  865. return false;
  866. if(vstd::contains(spells, spell->id) //hero has this spell in spellbook
  867. || (spell->air && hasBonusOfType(Bonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  868. || (spell->fire && hasBonusOfType(Bonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  869. || (spell->water && hasBonusOfType(Bonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  870. || (spell->earth && hasBonusOfType(Bonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  871. || hasBonusOfType(Bonus::SPELL, spell->id)
  872. || hasBonusOfType(Bonus::SPELLS_OF_LEVEL, spell->level)
  873. )
  874. return true;
  875. return false;
  876. }
  877. /**
  878. * Calculates what creatures and how many to be raised from a battle.
  879. * @param battleResult The results of the battle.
  880. * @return Returns a pair with the first value indicating the ID of the creature
  881. * type and second value the amount. Both values are returned as -1 if necromancy
  882. * could not be applied.
  883. */
  884. CStackInstance CGHeroInstance::calculateNecromancy (const BattleResult &battleResult) const
  885. {
  886. const ui8 necromancyLevel = getSecSkillLevel(12);
  887. // Hero knows necromancy.
  888. if (necromancyLevel > 0)
  889. {
  890. double necromancySkill = necromancyLevel*0.1
  891. + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 12)/100.0;
  892. amin(necromancySkill, 1.0); //it's impossible to raise more creatures than all...
  893. const std::map<ui32,si32> &casualties = battleResult.casualties[!battleResult.winner];
  894. ui32 raisedUnits = 0;
  895. // Figure out what to raise and how many.
  896. const ui32 creatureTypes[] = {56, 58, 60, 64}; // IDs for Skeletons, Walking Dead, Wights and Liches respectively.
  897. const bool improvedNecromancy = hasBonusOfType(Bonus::IMPROVED_NECROMANCY);
  898. const CCreature *raisedUnitType = VLC->creh->creatures[creatureTypes[improvedNecromancy ? necromancyLevel : 0]];
  899. const ui32 raisedUnitHP = raisedUnitType->valOfBonuses(Bonus::STACK_HEALTH);
  900. //calculate creatures raised from each defeated stack
  901. for (std::map<ui32,si32>::const_iterator it = casualties.begin(); it != casualties.end(); it++)
  902. {
  903. // Get lost enemy hit points convertible to units.
  904. const ui32 raisedHP = VLC->creh->creatures[it->first]->valOfBonuses(Bonus::STACK_HEALTH) * it->second * necromancySkill;
  905. raisedUnits += std::min<ui32>(raisedHP / raisedUnitHP, it->second * necromancySkill); //limit to % of HP and % of original stack count
  906. }
  907. // Make room for new units.
  908. int slot = getSlotFor(raisedUnitType->idNumber);
  909. if (slot == -1)
  910. {
  911. // If there's no room for unit, try it's upgraded version 2/3rds the size.
  912. raisedUnitType = VLC->creh->creatures[*raisedUnitType->upgrades.begin()];
  913. raisedUnits = (raisedUnits*2)/3;
  914. slot = getSlotFor(raisedUnitType->idNumber);
  915. }
  916. if (raisedUnits <= 0)
  917. raisedUnits = 1;
  918. return CStackInstance(raisedUnitType->idNumber, raisedUnits);
  919. }
  920. return CStackInstance();
  921. }
  922. /**
  923. * Show the necromancy dialog with information about units raised.
  924. * @param raisedStack Pair where the first element represents ID of the raised creature
  925. * and the second element the amount.
  926. */
  927. void CGHeroInstance::showNecromancyDialog(const CStackInstance &raisedStack) const
  928. {
  929. InfoWindow iw;
  930. iw.soundID = soundBase::GENIE;
  931. iw.player = tempOwner;
  932. iw.components.push_back(Component(raisedStack));
  933. if (raisedStack.count > 1) // Practicing the dark arts of necromancy, ... (plural)
  934. {
  935. iw.text.addTxt(MetaString::GENERAL_TXT, 145);
  936. iw.text.addReplacement(raisedStack.count);
  937. iw.text.addReplacement(MetaString::CRE_PL_NAMES, raisedStack.type->idNumber);
  938. }
  939. else // Practicing the dark arts of necromancy, ... (singular)
  940. {
  941. iw.text.addTxt(MetaString::GENERAL_TXT, 146);
  942. iw.text.addReplacement(MetaString::CRE_SING_NAMES, raisedStack.type->idNumber);
  943. }
  944. cb->showInfoDialog(&iw);
  945. }
  946. int3 CGHeroInstance::getSightCenter() const
  947. {
  948. return getPosition(false);
  949. }
  950. int CGHeroInstance::getSightRadious() const
  951. {
  952. return 5 + getSecSkillLevel(3) + valOfBonuses(Bonus::SIGHT_RADIOUS); //default + scouting
  953. }
  954. si32 CGHeroInstance::manaRegain() const
  955. {
  956. if (hasBonusOfType(Bonus::FULL_MANA_REGENERATION))
  957. return manaLimit();
  958. return 1 + getSecSkillLevel(8) + valOfBonuses(Bonus::MANA_REGENERATION); //1 + Mysticism level
  959. }
  960. si32 CGHeroInstance::getArtPos(int aid) const
  961. {
  962. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  963. if(i->second == aid)
  964. return i->first;
  965. return -1;
  966. }
  967. /**
  968. * Places an artifact in hero's backpack. If it's a big artifact equips it
  969. * or discards it if it cannot be equipped.
  970. */
  971. void CGHeroInstance::giveArtifact (ui32 aid)
  972. {
  973. const CArtifact &artifact = VLC->arth->artifacts[aid];
  974. if (artifact.isBig()) {
  975. for (std::vector<ui16>::const_iterator it = artifact.possibleSlots.begin(); it != artifact.possibleSlots.end(); ++it) {
  976. if (!vstd::contains(artifWorn, *it)) {
  977. VLC->arth->equipArtifact(artifWorn, *it, aid, &bonuses);
  978. break;
  979. }
  980. }
  981. } else {
  982. artifacts.push_back(aid);
  983. }
  984. }
  985. void CGHeroInstance::recreateArtBonuses()
  986. {
  987. //clear all bonuses from artifacts (if present) and give them again
  988. bonuses.remove_if(boost::bind(Bonus::IsFrom,_1,Bonus::ARTIFACT,0xffffff));
  989. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  990. {
  991. CArtifact &art = VLC->arth->artifacts[ari->second];
  992. art.addBonusesTo(&bonuses);
  993. }
  994. }
  995. bool CGHeroInstance::hasArt( ui32 aid ) const
  996. {
  997. if(vstd::contains(artifacts, aid))
  998. return true;
  999. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  1000. if(i->second == aid)
  1001. return true;
  1002. return false;
  1003. }
  1004. int CGHeroInstance::getBoatType() const
  1005. {
  1006. int alignment = type->heroType / 6;
  1007. switch(alignment)
  1008. {
  1009. case 0:
  1010. return 1; //good
  1011. case 1:
  1012. return 0; //evil
  1013. case 2:
  1014. return 2;
  1015. default:
  1016. throw std::string("Wrong alignment!");
  1017. }
  1018. }
  1019. void CGHeroInstance::getOutOffsets(std::vector<int3> &offsets) const
  1020. {
  1021. static int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0), int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  1022. for (size_t i = 0; i < ARRAY_COUNT(dirs); i++)
  1023. offsets += dirs[i];
  1024. }
  1025. int CGHeroInstance::getSpellCost(const CSpell *sp) const
  1026. {
  1027. return sp->costs[getSpellSchoolLevel(sp)];
  1028. }
  1029. void CGHeroInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  1030. {
  1031. CArmedInstance::getParents(out, root);// if(visitedTown && source != this && source != visitedTown)
  1032. // out.insert(visitedTown);
  1033. if((root == this || contains(static_cast<const CStackInstance *>(root))) && visitedTown)
  1034. out.insert(visitedTown);
  1035. }
  1036. void CGHeroInstance::pushPrimSkill(int which, int val)
  1037. {
  1038. bonuses.push_back(Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::HERO_BASE_SKILL, val, id, which));
  1039. }
  1040. void CGHeroInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  1041. {
  1042. #define FOREACH_OWNER_TOWN(town) if(const PlayerState *p = cb->getPlayerState(tempOwner)) BOOST_FOREACH(const CGTownInstance *town, p->towns)
  1043. CArmedInstance::getBonuses(out, selector, root);
  1044. //TODO eliminate by moving secondary skills effects to bonus system
  1045. if(Selector::matchesType(selector, Bonus::LUCK))
  1046. {
  1047. //luck skill
  1048. if(int luckSkill = getSecSkillLevel(9))
  1049. out.push_back(Bonus(Bonus::PERMANENT, Bonus::LUCK, Bonus::SECONDARY_SKILL, luckSkill, 9, VLC->generaltexth->arraytxt[73+luckSkill]));
  1050. //guardian spirit
  1051. FOREACH_OWNER_TOWN(t)
  1052. if(t->subID ==1 && vstd::contains(t->builtBuildings,26)) //rampart with grail
  1053. out.push_back(Bonus(Bonus::PERMANENT, Bonus::LUCK, Bonus::TOWN_STRUCTURE, +2, 26, VLC->generaltexth->buildings[1][26].first + " +2"));
  1054. }
  1055. if(Selector::matchesType(selector, Bonus::SEA_MOVEMENT))
  1056. {
  1057. //lighthouses
  1058. FOREACH_OWNER_TOWN(t)
  1059. if(t->subID == 0 && vstd::contains(t->builtBuildings,17)) //castle
  1060. out.push_back(Bonus(Bonus::PERMANENT, Bonus::SEA_MOVEMENT, Bonus::TOWN_STRUCTURE, +500, 17, VLC->generaltexth->buildings[0][17].first + " +500"));
  1061. }
  1062. if(Selector::matchesType(selector, Bonus::MORALE))
  1063. {
  1064. //leadership
  1065. if(int moraleSkill = getSecSkillLevel(6))
  1066. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::SECONDARY_SKILL, moraleSkill, 6, VLC->generaltexth->arraytxt[104+moraleSkill]));
  1067. //colossus
  1068. FOREACH_OWNER_TOWN(t)
  1069. if(t->subID == 0 && vstd::contains(t->builtBuildings,26)) //castle
  1070. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::TOWN_STRUCTURE, +2, 26, VLC->generaltexth->buildings[0][26].first + " +2"));
  1071. }
  1072. if(Selector::matchesTypeSubtype(selector, Bonus::SECONDARY_SKILL_PREMY, 12)) //necromancy
  1073. {
  1074. FOREACH_OWNER_TOWN(t)
  1075. {
  1076. if(t->subID == 4) //necropolis
  1077. {
  1078. if(vstd::contains(t->builtBuildings,21)) //necromancy amplifier
  1079. out.push_back(Bonus(Bonus::PERMANENT, Bonus::SECONDARY_SKILL_PREMY, Bonus::TOWN_STRUCTURE, +10, 21, VLC->generaltexth->buildings[4][21].first + " +10%", 12));
  1080. if(vstd::contains(t->builtBuildings,26)) //grail - Soul prison
  1081. out.push_back(Bonus(Bonus::PERMANENT, Bonus::SECONDARY_SKILL_PREMY, Bonus::TOWN_STRUCTURE, +20, 26, VLC->generaltexth->buildings[4][26].first + " +20%", 12));
  1082. }
  1083. }
  1084. }
  1085. }
  1086. void CGDwelling::initObj()
  1087. {
  1088. switch(ID)
  1089. {
  1090. case 17:
  1091. {
  1092. int crid = VLC->objh->cregens[subID];
  1093. const CCreature *crs = VLC->creh->creatures[crid];
  1094. creatures.resize(1);
  1095. creatures[0].second.push_back(crid);
  1096. hoverName = VLC->generaltexth->creGens[subID];
  1097. if(crs->level > 4)
  1098. addStack(0, CStackInstance(crs, (8 - crs->level) * 3));
  1099. if (getOwner() != 255)
  1100. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  1101. }
  1102. break;
  1103. case 20:
  1104. creatures.resize(4);
  1105. if(subID == 1) //Golem Factory
  1106. {
  1107. creatures[0].second.push_back(32); //Stone Golem
  1108. creatures[1].second.push_back(33); //Iron Golem
  1109. creatures[2].second.push_back(116); //Gold Golem
  1110. creatures[3].second.push_back(117); //Diamond Golem
  1111. //guards
  1112. addStack(0, CStackInstance(116, 9));
  1113. addStack(1, CStackInstance(117, 6));
  1114. }
  1115. else if(subID == 0) // Elemental Conflux
  1116. {
  1117. creatures[0].second.push_back(112); //Air Elemental
  1118. creatures[1].second.push_back(114); //Fire Elemental
  1119. creatures[2].second.push_back(113); //Earth Elemental
  1120. creatures[3].second.push_back(115); //Water Elemental
  1121. //guards
  1122. addStack(0, CStackInstance(113, 12));
  1123. }
  1124. else
  1125. {
  1126. assert(0);
  1127. }
  1128. hoverName = VLC->generaltexth->creGens4[subID];
  1129. break;
  1130. case 106: //War Machine Factory
  1131. creatures.resize(3);
  1132. creatures[0].second.push_back(146); //Ballista
  1133. creatures[1].second.push_back(147); //First Aid Tent
  1134. creatures[2].second.push_back(148); //Ammo Cart
  1135. break;
  1136. default:
  1137. assert(0);
  1138. break;
  1139. }
  1140. }
  1141. void CGDwelling::setProperty(ui8 what, ui32 val)
  1142. {
  1143. switch (what)
  1144. {
  1145. case ObjProperty::OWNER: //change owner
  1146. if (ID == 17) //single generators
  1147. {
  1148. if (tempOwner != NEUTRAL_PLAYER)
  1149. {
  1150. std::vector<CGDwelling *>* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  1151. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  1152. }
  1153. if (val != NEUTRAL_PLAYER) //can new owner be neutral?
  1154. cb->gameState()->players[val].dwellings.push_back (this);
  1155. }
  1156. break;
  1157. }
  1158. CGObjectInstance::setProperty(what,val);
  1159. }
  1160. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1161. {
  1162. if(h->tempOwner != tempOwner && stacksCount() > 0) //object is guarded
  1163. {
  1164. BlockingDialog bd;
  1165. bd.player = h->tempOwner;
  1166. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1167. bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards?
  1168. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1169. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + Slots().begin()->second.getQuantityID()*3);
  1170. bd.text.addReplacement(Slots().begin()->second);
  1171. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1172. return;
  1173. }
  1174. if(h->tempOwner != tempOwner && ID != 106)
  1175. {
  1176. cb->setOwner(id, h->tempOwner);
  1177. }
  1178. BlockingDialog bd;
  1179. bd.player = h->tempOwner;
  1180. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1181. if(ID == 17 || ID == 20)
  1182. {
  1183. bd.text.addTxt(MetaString::ADVOB_TXT, ID == 17 ? 35 : 36); //{%s} Would you like to recruit %s? / {%s} Would you like to recruit %s, %s, %s, or %s?
  1184. bd.text.addReplacement(ID == 17 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1185. for(size_t i = 0; i < creatures.size(); i++)
  1186. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1187. }
  1188. else if(ID == 106)
  1189. bd.text.addTxt(MetaString::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  1190. else
  1191. throw std::string("Illegal dwelling!");
  1192. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1193. }
  1194. void CGDwelling::newTurn() const
  1195. {
  1196. if(cb->getDate(1) != 1) //not first day of week
  1197. return;
  1198. //town growths and War Machines Factories are handled separately
  1199. if(ID == TOWNI_TYPE || ID == 106)
  1200. return;
  1201. bool change = false;
  1202. SetAvailableCreatures sac;
  1203. sac.creatures = creatures;
  1204. sac.tid = id;
  1205. for (size_t i = 0; i < creatures.size(); i++)
  1206. {
  1207. if(creatures[i].second.size())
  1208. {
  1209. if(false /*accumulate creatures*/)
  1210. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]]->growth;
  1211. else
  1212. sac.creatures[i].first = VLC->creh->creatures[creatures[i].second[0]]->growth;
  1213. change = true;
  1214. }
  1215. }
  1216. if(change)
  1217. cb->sendAndApply(&sac);
  1218. }
  1219. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1220. {
  1221. if(!answer)
  1222. return;
  1223. int crid = creatures[0].second[0];
  1224. CCreature *crs = VLC->creh->creatures[crid];
  1225. if(crs->level == 1) //first level - creatures are for free
  1226. {
  1227. if(creatures[0].first) //there are available creatures
  1228. {
  1229. int slot = h->getSlotFor(crid);
  1230. if(slot < 0) //no available slot
  1231. {
  1232. InfoWindow iw;
  1233. iw.player = h->tempOwner;
  1234. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1235. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1236. cb->showInfoDialog(&iw);
  1237. }
  1238. else //give creatures
  1239. {
  1240. SetAvailableCreatures sac;
  1241. sac.tid = id;
  1242. sac.creatures = creatures;
  1243. sac.creatures[0].first = 0;
  1244. SetGarrisons sg;
  1245. sg.garrs[h->id] = *h;
  1246. sg.garrs[h->id].addToSlot(slot, crid, creatures[0].first);
  1247. InfoWindow iw;
  1248. iw.player = h->tempOwner;
  1249. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1250. iw.text.addReplacement(creatures[0].first);
  1251. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1252. cb->showInfoDialog(&iw);
  1253. cb->sendAndApply(&sac);
  1254. cb->sendAndApply(&sg);
  1255. }
  1256. }
  1257. else //there no creatures
  1258. {
  1259. InfoWindow iw;
  1260. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1261. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1262. iw.player = h->tempOwner;
  1263. cb->sendAndApply(&iw);
  1264. }
  1265. }
  1266. else
  1267. {
  1268. if(ID == 106) //pick available War Machines
  1269. {
  1270. //there is 1 war machine available to recruit if hero doesn't have one
  1271. SetAvailableCreatures sac;
  1272. sac.tid = id;
  1273. sac.creatures = creatures;
  1274. sac.creatures[0].first = !h->getArt(13); //ballista
  1275. sac.creatures[1].first = !h->getArt(15); //first aid tent
  1276. sac.creatures[2].first = !h->getArt(14); //ammo cart
  1277. cb->sendAndApply(&sac);
  1278. }
  1279. OpenWindow ow;
  1280. ow.id1 = id;
  1281. ow.id2 = h->id;
  1282. ow.window = ID == 17
  1283. ? OpenWindow::RECRUITMENT_FIRST
  1284. : OpenWindow::RECRUITMENT_ALL;
  1285. cb->sendAndApply(&ow);
  1286. }
  1287. }
  1288. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1289. {
  1290. if(answer)
  1291. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1292. }
  1293. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1294. {
  1295. if (result->winner == 0)
  1296. {
  1297. onHeroVisit(h);
  1298. }
  1299. }
  1300. int CGTownInstance::getSightRadious() const //returns sight distance
  1301. {
  1302. if (subID == 2) //tower
  1303. {
  1304. if ((builtBuildings.find(26)) != builtBuildings.end()) //skyship
  1305. return -1; //entire map
  1306. else if ((builtBuildings.find(21)) != builtBuildings.end()) //lookout tower
  1307. return 20;
  1308. }
  1309. return 5;
  1310. }
  1311. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  1312. {
  1313. ///this is freakin' overcomplicated solution
  1314. switch (what)
  1315. {
  1316. case 11: //add visitor of town building
  1317. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id);
  1318. break;
  1319. case 12:
  1320. bonusingBuildings[val]->setProperty (12, 0);
  1321. break;
  1322. case 13: //add garrisoned hero to visitors
  1323. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, garrisonHero->id);
  1324. break;
  1325. case 14:
  1326. bonusValue.first = val;
  1327. break;
  1328. case 15:
  1329. bonusValue.second = val;
  1330. break;
  1331. }
  1332. }
  1333. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1334. {
  1335. if((builtBuildings.find(9))!=builtBuildings.end())
  1336. return 3;
  1337. if((builtBuildings.find(8))!=builtBuildings.end())
  1338. return 2;
  1339. if((builtBuildings.find(7))!=builtBuildings.end())
  1340. return 1;
  1341. return 0;
  1342. }
  1343. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1344. {
  1345. if ((builtBuildings.find(13))!=builtBuildings.end())
  1346. return 3;
  1347. if ((builtBuildings.find(12))!=builtBuildings.end())
  1348. return 2;
  1349. if ((builtBuildings.find(11))!=builtBuildings.end())
  1350. return 1;
  1351. if ((builtBuildings.find(10))!=builtBuildings.end())
  1352. return 0;
  1353. return -1;
  1354. }
  1355. int CGTownInstance::mageGuildLevel() const
  1356. {
  1357. if ((builtBuildings.find(4))!=builtBuildings.end())
  1358. return 5;
  1359. if ((builtBuildings.find(3))!=builtBuildings.end())
  1360. return 4;
  1361. if ((builtBuildings.find(2))!=builtBuildings.end())
  1362. return 3;
  1363. if ((builtBuildings.find(1))!=builtBuildings.end())
  1364. return 2;
  1365. if ((builtBuildings.find(0))!=builtBuildings.end())
  1366. return 1;
  1367. return 0;
  1368. }
  1369. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1370. {
  1371. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1372. }
  1373. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1374. {
  1375. return town->hordeLvl[HID];
  1376. }
  1377. int CGTownInstance::creatureGrowth(const int & level) const
  1378. {
  1379. int ret = VLC->creh->creatures[town->basicCreatures[level]]->growth;
  1380. switch(fortLevel())
  1381. {
  1382. case 3:
  1383. ret*=2;break;
  1384. case 2:
  1385. ret*=(1.5); break;
  1386. }
  1387. if(getHordeLevel(0)==level)
  1388. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1389. ret+=VLC->creh->creatures[town->basicCreatures[level]]->hordeGrowth;
  1390. if(getHordeLevel(1)==level)
  1391. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1392. ret+=VLC->creh->creatures[town->basicCreatures[level]]->hordeGrowth;
  1393. //support for legs of legion etc.
  1394. if(garrisonHero)
  1395. ret += garrisonHero->valOfBonuses(Bonus::CREATURE_GROWTH, level);
  1396. if(visitingHero)
  1397. ret += visitingHero->valOfBonuses(Bonus::CREATURE_GROWTH, level);
  1398. for (std::vector<CGDwelling*>::const_iterator it = cb->gameState()->players[tempOwner].dwellings.begin(); it != cb->gameState()->players[tempOwner].dwellings.end(); ++it)
  1399. { //+1 for each dwelling
  1400. if (VLC->creh->creatures[town->basicCreatures[level]]->idNumber == (*it)->creatures[0].second[0])
  1401. ++ret;
  1402. }
  1403. if(builtBuildings.find(26)!=builtBuildings.end()) //grail - +50% to ALL growth
  1404. ret*=1.5;
  1405. return ret;//check CCastleInterface.cpp->CCastleInterface::CCreaInfo::clickRight if this one will be modified
  1406. }
  1407. int CGTownInstance::dailyIncome() const
  1408. {
  1409. int ret = 0;
  1410. if ((builtBuildings.find(26))!=builtBuildings.end())
  1411. ret+=5000;
  1412. if ((builtBuildings.find(13))!=builtBuildings.end())
  1413. ret+=4000;
  1414. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1415. ret+=2000;
  1416. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1417. ret+=1000;
  1418. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1419. ret+=500;
  1420. return ret;
  1421. }
  1422. bool CGTownInstance::hasFort() const
  1423. {
  1424. return (builtBuildings.find(7))!=builtBuildings.end();
  1425. }
  1426. bool CGTownInstance::hasCapitol() const
  1427. {
  1428. return (builtBuildings.find(13))!=builtBuildings.end();
  1429. }
  1430. CGTownInstance::CGTownInstance()
  1431. :IShipyard(this), IMarket(this)
  1432. {
  1433. builded=-1;
  1434. destroyed=-1;
  1435. garrisonHero=NULL;
  1436. town=NULL;
  1437. visitingHero = NULL;
  1438. }
  1439. CGTownInstance::~CGTownInstance()
  1440. {
  1441. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1442. delete *i;
  1443. }
  1444. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1445. {
  1446. if(checkGuild && mageGuildLevel() < level)
  1447. return 0;
  1448. int ret = 6 - level; //how many spells are available at this level
  1449. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1450. ret++;
  1451. return ret;
  1452. }
  1453. int CGTownInstance::defenceBonus(int type) const
  1454. {
  1455. int ret=0;
  1456. switch (type)
  1457. {
  1458. /*attack*/ case 0:
  1459. if (subID == 6 && vstd::contains(builtBuildings,26))//Stronghold, grail
  1460. ret += 12;
  1461. if (subID == 7 && vstd::contains(builtBuildings,26))//Fortress, grail
  1462. ret += 10;
  1463. if (subID == 7 && vstd::contains(builtBuildings,22))//Fortress, Blood Obelisk
  1464. ret += 2;
  1465. return ret;
  1466. /*defence*/ case 1:
  1467. if (subID == 7 && vstd::contains(builtBuildings,21))//Fortress, Glyphs of Fear
  1468. ret += 2;
  1469. if (subID == 7 && vstd::contains(builtBuildings,26))//Fortress, Grail
  1470. ret += 10;
  1471. return ret;
  1472. /*spellpower*/ case 2:
  1473. if (subID == 3 && vstd::contains(builtBuildings,21))//Inferno, Brimstone Clouds
  1474. ret += 2;
  1475. if (subID == 5 && vstd::contains(builtBuildings,26))//Dungeon, Grail
  1476. ret += 12;
  1477. return ret;
  1478. /*knowledge*/ case 3:
  1479. if (subID == 2 && vstd::contains(builtBuildings,26))//Tower, Grail
  1480. ret += 15;
  1481. return ret;
  1482. }
  1483. return 0;//Why we are here? wrong type?
  1484. }
  1485. bool CGTownInstance::needsLastStack() const
  1486. {
  1487. if(garrisonHero)
  1488. return true;
  1489. else return false;
  1490. }
  1491. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1492. {
  1493. if(getOwner() != h->getOwner())
  1494. {
  1495. //TODO ally check
  1496. if(stacksCount() > 0 || visitingHero)
  1497. {
  1498. const CGHeroInstance *defendingHero = NULL;
  1499. if(visitingHero)
  1500. defendingHero = visitingHero;
  1501. else if(garrisonHero)
  1502. defendingHero = garrisonHero;
  1503. const CArmedInstance *defendingArmy = this;
  1504. if(defendingHero)
  1505. defendingArmy = defendingHero;
  1506. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1507. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1508. }
  1509. else
  1510. {
  1511. cb->setOwner(id, h->tempOwner);
  1512. removeCapitols (h->getOwner());
  1513. cb->heroVisitCastle(id, h->id);
  1514. }
  1515. }
  1516. else
  1517. cb->heroVisitCastle(id, h->id);
  1518. }
  1519. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1520. {
  1521. cb->stopHeroVisitCastle(id,h->id);
  1522. }
  1523. void CGTownInstance::initObj()
  1524. ///initialize town structures
  1525. {
  1526. blockVisit = true;
  1527. hoverName = name + ", " + town->Name();
  1528. creatures.resize(CREATURES_PER_TOWN);
  1529. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1530. {
  1531. if(creatureDwelling(i,false))
  1532. creatures[i].second.push_back(town->basicCreatures[i]);
  1533. if(creatureDwelling(i,true))
  1534. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1535. }
  1536. switch (subID)
  1537. { //add new visitable objects
  1538. case 0:
  1539. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1540. break;
  1541. case 2: case 3: case 5: case 6:
  1542. bonusingBuildings.push_back (new CTownBonus(23, this));
  1543. if (subID == 5)
  1544. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1545. break;
  1546. case 7:
  1547. bonusingBuildings.push_back (new CTownBonus(17, this));
  1548. break;
  1549. case 4:
  1550. if (vstd::contains(builtBuildings, 17))
  1551. {
  1552. GiveBonus gb(GiveBonus::TOWN); //magic
  1553. gb.bonus.type = Bonus::DARKNESS;
  1554. gb.bonus.val = 20;
  1555. gb.id = id;
  1556. gb.bonus.duration = Bonus::PERMANENT;
  1557. gb.bonus.source = Bonus::TOWN_STRUCTURE;
  1558. gb.bonus.id = id;
  1559. cb->sendAndApply(&gb);
  1560. }
  1561. break;
  1562. }
  1563. }
  1564. void CGTownInstance::newTurn() const
  1565. {
  1566. if (cb->getDate(1) == 1) //reset on new week
  1567. {
  1568. if (vstd::contains(builtBuildings,17) && subID == 1 && cb->getDate(0) != 1 && (tempOwner < PLAYER_LIMIT) )//give resources for Rampart, Mystic Pond
  1569. {
  1570. int resID = rand()%4+2;//bonus to random rare resource
  1571. resID = (resID==2)?1:resID;
  1572. int resVal = rand()%4+1;//with size 1..4
  1573. cb->giveResource(tempOwner, resID, resVal);
  1574. cb->setObjProperty (id, 14, resID);
  1575. cb->setObjProperty (id, 15, resVal);
  1576. }
  1577. if ( subID == 5 )
  1578. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1579. {
  1580. if ((*i)->ID == 21)
  1581. cb->setObjProperty (id, 12, (*i)->id); //reset visitors for Mana Vortex
  1582. }
  1583. }
  1584. }
  1585. int3 CGTownInstance::getSightCenter() const
  1586. {
  1587. return pos - int3(2,0,0);
  1588. }
  1589. ui8 CGTownInstance::getPassableness() const
  1590. {
  1591. return stacksCount() ? 1<<tempOwner : ALL_PLAYERS; //if there is garrison army, castle be entered only by owner //TODO: allies
  1592. }
  1593. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1594. {
  1595. offsets += int3(-1,2,0), int3(-3,2,0);
  1596. }
  1597. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1598. {
  1599. if(result->winner == 0)
  1600. {
  1601. if (hasBonusOfType(Bonus::DARKNESS))
  1602. {
  1603. //TODO: Make some 'change owner' function for bonus, or bonuses independent of player
  1604. /*
  1605. RemoveBonus rb(RemoveBonus::PLAYER);
  1606. rb.whoID = getOwner();
  1607. rb.source = Bonus::TOWN_STRUCTURE;
  1608. rb.id = id;
  1609. cb->sendAndApply(&rb);
  1610. GiveBonus gb(GiveBonus::PLAYER);
  1611. gb.bonus.type = Bonus::DARKNESS;
  1612. gb.bonus.val = 20;
  1613. gb.id = h->tempOwner;
  1614. gb.bonus.duration = Bonus::PERMANENT;
  1615. gb.bonus.source = Bonus::TOWN_STRUCTURE;
  1616. gb.bonus.id = id;
  1617. cb->sendAndApply(&gb);
  1618. */
  1619. }
  1620. removeCapitols (h->getOwner());
  1621. cb->setOwner (id, h->tempOwner); //give control after checkout is done
  1622. FoWChange fw;
  1623. fw.player = h->tempOwner;
  1624. fw.mode = 1;
  1625. getSightTiles (fw.tiles); //update visibility for castle structures
  1626. cb->sendAndApply (&fw);
  1627. }
  1628. }
  1629. void CGTownInstance::removeCapitols (ui8 owner) const
  1630. {
  1631. if (hasCapitol()) // search if there's an older capitol
  1632. {
  1633. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  1634. for (std::vector<CGTownInstance*>::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
  1635. {
  1636. if (*i != this && (*i)->hasCapitol())
  1637. {
  1638. RazeStructures rs;
  1639. rs.tid = id;
  1640. rs.bid.insert(13);
  1641. rs.destroyed = destroyed;
  1642. cb->sendAndApply(&rs);
  1643. return;
  1644. }
  1645. }
  1646. }
  1647. }
  1648. int CGTownInstance::getBoatType() const
  1649. {
  1650. const CCreature *c = VLC->creh->creatures[town->basicCreatures.front()];
  1651. if (c->isGood())
  1652. return 1;
  1653. else if (c->isEvil())
  1654. return 0;
  1655. else //neutral
  1656. return 2;
  1657. }
  1658. void CGTownInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  1659. {
  1660. CArmedInstance::getParents(out, root);
  1661. if(root == this && visitingHero && visitingHero != root)
  1662. out.insert(visitingHero);
  1663. }
  1664. void CGTownInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  1665. {
  1666. CArmedInstance::getBonuses(out, selector, root);
  1667. //TODO eliminate by moving structures effects to bonus system
  1668. if(Selector::matchesType(selector, Bonus::LUCK))
  1669. {
  1670. if(subID == 1 && vstd::contains(builtBuildings,21)) //rampart, fountain of fortune
  1671. out.push_back(Bonus(Bonus::PERMANENT, Bonus::LUCK, Bonus::TOWN_STRUCTURE, +2, 21, VLC->generaltexth->buildings[1][21].first + " +2"));
  1672. }
  1673. if(Selector::matchesType(selector, Bonus::MORALE))
  1674. {
  1675. if(subID == 0 && vstd::contains(builtBuildings,22)) //castle, brotherhood of sword built
  1676. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::TOWN_STRUCTURE, +2, 22, VLC->generaltexth->buildings[0][22].first + " +2"));
  1677. else if(vstd::contains(builtBuildings,5)) //tavern is built
  1678. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::TOWN_STRUCTURE, +1, 5, VLC->generaltexth->buildings[0][5].first + " +1"));
  1679. }
  1680. }
  1681. int CGTownInstance::getMarketEfficiency() const
  1682. {
  1683. if(!vstd::contains(builtBuildings, 14))
  1684. return 0;
  1685. const PlayerState *p = cb->getPlayerState(tempOwner);
  1686. assert(p);
  1687. int marketCount = 0;
  1688. BOOST_FOREACH(const CGTownInstance *t, p->towns)
  1689. if(vstd::contains(t->builtBuildings, 14))
  1690. marketCount++;
  1691. return marketCount;
  1692. }
  1693. bool CGTownInstance::allowsTrade(EMarketMode mode) const
  1694. {
  1695. switch(mode)
  1696. {
  1697. case RESOURCE_RESOURCE:
  1698. case RESOURCE_PLAYER:
  1699. return vstd::contains(builtBuildings, 14); // marketplace
  1700. case ARTIFACT_RESOURCE:
  1701. return (subID == 2 || subID == 5 || subID == 8) && vstd::contains(builtBuildings, 17);//artifact merchants
  1702. case CREATURE_RESOURCE:
  1703. return subID == 6 && vstd::contains(builtBuildings, 21); //Freelancer's guild
  1704. default:
  1705. assert(0);
  1706. return false;
  1707. }
  1708. }
  1709. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1710. {
  1711. if(visitors.find(h->id)==visitors.end())
  1712. {
  1713. onNAHeroVisit(h->id, false);
  1714. switch(ID)
  1715. {
  1716. case 102: //tree
  1717. case 4: //arena
  1718. case 41://library
  1719. case 47: //School of Magic
  1720. case 107://School of War
  1721. break;
  1722. default:
  1723. cb->setObjProperty(id, ObjProperty::VISITORS, h->id); //add to the visitors
  1724. break;
  1725. }
  1726. }
  1727. else
  1728. {
  1729. onNAHeroVisit(h->id, true);
  1730. }
  1731. }
  1732. void CGVisitableOPH::initObj()
  1733. {
  1734. if(ID==102)
  1735. ttype = ran()%3;
  1736. else
  1737. ttype = -1;
  1738. }
  1739. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, ui64 expVal, ui32 result ) const
  1740. {
  1741. if(result) //player agreed to give res for exp
  1742. {
  1743. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1744. cb->changePrimSkill(heroID,4,expVal); //give exp
  1745. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  1746. }
  1747. }
  1748. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1749. {
  1750. int id=0, subid=0, ot=0, val=1, sound = 0;
  1751. switch(ID)
  1752. {
  1753. case 4: //arena
  1754. sound = soundBase::NOMAD;
  1755. ot = 0;
  1756. break;
  1757. case 51: //mercenary camp
  1758. sound = soundBase::NOMAD;
  1759. subid=0;
  1760. ot=80;
  1761. break;
  1762. case 23: //marletto tower
  1763. sound = soundBase::NOMAD;
  1764. subid=1;
  1765. ot=39;
  1766. break;
  1767. case 61:
  1768. sound = soundBase::gazebo;
  1769. subid=2;
  1770. ot=100;
  1771. break;
  1772. case 32:
  1773. sound = soundBase::GETPROTECTION;
  1774. subid=3;
  1775. ot=59;
  1776. break;
  1777. case 100:
  1778. sound = soundBase::gazebo;
  1779. id=5;
  1780. ot=143;
  1781. val=1000;
  1782. break;
  1783. case 102:
  1784. sound = soundBase::gazebo;
  1785. id = 5;
  1786. subid = 1;
  1787. ot = 146;
  1788. val = 1;
  1789. break;
  1790. case 41:
  1791. sound = soundBase::gazebo;
  1792. ot = 66;
  1793. break;
  1794. case 47: //School of Magic
  1795. sound = soundBase::faerie;
  1796. ot = 71;
  1797. break;
  1798. case 107://School of War
  1799. sound = soundBase::MILITARY;
  1800. ot = 158;
  1801. break;
  1802. }
  1803. if (!alreadyVisited)
  1804. {
  1805. switch (ID)
  1806. {
  1807. case 4: //arena
  1808. {
  1809. BlockingDialog sd(false,true);
  1810. sd.soundID = sound;
  1811. sd.text << std::pair<ui8,ui32>(11,ot);
  1812. sd.components.push_back(Component(0,0,2,0));
  1813. sd.components.push_back(Component(0,1,2,0));
  1814. sd.player = cb->getOwner(heroID);
  1815. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1816. return;
  1817. }
  1818. case 51:
  1819. case 23:
  1820. case 61:
  1821. case 32:
  1822. {
  1823. cb->changePrimSkill(heroID,subid,val);
  1824. InfoWindow iw;
  1825. iw.soundID = sound;
  1826. iw.components.push_back(Component(0,subid,val,0));
  1827. iw.text << std::pair<ui8,ui32>(11,ot);
  1828. iw.player = cb->getOwner(heroID);
  1829. cb->showInfoDialog(&iw);
  1830. break;
  1831. }
  1832. case 100: //give exp
  1833. {
  1834. InfoWindow iw;
  1835. iw.soundID = sound;
  1836. iw.components.push_back(Component(id,subid,val,0));
  1837. iw.player = cb->getOwner(heroID);
  1838. iw.text << std::pair<ui8,ui32>(11,ot);
  1839. iw.soundID = soundBase::gazebo;
  1840. cb->showInfoDialog(&iw);
  1841. cb->changePrimSkill(heroID,4,val);
  1842. break;
  1843. }
  1844. case 102://tree
  1845. {
  1846. const CGHeroInstance *h = cb->getHero(heroID);
  1847. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1848. if(!ttype)
  1849. {
  1850. cb->setObjProperty(this->id, ObjProperty::VISITORS, heroID); //add to the visitors
  1851. InfoWindow iw;
  1852. iw.soundID = sound;
  1853. iw.components.push_back(Component(id,subid,1,0));
  1854. iw.player = cb->getOwner(heroID);
  1855. iw.text << std::pair<ui8,ui32>(11,148);
  1856. cb->showInfoDialog(&iw);
  1857. cb->changePrimSkill(heroID,4,val);
  1858. break;
  1859. }
  1860. else
  1861. {
  1862. int res, resval;
  1863. if(ttype==1)
  1864. {
  1865. res = 6;
  1866. resval = 2000;
  1867. ot = 149;
  1868. }
  1869. else
  1870. {
  1871. res = 5;
  1872. resval = 10;
  1873. ot = 151;
  1874. }
  1875. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1876. {
  1877. ot++;
  1878. InfoWindow iw;
  1879. iw.soundID = sound;
  1880. iw.player = h->tempOwner;
  1881. iw.text << std::pair<ui8,ui32>(11,ot);
  1882. cb->showInfoDialog(&iw);
  1883. return;
  1884. }
  1885. BlockingDialog sd (true, false);
  1886. sd.soundID = sound;
  1887. sd.player = cb->getOwner(heroID);
  1888. sd.text << std::pair<ui8,ui32>(11,ot);
  1889. sd.components.push_back (Component (Component::RESOURCE, res, resval, 0));
  1890. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1891. }
  1892. break;
  1893. }
  1894. case 41://library of enlightenment
  1895. {
  1896. const CGHeroInstance *h = cb->getHero(heroID);
  1897. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1898. {
  1899. InfoWindow iw;
  1900. iw.soundID = sound;
  1901. iw.player = cb->getOwner(heroID);
  1902. iw.text << std::pair<ui8,ui32>(11,68);
  1903. cb->showInfoDialog(&iw);
  1904. }
  1905. else
  1906. {
  1907. cb->setObjProperty(this->id, ObjProperty::VISITORS, heroID); //add to the visitors
  1908. cb->changePrimSkill(heroID,0,2);
  1909. cb->changePrimSkill(heroID,1,2);
  1910. cb->changePrimSkill(heroID,2,2);
  1911. cb->changePrimSkill(heroID,3,2);
  1912. InfoWindow iw;
  1913. iw.soundID = sound;
  1914. iw.player = cb->getOwner(heroID);
  1915. iw.text << std::pair<ui8,ui32>(11,66);
  1916. cb->showInfoDialog(&iw);
  1917. }
  1918. break;
  1919. }
  1920. case 47: //School of Magic
  1921. case 107://School of War
  1922. {
  1923. int skill = (ID==47 ? 2 : 0);
  1924. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1925. {
  1926. InfoWindow iw;
  1927. iw.soundID = sound;
  1928. iw.player = cb->getOwner(heroID);
  1929. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1930. cb->showInfoDialog(&iw);
  1931. }
  1932. else
  1933. {
  1934. BlockingDialog sd(true,true);
  1935. sd.soundID = sound;
  1936. sd.player = cb->getOwner(heroID);
  1937. sd.text << std::pair<ui8,ui32>(11,ot);
  1938. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1939. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1940. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1941. }
  1942. }
  1943. break;
  1944. }
  1945. }
  1946. else
  1947. {
  1948. ot++;
  1949. InfoWindow iw;
  1950. iw.soundID = sound;
  1951. iw.player = cb->getOwner(heroID);
  1952. iw.text << std::pair<ui8,ui32>(11,ot);
  1953. cb->showInfoDialog(&iw);
  1954. }
  1955. }
  1956. const std::string & CGVisitableOPH::getHoverText() const
  1957. {
  1958. int pom = -1;
  1959. switch(ID)
  1960. {
  1961. case 4:
  1962. pom = -1;
  1963. break;
  1964. case 51:
  1965. pom = 8;
  1966. break;
  1967. case 23:
  1968. pom = 7;
  1969. break;
  1970. case 61:
  1971. pom = 11;
  1972. break;
  1973. case 32:
  1974. pom = 4;
  1975. break;
  1976. case 100:
  1977. pom = 5;
  1978. break;
  1979. case 102:
  1980. pom = 18;
  1981. break;
  1982. case 41:
  1983. break;
  1984. case 47: //School of Magic
  1985. pom = 9;
  1986. break;
  1987. case 107://School of War
  1988. pom = 10;
  1989. break;
  1990. default:
  1991. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1992. }
  1993. hoverName = VLC->generaltexth->names[ID];
  1994. if(pom >= 0)
  1995. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1996. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1997. if(h)
  1998. {
  1999. hoverName += ' ';
  2000. hoverName += (vstd::contains(visitors,h->id))
  2001. ? (VLC->generaltexth->allTexts[352]) //visited
  2002. : ( VLC->generaltexth->allTexts[353]); //not visited
  2003. }
  2004. return hoverName;
  2005. }
  2006. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  2007. {
  2008. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2009. cb->changePrimSkill(heroID,primSkill-1,2);
  2010. }
  2011. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  2012. {
  2013. if(what == ObjProperty::VISITORS)
  2014. visitors.insert(val);
  2015. }
  2016. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  2017. {
  2018. if(!which) //player refused to pay
  2019. return;
  2020. int base = (ID == 47 ? 2 : 0);
  2021. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2022. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  2023. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  2024. }
  2025. COPWBonus::COPWBonus (int index, CGTownInstance *TOWN)
  2026. {
  2027. ID = index;
  2028. town = TOWN;
  2029. id = town->bonusingBuildings.size();
  2030. }
  2031. void COPWBonus::setProperty(ui8 what, ui32 val)
  2032. {
  2033. switch (what)
  2034. {
  2035. case 4:
  2036. visitors.insert(val);
  2037. break;
  2038. case 12:
  2039. visitors.clear();
  2040. break;
  2041. }
  2042. }
  2043. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  2044. {
  2045. int heroID = h->id;
  2046. if (town->builtBuildings.find(ID) != town->builtBuildings.end())
  2047. {
  2048. InfoWindow iw;
  2049. iw.player = h->tempOwner;
  2050. switch (town->subID)
  2051. {
  2052. case 0: //Stables
  2053. if (!h->hasBonusFrom(Bonus::OBJECT, 94)) //does not stack with advMap Stables
  2054. {
  2055. GiveBonus gb;
  2056. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  2057. gb.id = heroID;
  2058. cb->giveHeroBonus(&gb);
  2059. iw.text << VLC->generaltexth->allTexts[580];
  2060. cb->showInfoDialog(&iw);
  2061. }
  2062. break;
  2063. case 5: //Mana Vortex
  2064. if (visitors.empty() && h->mana <= h->manaLimit())
  2065. {
  2066. cb->setManaPoints (heroID, 2 * h->manaLimit());
  2067. cb->setObjProperty (id, ObjProperty::VISITED, true);
  2068. iw.text << VLC->generaltexth->allTexts[579];
  2069. cb->showInfoDialog(&iw);
  2070. cb->setObjProperty (town->id, 11, id); //add to visitors
  2071. }
  2072. break;
  2073. }
  2074. }
  2075. }
  2076. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  2077. {
  2078. ID = index;
  2079. town = TOWN;
  2080. id = town->bonusingBuildings.size();
  2081. }
  2082. void CTownBonus::setProperty (ui8 what, ui32 val)
  2083. {
  2084. if(what == 4)
  2085. visitors.insert(val);
  2086. }
  2087. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  2088. {
  2089. int heroID = h->id;
  2090. if ((town->builtBuildings.find(ID) != town->builtBuildings.end()) && (visitors.find(heroID) == visitors.end()))
  2091. {
  2092. InfoWindow iw;
  2093. int what, val, mid;
  2094. switch (ID)
  2095. {
  2096. case 23:
  2097. switch(town->subID)
  2098. {
  2099. case 2: //wall
  2100. what = 3;
  2101. val = 1;
  2102. mid = 581;
  2103. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  2104. break;
  2105. case 3: //order of fire
  2106. what = 2;
  2107. val = 1;
  2108. mid = 582;
  2109. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  2110. break;
  2111. case 6://hall of valhalla
  2112. what = 0;
  2113. val = 1;
  2114. mid = 584;
  2115. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  2116. break;
  2117. case 5://academy of battle scholars
  2118. what = 4;
  2119. val = 1000;
  2120. mid = 583;
  2121. iw.components.push_back (Component(Component::EXPERIENCE, 0, 1000, 0));
  2122. break;
  2123. }
  2124. break;
  2125. case 17:
  2126. switch(town->subID)
  2127. {
  2128. case 7: //cage of warlords
  2129. what = 1;
  2130. val = 1;
  2131. mid = 585;
  2132. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  2133. break;
  2134. }
  2135. break;
  2136. }
  2137. iw.player = cb->getOwner(heroID);
  2138. iw.text << VLC->generaltexth->allTexts[mid];
  2139. cb->showInfoDialog(&iw);
  2140. cb->changePrimSkill (heroID, what, val);
  2141. if (town->visitingHero == h)
  2142. cb->setObjProperty (town->id, 11, id); //add to visitors
  2143. else
  2144. cb->setObjProperty (town->id, 13, id); //then it must be garrisoned hero
  2145. }
  2146. }
  2147. /*const std::string & CGCreature::getHoverText() const
  2148. {
  2149. hoverName = VLC->generaltexth->names[ID];
  2150. hoverName += "\n Power rating: ";
  2151. float ratio = ((float)getArmyStrength() / cb->getSelectedHero(cb->getCurrentPlayer())->getTotalStrength());
  2152. if (ratio < 0.1) hoverName += "Effortless";
  2153. else if (ratio < 0.3) hoverName += "Very Weak";
  2154. else if (ratio < 0.6) hoverName += "Weak";
  2155. else if (ratio < 0.9) hoverName += "A bit weaker";
  2156. else if (ratio < 1.1) hoverName += "Equal";
  2157. else if (ratio < 1.3) hoverName += "A bit stronger";
  2158. else if (ratio < 1.8) hoverName += "Strong";
  2159. else if (ratio < 2.5) hoverName += "Very Strong";
  2160. else if (ratio < 4) hoverName += "Challenging";
  2161. else if (ratio < 8) hoverName += "Overpowering";
  2162. else if (ratio < 20) hoverName += "Deadly";
  2163. else hoverName += "Impossible";
  2164. return hoverName;
  2165. }*/
  2166. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  2167. {
  2168. int action = takenAction(h);
  2169. switch( action ) //decide what we do...
  2170. {
  2171. case -2: //fight
  2172. fight(h);
  2173. break;
  2174. case -1: //flee
  2175. {
  2176. flee(h);
  2177. break;
  2178. }
  2179. case 0: //join for free
  2180. {
  2181. BlockingDialog ynd(true,false);
  2182. ynd.player = h->tempOwner;
  2183. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  2184. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2185. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  2186. break;
  2187. }
  2188. default: //join for gold
  2189. {
  2190. assert(action > 0);
  2191. //ask if player agrees to pay gold
  2192. BlockingDialog ynd(true,false);
  2193. ynd.player = h->tempOwner;
  2194. std::string tmp = VLC->generaltexth->advobtxt[90];
  2195. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(slots.find(0)->second.count));
  2196. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  2197. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID]->namePl);
  2198. ynd.text << tmp;
  2199. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  2200. break;
  2201. }
  2202. }
  2203. }
  2204. void CGCreature::endBattle( BattleResult *result ) const
  2205. {
  2206. if(result->winner==0)
  2207. {
  2208. cb->removeObject(id);
  2209. }
  2210. else
  2211. {
  2212. //int killedAmount=0;
  2213. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  2214. // if(i->first == subID)
  2215. // killedAmount += i->second;
  2216. //cb->setAmount(id, slots.find(0)->second.second - killedAmount);
  2217. MetaString ms;
  2218. int pom = slots.find(0)->second.getQuantityID();
  2219. pom = 174 + 3*pom + 1;
  2220. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2221. cb->setHoverName(id,&ms);
  2222. }
  2223. }
  2224. void CGCreature::initObj()
  2225. {
  2226. blockVisit = true;
  2227. switch(character)
  2228. {
  2229. case 0:
  2230. character = 0;
  2231. break;
  2232. case 1:
  2233. character = 1 + ran()%7;
  2234. break;
  2235. case 2:
  2236. character = 1 + ran()%10;
  2237. break;
  2238. case 3:
  2239. character = 4 + ran()%7;
  2240. break;
  2241. case 4:
  2242. character = 10;
  2243. break;
  2244. }
  2245. slots[0].setType(subID);
  2246. si32 &amount = slots[0].count;
  2247. CCreature &c = *VLC->creh->creatures[subID];
  2248. if(!amount)
  2249. if(c.ammMax == c.ammMin)
  2250. amount = c.ammMax;
  2251. else
  2252. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2253. MetaString ms;
  2254. int pom = slots.find(0)->second.getQuantityID();
  2255. pom = 174 + 3*pom + 1;
  2256. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2257. ms.toString(hoverName);
  2258. }
  2259. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2260. {
  2261. double hlp = h->getTotalStrength() / getArmyStrength();
  2262. if(!character) //compliant creatures will always join
  2263. return 0;
  2264. else if(allowJoin)//test for joining
  2265. {
  2266. int factor;
  2267. if(hlp >= 7)
  2268. factor = 11;
  2269. else if(hlp >= 1)
  2270. factor = (int)(2*(hlp-1));
  2271. else if(hlp >= 0.5)
  2272. factor = -1;
  2273. else if(hlp >= 0.333)
  2274. factor = -2;
  2275. else
  2276. factor = -3;
  2277. int sympathy = 0;
  2278. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2279. myKindCres.insert(subID); //we
  2280. myKindCres.insert(VLC->creh->creatures[subID]->upgrades.begin(),VLC->creh->creatures[subID]->upgrades.end()); //our upgrades
  2281. for(std::vector<CCreature*>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  2282. if(vstd::contains((*i)->upgrades, (ui32) id)) //it's our base creatures
  2283. myKindCres.insert((*i)->idNumber);
  2284. int count = 0, //how many creatures of our kind has hero
  2285. totalCount = 0;
  2286. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  2287. {
  2288. if(vstd::contains(myKindCres,i->second.type->idNumber))
  2289. count += i->second.count;
  2290. totalCount += i->second.count;
  2291. }
  2292. if(count*2 > totalCount)
  2293. sympathy++;
  2294. if(count)
  2295. sympathy++;
  2296. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  2297. if(charisma >= character) //creatures might join...
  2298. {
  2299. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  2300. return 0; //join for free
  2301. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  2302. return VLC->creh->creatures[subID]->cost[6] * Slots().find(0)->second.count; //join for gold
  2303. }
  2304. }
  2305. //we are still here - creatures not joined heroes, test for fleeing
  2306. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  2307. //TODO: should be deterministic (will be needed for Vision spell)
  2308. int hlp2 = (int) (hlp - 2)*1000;
  2309. if(!neverFlees
  2310. && hlp2 >= 0
  2311. && rand()%2000 < hlp2
  2312. )
  2313. return -1; //flee
  2314. else
  2315. return -2; //fight
  2316. }
  2317. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2318. {
  2319. if(pursue)
  2320. {
  2321. fight(h);
  2322. }
  2323. else
  2324. {
  2325. cb->removeObject(id);
  2326. }
  2327. }
  2328. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2329. {
  2330. if(!accept)
  2331. {
  2332. if(takenAction(h,false) == -1) //they flee
  2333. {
  2334. flee(h);
  2335. }
  2336. else //they fight
  2337. {
  2338. InfoWindow iw;
  2339. iw.player = h->tempOwner;
  2340. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2341. cb->showInfoDialog(&iw);
  2342. fight(h);
  2343. }
  2344. }
  2345. else //accepted
  2346. {
  2347. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2348. {
  2349. InfoWindow iw;
  2350. iw.player = h->tempOwner;
  2351. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2352. cb->showInfoDialog(&iw);
  2353. //act as if player refused
  2354. joinDecision(h,cost,false);
  2355. return;
  2356. }
  2357. //take gold
  2358. if(cost)
  2359. cb->giveResource(h->tempOwner,6,-cost);
  2360. int slot = h->getSlotFor(subID);
  2361. if(slot >= 0) //there is place
  2362. {
  2363. //add creatures
  2364. SetGarrisons sg;
  2365. sg.garrs[h->id] = h->getArmy();
  2366. sg.garrs[h->id].addToSlot(slot, subID, getAmount(0));
  2367. cb->sendAndApply(&sg);
  2368. cb->removeObject(id);
  2369. }
  2370. else
  2371. {
  2372. cb->showGarrisonDialog(id,h->id,true,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  2373. }
  2374. }
  2375. }
  2376. void CGCreature::fight( const CGHeroInstance *h ) const
  2377. {
  2378. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2379. }
  2380. void CGCreature::flee( const CGHeroInstance * h ) const
  2381. {
  2382. BlockingDialog ynd(true,false);
  2383. ynd.player = h->tempOwner;
  2384. ynd.text << std::pair<ui8,ui32>(11,91);
  2385. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2386. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2387. }
  2388. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  2389. {
  2390. if(subID == 7) //TODO: support for abandoned mine
  2391. return;
  2392. if(h->tempOwner == tempOwner) //we're visiting our mine
  2393. {
  2394. cb->showGarrisonDialog(id,h->id,true,0);
  2395. return;
  2396. }
  2397. //TODO: check if mine is guarded
  2398. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  2399. MetaString ms;
  2400. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  2401. cb->setHoverName(id,&ms);
  2402. int vv=1; //amount of resource per turn
  2403. if (subID==0 || subID==2)
  2404. vv++;
  2405. else if (subID==6)
  2406. vv = 1000;
  2407. InfoWindow iw;
  2408. iw.soundID = soundBase::FLAGMINE;
  2409. iw.text << std::pair<ui8,ui32>(10,subID);
  2410. iw.player = h->tempOwner;
  2411. iw.components.push_back(Component(2,subID,vv,-1));
  2412. cb->showInfoDialog(&iw);
  2413. }
  2414. void CGMine::newTurn() const
  2415. {
  2416. if(cb->getDate() == 1)
  2417. return;
  2418. if (tempOwner == NEUTRAL_PLAYER)
  2419. return;
  2420. int vv = 1;
  2421. if (subID==0 || subID==2)
  2422. vv++;
  2423. else if (subID==6)
  2424. vv = 1000;
  2425. cb->giveResource(tempOwner,subID,vv);
  2426. }
  2427. void CGMine::initObj()
  2428. {
  2429. MetaString ms;
  2430. ms << std::pair<ui8,ui32>(9,subID);
  2431. if(tempOwner >= PLAYER_LIMIT)
  2432. tempOwner = NEUTRAL_PLAYER;
  2433. else
  2434. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  2435. ms.toString(hoverName);
  2436. }
  2437. void CGResource::initObj()
  2438. {
  2439. blockVisit = true;
  2440. hoverName = VLC->generaltexth->restypes[subID];
  2441. if(!amount)
  2442. {
  2443. switch(subID)
  2444. {
  2445. case 6:
  2446. amount = 500 + (rand()%6)*100;
  2447. break;
  2448. case 0: case 2:
  2449. amount = 6 + (rand()%5);
  2450. break;
  2451. default:
  2452. amount = 3 + (rand()%3);
  2453. break;
  2454. }
  2455. }
  2456. }
  2457. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  2458. {
  2459. if(stacksCount())
  2460. {
  2461. if(message.size())
  2462. {
  2463. BlockingDialog ynd(true,false);
  2464. ynd.player = h->getOwner();
  2465. ynd.text << message;
  2466. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  2467. }
  2468. else
  2469. {
  2470. fightForRes(1,h);
  2471. }
  2472. }
  2473. else
  2474. {
  2475. if(message.length())
  2476. {
  2477. InfoWindow iw;
  2478. iw.player = h->tempOwner;
  2479. iw.text << message;
  2480. cb->showInfoDialog(&iw);
  2481. }
  2482. collectRes(h->getOwner());
  2483. }
  2484. }
  2485. void CGResource::collectRes( int player ) const
  2486. {
  2487. cb->giveResource(player,subID,amount);
  2488. ShowInInfobox sii;
  2489. sii.player = player;
  2490. sii.c = Component(2,subID,amount,0);
  2491. sii.text << std::pair<ui8,ui32>(11,113);
  2492. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  2493. cb->showCompInfo(&sii);
  2494. cb->removeObject(id);
  2495. }
  2496. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  2497. {
  2498. if(agreed)
  2499. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  2500. }
  2501. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2502. {
  2503. if(result->winner == 0) //attacker won
  2504. collectRes(h->getOwner());
  2505. }
  2506. void CGVisitableOPW::newTurn() const
  2507. {
  2508. if (cb->getDate(1)==1) //first day of week
  2509. {
  2510. cb->setObjProperty(id, ObjProperty::VISITED, false);
  2511. MetaString ms; //set text to "not visited"
  2512. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  2513. cb->setHoverName(id,&ms);
  2514. }
  2515. }
  2516. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  2517. {
  2518. int mid, sound = 0;
  2519. switch (ID)
  2520. {
  2521. case 55: //mystical garden
  2522. sound = soundBase::experience;
  2523. mid = 92;
  2524. break;
  2525. case 112://windmill
  2526. sound = soundBase::GENIE;
  2527. mid = 170;
  2528. break;
  2529. case 109://waterwheel
  2530. sound = soundBase::GENIE;
  2531. mid = 164;
  2532. break;
  2533. }
  2534. if (visited)
  2535. {
  2536. if (ID!=112)
  2537. mid++;
  2538. else
  2539. mid--;
  2540. InfoWindow iw;
  2541. iw.soundID = sound;
  2542. iw.player = h->tempOwner;
  2543. iw.text << std::pair<ui8,ui32>(11,mid);
  2544. cb->showInfoDialog(&iw);
  2545. }
  2546. else
  2547. {
  2548. int type, sub, val;
  2549. type = 2;
  2550. switch (ID)
  2551. {
  2552. case 55:
  2553. if (rand()%2)
  2554. {
  2555. sub = 5;
  2556. val = 5;
  2557. }
  2558. else
  2559. {
  2560. sub = 6;
  2561. val = 500;
  2562. }
  2563. break;
  2564. case 112:
  2565. mid = 170;
  2566. sub = (rand() % 5) + 1;
  2567. val = (rand() % 4) + 3;
  2568. break;
  2569. case 109:
  2570. mid = 164;
  2571. sub = 6;
  2572. if(cb->getDate(0)<8)
  2573. val = 500;
  2574. else
  2575. val = 1000;
  2576. }
  2577. cb->giveResource(h->tempOwner,sub,val);
  2578. InfoWindow iw;
  2579. iw.soundID = sound;
  2580. iw.player = h->tempOwner;
  2581. iw.components.push_back(Component(type,sub,val,0));
  2582. iw.text << std::pair<ui8,ui32>(11,mid);
  2583. cb->showInfoDialog(&iw);
  2584. cb->setObjProperty(id, ObjProperty::VISITED, true);
  2585. MetaString ms; //set text to "visited"
  2586. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  2587. cb->setHoverName(id,&ms);
  2588. }
  2589. }
  2590. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  2591. {
  2592. if(what == ObjProperty::VISITED)
  2593. visited = val;
  2594. }
  2595. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  2596. {
  2597. int destinationid=-1;
  2598. switch(ID)
  2599. {
  2600. case 43: //one way - find corresponding exit monolith
  2601. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  2602. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  2603. else
  2604. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2605. break;
  2606. case 45://two way monolith - pick any other one
  2607. case 111: //Whirlpool
  2608. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  2609. {
  2610. while ((destinationid = objs[ID][subID][rand()%objs[ID][subID].size()]) == id); //choose another exit
  2611. if (ID == 111)
  2612. {
  2613. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  2614. {
  2615. CCreatureSet army = h->getArmy();
  2616. int targetstack = army.Slots().size()-1;
  2617. for(TSlots::const_reverse_iterator i = army.Slots().rbegin(); i != army.Slots().rend(); i++)
  2618. {
  2619. if (army.getPower(targetstack) > army.getPower(i->first))
  2620. {
  2621. targetstack = (i->first);
  2622. }
  2623. }
  2624. CCreatureSet ourArmy;
  2625. ourArmy.addStack (targetstack, army.getStack(targetstack));
  2626. TQuantity tq = (TQuantity)((double)(ourArmy.getAmount(targetstack))*0.3);
  2627. if (tq) //casualties > 0
  2628. {
  2629. ourArmy.setStackCount (targetstack, tq);
  2630. InfoWindow iw;
  2631. iw.player = h->tempOwner;
  2632. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  2633. iw.components.push_back (Component(ourArmy.getStack(targetstack)));
  2634. cb->showInfoDialog(&iw);
  2635. cb->takeCreatures (h->id, ourArmy.Slots());
  2636. }
  2637. }
  2638. }
  2639. }
  2640. else
  2641. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  2642. break;
  2643. case 103: //find nearest subterranean gate on the other level
  2644. {
  2645. int i=0;
  2646. for(; i < gates.size(); i++)
  2647. {
  2648. if(gates[i].first == id)
  2649. {
  2650. destinationid = gates[i].second;
  2651. break;
  2652. }
  2653. else if(gates[i].second == id)
  2654. {
  2655. destinationid = gates[i].first;
  2656. break;
  2657. }
  2658. }
  2659. if(destinationid < 0 || i == gates.size()) //no exit
  2660. {
  2661. InfoWindow iw;
  2662. iw.player = h->tempOwner;
  2663. iw.text.addTxt(MetaString::ADVOB_TXT, 153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  2664. cb->sendAndApply(&iw);
  2665. }
  2666. break;
  2667. }
  2668. }
  2669. if(destinationid < 0)
  2670. {
  2671. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  2672. return;
  2673. }
  2674. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  2675. }
  2676. void CGTeleport::initObj()
  2677. {
  2678. int si = subID;
  2679. switch (ID)
  2680. {
  2681. case 103://ignore subterranean gates subid
  2682. case 111:
  2683. {
  2684. si = 0;
  2685. break;
  2686. }
  2687. default:
  2688. break;
  2689. }
  2690. objs[ID][si].push_back(id);
  2691. }
  2692. void CGTeleport::postInit() //matches subterranean gates into pairs
  2693. {
  2694. //split on underground and surface gates
  2695. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  2696. for(size_t i = 0; i < objs[103][0].size(); i++)
  2697. {
  2698. const CGObjectInstance *hlp = cb->getObj(objs[103][0][i]);
  2699. gatesSplit[hlp->pos.z].push_back(hlp);
  2700. }
  2701. //sort by position
  2702. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  2703. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  2704. {
  2705. const CGObjectInstance *cur = gatesSplit[0][i];
  2706. //find nearest underground exit
  2707. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  2708. for(int j = 0; j < gatesSplit[1].size(); j++)
  2709. {
  2710. const CGObjectInstance *checked = gatesSplit[1][j];
  2711. if(!checked)
  2712. continue;
  2713. double hlp = checked->pos.dist2d(cur->pos);
  2714. if(hlp < best.second)
  2715. {
  2716. best.first = j;
  2717. best.second = hlp;
  2718. }
  2719. }
  2720. if(best.first >= 0) //found pair
  2721. {
  2722. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  2723. gatesSplit[1][best.first] = NULL;
  2724. }
  2725. else
  2726. {
  2727. gates.push_back(std::pair<int, int>(cur->id, -1));
  2728. }
  2729. }
  2730. objs.erase(103);
  2731. }
  2732. void CGArtifact::initObj()
  2733. {
  2734. blockVisit = true;
  2735. if(ID == 5)
  2736. hoverName = VLC->arth->artifacts[subID].Name();
  2737. }
  2738. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  2739. {
  2740. if(!stacksCount())
  2741. {
  2742. if(ID == 5)
  2743. {
  2744. InfoWindow iw;
  2745. iw.soundID = soundBase::treasure;
  2746. iw.player = h->tempOwner;
  2747. iw.components.push_back(Component(4,subID,0,0));
  2748. if(message.length())
  2749. iw.text << message;
  2750. else
  2751. iw.text << std::pair<ui8,ui32>(12,subID);
  2752. cb->showInfoDialog(&iw);
  2753. }
  2754. pick(h);
  2755. }
  2756. else
  2757. {
  2758. if(message.size())
  2759. {
  2760. BlockingDialog ynd(true,false);
  2761. ynd.player = h->getOwner();
  2762. ynd.text << message;
  2763. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  2764. }
  2765. else
  2766. {
  2767. fightForArt(0,h);
  2768. }
  2769. }
  2770. }
  2771. void CGArtifact::pick(const CGHeroInstance * h) const
  2772. {
  2773. if(ID == 5) //Artifact
  2774. {
  2775. cb->giveHeroArtifact(subID,h->id,-2);
  2776. }
  2777. else if(ID == 93) // Spell scroll
  2778. {
  2779. //TODO: support for the spell scroll
  2780. }
  2781. cb->removeObject(id);
  2782. }
  2783. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  2784. {
  2785. if(agreed)
  2786. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  2787. }
  2788. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2789. {
  2790. if(result->winner == 0) //attacker won
  2791. pick(h);
  2792. }
  2793. void CGPickable::initObj()
  2794. {
  2795. blockVisit = true;
  2796. switch(ID)
  2797. {
  2798. case 12: //campfire
  2799. val2 = (ran()%3) + 4; //4 - 6
  2800. val1 = val2 * 100;
  2801. type = ran()%6; //given resource
  2802. break;
  2803. case 29: //floatsam
  2804. switch(type = ran()%4)
  2805. {
  2806. case 0:
  2807. val1 = val2 = 0;
  2808. break;
  2809. case 1:
  2810. val1 = 5;
  2811. val2 = 0;
  2812. break;
  2813. case 2:
  2814. val1 = 5;
  2815. val2 = 200;
  2816. break;
  2817. case 3:
  2818. val1 = 10;
  2819. val2 = 500;
  2820. break;
  2821. }
  2822. break;
  2823. case 82: //sea chest
  2824. {
  2825. int hlp = ran()%100;
  2826. if(hlp < 20)
  2827. {
  2828. val1 = 0;
  2829. type = 0;
  2830. }
  2831. else if(hlp < 90)
  2832. {
  2833. val1 = 1500;
  2834. type = 2;
  2835. }
  2836. else
  2837. {
  2838. val1 = 1000;
  2839. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2840. type = 1;
  2841. }
  2842. }
  2843. break;
  2844. case 86: //Shipwreck Survivor
  2845. {
  2846. int hlp = ran()%100;
  2847. if(hlp < 55)
  2848. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2849. else if(hlp < 75)
  2850. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  2851. else if(hlp < 95)
  2852. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  2853. else
  2854. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  2855. }
  2856. break;
  2857. case 101: //treasure chest
  2858. {
  2859. int hlp = ran()%100;
  2860. if(hlp >= 95)
  2861. {
  2862. type = 1;
  2863. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  2864. return;
  2865. }
  2866. else if (hlp >= 65)
  2867. {
  2868. val1 = 2000;
  2869. }
  2870. else if(hlp >= 33)
  2871. {
  2872. val1 = 1500;
  2873. }
  2874. else
  2875. {
  2876. val1 = 1000;
  2877. }
  2878. val2 = val1 - 500;
  2879. type = 0;
  2880. break;
  2881. }
  2882. }
  2883. }
  2884. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2885. {
  2886. switch(ID)
  2887. {
  2888. case 12: //campfire
  2889. {
  2890. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2891. cb->giveResource(h->tempOwner,6,val1);//gold
  2892. InfoWindow iw;
  2893. iw.soundID = soundBase::experience;
  2894. iw.player = h->tempOwner;
  2895. iw.components.push_back(Component(2,6,val1,0));
  2896. iw.components.push_back(Component(2,type,val2,0));
  2897. iw.text << std::pair<ui8,ui32>(11,23);
  2898. cb->showInfoDialog(&iw);
  2899. break;
  2900. }
  2901. case 29: //flotsam
  2902. {
  2903. cb->giveResource(h->tempOwner,0,val1); //wood
  2904. cb->giveResource(h->tempOwner,6,val2);//gold
  2905. InfoWindow iw;
  2906. iw.soundID = soundBase::GENIE;
  2907. iw.player = h->tempOwner;
  2908. if(val1)
  2909. iw.components.push_back(Component(2,0,val1,0));
  2910. if(val2)
  2911. iw.components.push_back(Component(2,6,val2,0));
  2912. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  2913. cb->showInfoDialog(&iw);
  2914. break;
  2915. }
  2916. case 82: //Sea Chest
  2917. {
  2918. InfoWindow iw;
  2919. iw.soundID = soundBase::chest;
  2920. iw.player = h->tempOwner;
  2921. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  2922. if(val1) //there is gold
  2923. {
  2924. iw.components.push_back(Component(2,6,val1,0));
  2925. cb->giveResource(h->tempOwner,6,val1);
  2926. }
  2927. if(type == 1) //art
  2928. {
  2929. //TODO: what if no space in backpack?
  2930. iw.components.push_back(Component(4, val2, 1, 0));
  2931. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  2932. cb->giveHeroArtifact(val2,h->id,-2);
  2933. }
  2934. cb->showInfoDialog(&iw);
  2935. break;
  2936. }
  2937. case 86: //Shipwreck Survivor
  2938. {
  2939. //TODO: what if no space in backpack?
  2940. InfoWindow iw;
  2941. iw.soundID = soundBase::experience;
  2942. iw.player = h->tempOwner;
  2943. iw.components.push_back(Component(4,val1,1,0));
  2944. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  2945. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2946. cb->giveHeroArtifact(val1,h->id,-2);
  2947. cb->showInfoDialog(&iw);
  2948. break;
  2949. }
  2950. case 101: //treasure chest
  2951. {
  2952. if (subID) //not OH3 treasure chest
  2953. {
  2954. tlog2 << "Not supported WoG treasure chest!\n";
  2955. return;
  2956. }
  2957. if(type) //there is an artifact
  2958. {
  2959. cb->giveHeroArtifact(val1,h->id,-2);
  2960. InfoWindow iw;
  2961. iw.soundID = soundBase::treasure;
  2962. iw.player = h->tempOwner;
  2963. iw.components.push_back(Component(4,val1,1,0));
  2964. iw.text << std::pair<ui8,ui32>(11,145);
  2965. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2966. cb->showInfoDialog(&iw);
  2967. break;
  2968. }
  2969. else
  2970. {
  2971. BlockingDialog sd(false,true);
  2972. sd.player = h->tempOwner;
  2973. sd.text << std::pair<ui8,ui32>(11,146);
  2974. sd.components.push_back(Component(2,6,val1,0));
  2975. sd.components.push_back(Component(5,0,val2,0));
  2976. sd.soundID = soundBase::chest;
  2977. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2978. cb->showBlockingDialog(&sd,fun);
  2979. return;
  2980. }
  2981. }
  2982. }
  2983. cb->removeObject(id);
  2984. }
  2985. void CGPickable::chosen( int which, int heroID ) const
  2986. {
  2987. switch(which)
  2988. {
  2989. case 1: //player pick gold
  2990. cb->giveResource(cb->getOwner(heroID),6,val1);
  2991. break;
  2992. case 2: //player pick exp
  2993. cb->changePrimSkill(heroID, 4, val2);
  2994. break;
  2995. default:
  2996. throw std::string("Unhandled treasure choice");
  2997. }
  2998. cb->removeObject(id);
  2999. }
  3000. bool CQuest::checkQuest (const CGHeroInstance * h) const
  3001. {
  3002. switch (missionType)
  3003. {
  3004. case MISSION_NONE:
  3005. return true;
  3006. break;
  3007. case MISSION_LEVEL:
  3008. if (m13489val <= h->level)
  3009. return true;
  3010. return false;
  3011. break;
  3012. case MISSION_PRIMARY_STAT:
  3013. for (int i = 0; i < 4; ++i)
  3014. {
  3015. if (h->getPrimSkillLevel(i) < m2stats[i])
  3016. return false;
  3017. }
  3018. return true;
  3019. break;
  3020. case MISSION_KILL_HERO:
  3021. if (h->cb->gameState()->map->heroesToBeat[m13489val]->tempOwner < PLAYER_LIMIT)
  3022. return false; //if the pointer is not NULL
  3023. return true;
  3024. break;
  3025. case MISSION_KILL_CREATURE:
  3026. if (h->cb->gameState()->map->monsters[m13489val]->pos == int3(-1,-1,-1))
  3027. return true;
  3028. return false;
  3029. break;
  3030. case MISSION_ART:
  3031. for (int i = 0; i < m5arts.size(); ++i)
  3032. {
  3033. if (h->hasArt(m5arts[i]))
  3034. continue;
  3035. return false; //if the artifact was not found
  3036. }
  3037. return true;
  3038. break;
  3039. case MISSION_ARMY:
  3040. {
  3041. TSlots::const_iterator it, cre;
  3042. ui32 count;
  3043. for (cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  3044. {
  3045. for (count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  3046. {
  3047. if (it->second.type == cre->second.type)
  3048. count += it->second.count;
  3049. }
  3050. if (count < cre->second.count) //not enough creatures of this kind
  3051. return false;
  3052. }
  3053. }
  3054. return true;
  3055. break;
  3056. case MISSION_RESOURCES:
  3057. for (int i = 0; i < 7; ++i) //including Mithril ?
  3058. { //Quest has no direct access to callback
  3059. if (h->cb->getResource (h->tempOwner, i) < m7resources[i])
  3060. return false;
  3061. }
  3062. return true;
  3063. break;
  3064. case MISSION_HERO:
  3065. if (m13489val == h->type->ID)
  3066. return true;
  3067. return false;
  3068. break;
  3069. case MISSION_PLAYER:
  3070. if (m13489val == h->getOwner())
  3071. return true;
  3072. return false;
  3073. break;
  3074. default:
  3075. return false;
  3076. }
  3077. }
  3078. void CGSeerHut::initObj()
  3079. {
  3080. seerName = VLC->generaltexth->seerNames[ran()%VLC->generaltexth->seerNames.size()];
  3081. textOption = ran()%3;
  3082. progress = 0;
  3083. if (missionType)
  3084. {
  3085. if (!isCustom)
  3086. {
  3087. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  3088. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  3089. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  3090. }
  3091. }
  3092. else
  3093. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  3094. }
  3095. const std::string & CGSeerHut::getHoverText() const
  3096. {
  3097. switch (ID)
  3098. {
  3099. case 83:
  3100. hoverName = VLC->generaltexth->allTexts[347];
  3101. boost::algorithm::replace_first(hoverName,"%s", seerName);
  3102. break;
  3103. case 215:
  3104. hoverName = VLC->generaltexth->names[ID];
  3105. break;
  3106. default:
  3107. tlog5 << "unrecognized quest object\n";
  3108. }
  3109. if (progress & missionType) //rollover when the quest is active
  3110. {
  3111. MetaString ms;
  3112. ms << "\n\n" << VLC->generaltexth->quests[missionType-1][3][textOption];
  3113. std::string str;
  3114. switch (missionType)
  3115. {
  3116. case MISSION_LEVEL:
  3117. ms.addReplacement (m13489val);
  3118. break;
  3119. case MISSION_PRIMARY_STAT:
  3120. {
  3121. MetaString loot;
  3122. for (int i = 0; i < 4; ++i)
  3123. {
  3124. if (m2stats[i])
  3125. {
  3126. loot << "%d %s";
  3127. loot.addReplacement (m2stats[i]);
  3128. loot.addReplacement (VLC->generaltexth->primarySkillNames[i]);
  3129. }
  3130. }
  3131. ms.addReplacement(loot.buildList());
  3132. }
  3133. break;
  3134. case MISSION_KILL_HERO:
  3135. ms.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3136. break;
  3137. case MISSION_HERO:
  3138. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3139. break;
  3140. case MISSION_KILL_CREATURE:
  3141. {
  3142. ms.addReplacement(cb->gameState()->map->monsters[m13489val]->getStack(0));
  3143. }
  3144. break;
  3145. case MISSION_ART:
  3146. {
  3147. MetaString loot;
  3148. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3149. {
  3150. loot << "%s";
  3151. loot.addReplacement (MetaString::ART_NAMES, *it);
  3152. }
  3153. ms.addReplacement(loot.buildList());
  3154. }
  3155. break;
  3156. case MISSION_ARMY:
  3157. {
  3158. MetaString loot;
  3159. for (TSlots::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3160. {
  3161. loot << "%s";
  3162. loot.addReplacement(it->second);
  3163. }
  3164. ms.addReplacement (loot.buildList());
  3165. }
  3166. break;
  3167. case MISSION_RESOURCES:
  3168. {
  3169. MetaString loot;
  3170. for (int i = 0; i < 7; ++i)
  3171. {
  3172. if (m7resources[i])
  3173. {
  3174. loot << "%d %s";
  3175. loot.addReplacement (m7resources[i]);
  3176. loot.addReplacement (MetaString::RES_NAMES, i);
  3177. }
  3178. }
  3179. ms.addReplacement (loot.buildList());
  3180. }
  3181. break;
  3182. case MISSION_PLAYER:
  3183. ms.addReplacement (VLC->generaltexth->colors[m13489val]);
  3184. break;
  3185. default:
  3186. break;
  3187. }
  3188. ms.toString(str);
  3189. hoverName += str;
  3190. }
  3191. return hoverName;
  3192. }
  3193. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  3194. {
  3195. switch (what)
  3196. {
  3197. case 10:
  3198. progress = val;
  3199. break;
  3200. case 11:
  3201. missionType = CQuest::MISSION_NONE;
  3202. break;
  3203. }
  3204. }
  3205. void CGSeerHut::newTurn() const
  3206. {
  3207. if (lastDay >= 0 && lastDay <= cb->getDate(0)) //time is up
  3208. {
  3209. cb->setObjProperty (id,11,0);
  3210. cb->setObjProperty (id,10,0);
  3211. }
  3212. }
  3213. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  3214. {
  3215. InfoWindow iw;
  3216. iw.player = h->getOwner();
  3217. if (missionType)
  3218. {
  3219. if (!progress) //propose quest
  3220. {
  3221. iw.text << firstVisitText;
  3222. switch (missionType)
  3223. {
  3224. case MISSION_LEVEL:
  3225. iw.components.push_back (Component (Component::EXPERIENCE, 1, m13489val, 0));
  3226. if (!isCustom)
  3227. iw.text.addReplacement(m13489val);
  3228. break;
  3229. case MISSION_PRIMARY_STAT:
  3230. {
  3231. MetaString loot;
  3232. for (int i = 0; i < 4; ++i)
  3233. {
  3234. if (m2stats[i])
  3235. {
  3236. iw.components.push_back (Component (Component::PRIM_SKILL, i, m2stats[i], 0));
  3237. loot << "%d %s";
  3238. loot.addReplacement (m2stats[i]);
  3239. loot.addReplacement (VLC->generaltexth->primarySkillNames[i]);
  3240. }
  3241. }
  3242. if (!isCustom)
  3243. iw.text.addReplacement (loot.buildList());
  3244. }
  3245. break;
  3246. case MISSION_KILL_HERO:
  3247. iw.components.push_back (Component (Component::HERO,
  3248. cb->gameState()->map->heroesToBeat[m13489val]->type->ID, 0, 0));
  3249. if (!isCustom)
  3250. iw.text.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3251. break;
  3252. case MISSION_HERO:
  3253. iw.components.push_back (Component (Component::HERO, m13489val, 0, 0));
  3254. if (!isCustom)
  3255. iw.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3256. break;
  3257. case MISSION_KILL_CREATURE:
  3258. {
  3259. CStackInstance stack = cb->gameState()->map->monsters[m13489val]->getStack(0);
  3260. iw.components.push_back (Component(stack));
  3261. if (!isCustom)
  3262. {
  3263. iw.text.addReplacement(stack);
  3264. if (std::count(firstVisitText.begin(), firstVisitText.end(), '%') == 2) //say where is placed monster
  3265. {
  3266. iw.text.addReplacement (VLC->generaltexth->arraytxt[147+checkDirection()]);
  3267. }
  3268. }
  3269. }
  3270. break;
  3271. case MISSION_ART:
  3272. {
  3273. MetaString loot;
  3274. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3275. {
  3276. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3277. loot << "%s";
  3278. loot.addReplacement (MetaString::ART_NAMES, *it);
  3279. }
  3280. if (!isCustom)
  3281. iw.text.addReplacement (loot.buildList());
  3282. }
  3283. break;
  3284. case MISSION_ARMY:
  3285. {
  3286. MetaString loot;
  3287. for (TSlots::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3288. {
  3289. iw.components.push_back(Component(it->second));
  3290. loot << "%s";
  3291. loot.addReplacement(it->second);
  3292. }
  3293. if (!isCustom)
  3294. iw.text.addReplacement (loot.buildList());
  3295. }
  3296. break;
  3297. case MISSION_RESOURCES:
  3298. {
  3299. MetaString loot;
  3300. for (int i = 0; i < 7; ++i)
  3301. {
  3302. if (m7resources[i])
  3303. {
  3304. iw.components.push_back (Component (Component::RESOURCE, i, m7resources[i], 0));
  3305. loot << "%d %s";
  3306. loot.addReplacement (m7resources[i]);
  3307. loot.addReplacement (MetaString::RES_NAMES, i);
  3308. }
  3309. }
  3310. if (!isCustom)
  3311. iw.text.addReplacement (loot.buildList());
  3312. }
  3313. break;
  3314. case MISSION_PLAYER:
  3315. iw.components.push_back (Component (Component::FLAG, m13489val, 0, 0));
  3316. if (!isCustom)
  3317. iw.text.addReplacement (VLC->generaltexth->colors[m13489val]);
  3318. break;
  3319. }
  3320. cb->setObjProperty (id,10,1);
  3321. cb->showInfoDialog(&iw);
  3322. }
  3323. else if (!checkQuest(h))
  3324. {
  3325. iw.text << nextVisitText;
  3326. cb->showInfoDialog(&iw);
  3327. }
  3328. if (checkQuest(h)) // propose completion, also on first visit
  3329. {
  3330. BlockingDialog bd (true, false);
  3331. bd.player = h->getOwner();
  3332. bd.soundID = soundBase::QUEST;
  3333. bd.text << completedText;
  3334. switch (missionType)
  3335. {
  3336. case CQuest::MISSION_LEVEL:
  3337. if (!isCustom)
  3338. bd.text.addReplacement(m13489val);
  3339. break;
  3340. case CQuest::MISSION_PRIMARY_STAT:
  3341. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  3342. {
  3343. MetaString loot;
  3344. for (int i = 0; i < 4; ++i)
  3345. {
  3346. if (m2stats[i])
  3347. {
  3348. loot << "%d %s";
  3349. loot.addReplacement (m2stats[i]);
  3350. loot.addReplacement (VLC->generaltexth->primarySkillNames[i]);
  3351. }
  3352. }
  3353. if (!isCustom)
  3354. bd.text.addReplacement (loot.buildList());
  3355. }
  3356. break;
  3357. case CQuest::MISSION_ART:
  3358. {
  3359. MetaString loot;
  3360. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3361. {
  3362. bd.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  3363. loot << "%s";
  3364. loot.addReplacement (MetaString::ART_NAMES, *it);
  3365. }
  3366. if (!isCustom)
  3367. bd.text.addReplacement (loot.buildList());
  3368. }
  3369. break;
  3370. case CQuest::MISSION_ARMY:
  3371. {
  3372. MetaString loot;
  3373. for (TSlots::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3374. {
  3375. bd.components.push_back(Component(it->second));
  3376. loot << "%s";
  3377. loot.addReplacement(it->second);
  3378. }
  3379. if (!isCustom)
  3380. bd.text.addReplacement (loot.buildList());
  3381. }
  3382. break;
  3383. case CQuest::MISSION_RESOURCES:
  3384. {
  3385. MetaString loot;
  3386. for (int i = 0; i < 7; ++i)
  3387. {
  3388. if (m7resources[i])
  3389. {
  3390. bd.components.push_back (Component (Component::RESOURCE, i, m7resources[i], 0));
  3391. loot << "%d %s";
  3392. loot.addReplacement (m7resources[i]);
  3393. loot.addReplacement (MetaString::RES_NAMES, i);
  3394. }
  3395. }
  3396. if (!isCustom)
  3397. bd.text.addReplacement (loot.buildList());
  3398. }
  3399. break;
  3400. case MISSION_KILL_HERO:
  3401. if (!isCustom)
  3402. bd.text.addReplacement(cb->gameState()->map->heroesToBeat[m13489val]->name);
  3403. break;
  3404. case MISSION_HERO:
  3405. if (!isCustom)
  3406. bd.text.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3407. break;
  3408. case MISSION_KILL_CREATURE:
  3409. {
  3410. {
  3411. bd.text.addReplacement(cb->gameState()->map->monsters[m13489val]->getArmy()[0]);
  3412. if (std::count(firstVisitText.begin(), firstVisitText.end(), '%') == 2) //say where is placed monster
  3413. {
  3414. bd.text.addReplacement (VLC->generaltexth->arraytxt[147+checkDirection()]);
  3415. }
  3416. }
  3417. }
  3418. case MISSION_PLAYER:
  3419. bd.components.push_back (Component (Component::FLAG, m13489val, 0, 0));
  3420. if (!isCustom)
  3421. bd.text.addReplacement (VLC->generaltexth->colors[m13489val]);
  3422. break;
  3423. }
  3424. cb->showBlockingDialog (&bd, boost::bind (&CGSeerHut::finishQuest, this, h, _1));
  3425. return;
  3426. }
  3427. }
  3428. else
  3429. {
  3430. iw.text << VLC->generaltexth->seerEmpty[textOption];
  3431. if (ID == 83)
  3432. iw.text.addReplacement(seerName);
  3433. cb->showInfoDialog(&iw);
  3434. }
  3435. }
  3436. int CGSeerHut::checkDirection() const
  3437. {
  3438. int3 cord = cb->gameState()->map->monsters[m13489val]->pos;
  3439. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  3440. {
  3441. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  3442. return 8;
  3443. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  3444. return 1;
  3445. else //northeast
  3446. return 2;
  3447. }
  3448. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  3449. {
  3450. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  3451. return 7;
  3452. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  3453. return 9;
  3454. else //east
  3455. return 3;
  3456. }
  3457. else //south
  3458. {
  3459. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  3460. return 6;
  3461. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  3462. return 5;
  3463. else //southeast
  3464. return 4;
  3465. }
  3466. }
  3467. void CGSeerHut::finishQuest (const CGHeroInstance * h, ui32 accept) const
  3468. {
  3469. if (accept)
  3470. {
  3471. switch (missionType)
  3472. {
  3473. case CQuest::MISSION_ART:
  3474. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3475. {
  3476. cb->removeArtifact(*it, h->id);
  3477. }
  3478. break;
  3479. case CQuest::MISSION_ARMY:
  3480. cb->takeCreatures (h->id, m6creatures);
  3481. break;
  3482. case CQuest::MISSION_RESOURCES:
  3483. for (int i = 0; i < 7; ++i)
  3484. {
  3485. cb->giveResource(h->getOwner(), i, -m7resources[i]);
  3486. }
  3487. break;
  3488. default:
  3489. break;
  3490. }
  3491. cb->setObjProperty (id,11,0); //no more mission avaliable
  3492. completeQuest(h); //make sure to remove QuestQuard at the very end
  3493. }
  3494. }
  3495. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  3496. {
  3497. InfoWindow iw;
  3498. iw.player = h->getOwner();
  3499. switch (rewardType)
  3500. {
  3501. case 1: //experience
  3502. cb->changePrimSkill(h->id, 4, rVal, false);
  3503. iw.components.push_back (Component (Component::EXPERIENCE, 0, rVal, 0));
  3504. break;
  3505. case 2: //mana points
  3506. cb->setManaPoints(h->id, h->mana+rVal);
  3507. iw.components.push_back (Component (Component::PRIM_SKILL, 5, rVal, 0));
  3508. break;
  3509. case 3: case 4: //morale /luck
  3510. {
  3511. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  3512. Bonus::OBJECT, rVal, h->id, "", -1);
  3513. GiveBonus gb;
  3514. gb.id = h->id;
  3515. gb.bonus = hb;
  3516. //gb.descr = "";
  3517. cb->giveHeroBonus(&gb);
  3518. iw.components.push_back (Component (
  3519. (rewardType == 3 ? Component::MORALE : Component::LUCK), 0, rVal, 0));
  3520. }
  3521. break;
  3522. case 5: //resources
  3523. cb->giveResource(h->getOwner(), rID, rVal);
  3524. iw.components.push_back (Component (Component::RESOURCE, rID, rVal, 0));
  3525. break;
  3526. case 6: //main ability bonus (attak, defence etd.)
  3527. cb->changePrimSkill(h->id, rID, rVal, false);
  3528. iw.components.push_back (Component (Component::PRIM_SKILL, rID, rVal, 0));
  3529. break;
  3530. case 7: // secondary ability gain
  3531. cb->changeSecSkill(h->id, rID, rVal, false);
  3532. iw.components.push_back (Component (Component::SEC_SKILL, rID, rVal, 0));
  3533. break;
  3534. case 8: // artifact
  3535. cb->giveHeroArtifact(rID, h->id, -2);
  3536. iw.components.push_back (Component (Component::ARTIFACT, rID, 0, 0));
  3537. break;
  3538. case 9:// spell
  3539. {
  3540. std::set<ui32> spell;
  3541. spell.insert (rID);
  3542. cb->changeSpells(h->id, true, spell);
  3543. iw.components.push_back (Component (Component::SPELL, rID, 0, 0));
  3544. }
  3545. break;
  3546. case 10:// creature
  3547. {
  3548. CCreatureSet creatures;
  3549. creatures.setCreature (0, rID, rVal);
  3550. cb->giveCreatures (id, h, creatures);
  3551. iw.components.push_back (Component (Component::CREATURE, rID, rVal, 0));
  3552. }
  3553. break;
  3554. default:
  3555. break;
  3556. }
  3557. cb->showInfoDialog(&iw);
  3558. }
  3559. void CGQuestGuard::initObj()
  3560. {
  3561. blockVisit = true;
  3562. progress = 0;
  3563. textOption = ran()%3 + 3; //3-5
  3564. if (missionType && !isCustom)
  3565. {
  3566. firstVisitText = VLC->generaltexth->quests[missionType-1][0][textOption];
  3567. nextVisitText = VLC->generaltexth->quests[missionType-1][1][textOption];
  3568. completedText = VLC->generaltexth->quests[missionType-1][2][textOption];
  3569. }
  3570. else
  3571. firstVisitText = VLC->generaltexth->seerEmpty[textOption];
  3572. }
  3573. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  3574. {
  3575. cb->removeObject(id);
  3576. }
  3577. void CGWitchHut::initObj()
  3578. {
  3579. ability = allowedAbilities[ran()%allowedAbilities.size()];
  3580. }
  3581. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  3582. {
  3583. InfoWindow iw;
  3584. iw.soundID = soundBase::gazebo;
  3585. iw.player = h->getOwner();
  3586. if(!hasVisited(h->tempOwner))
  3587. cb->setObjProperty(id,10,h->tempOwner);
  3588. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  3589. {
  3590. iw.text << std::pair<ui8,ui32>(11,172);
  3591. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3592. }
  3593. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  3594. {
  3595. iw.text << std::pair<ui8,ui32>(11,173);
  3596. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3597. }
  3598. else //give sec skill
  3599. {
  3600. iw.components.push_back(Component(1, ability, 1, 0));
  3601. iw.text << std::pair<ui8,ui32>(11,171);
  3602. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  3603. cb->changeSecSkill(h->id,ability,1,true);
  3604. }
  3605. cb->showInfoDialog(&iw);
  3606. }
  3607. const std::string & CGWitchHut::getHoverText() const
  3608. {
  3609. hoverName = VLC->generaltexth->names[ID];
  3610. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  3611. {
  3612. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  3613. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  3614. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3615. if(h && h->getSecSkillLevel(ability)) //hero knows that ability
  3616. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  3617. }
  3618. return hoverName;
  3619. }
  3620. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  3621. {
  3622. bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  3623. int messageID, bonusType, bonusVal;
  3624. int bonusMove = 0, sound = -1;
  3625. InfoWindow iw;
  3626. iw.player = h->tempOwner;
  3627. GiveBonus gbonus;
  3628. gbonus.id = h->id;
  3629. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  3630. gbonus.bonus.source = Bonus::OBJECT;
  3631. gbonus.bonus.id = ID;
  3632. bool second = false;
  3633. Bonus secondBonus;
  3634. switch(ID)
  3635. {
  3636. case 11: //buoy
  3637. messageID = 21;
  3638. sound = soundBase::MORALE;
  3639. gbonus.bonus.type = Bonus::MORALE;
  3640. gbonus.bonus.val = +1;
  3641. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  3642. break;
  3643. case 14: //swan pond
  3644. messageID = 29;
  3645. sound = soundBase::LUCK;
  3646. gbonus.bonus.type = Bonus::LUCK;
  3647. gbonus.bonus.val = 2;
  3648. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  3649. bonusMove = -h->movement;
  3650. break;
  3651. case 28: //Faerie Ring
  3652. messageID = 49;
  3653. sound = soundBase::LUCK;
  3654. gbonus.bonus.type = Bonus::LUCK;
  3655. gbonus.bonus.val = 1;
  3656. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  3657. break;
  3658. case 30: //fountain of fortune
  3659. messageID = 55;
  3660. sound = soundBase::LUCK;
  3661. gbonus.bonus.type = Bonus::LUCK;
  3662. gbonus.bonus.val = rand()%5 - 1;
  3663. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  3664. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  3665. break;
  3666. case 38: //idol of fortune
  3667. messageID = 62;
  3668. sound = soundBase::experience;
  3669. gbonus.bonus.val = 1;
  3670. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  3671. if(cb->getDate(1) == 7) //7th day of week
  3672. {
  3673. gbonus.bonus.type = Bonus::MORALE;
  3674. second = true;
  3675. secondBonus = gbonus.bonus;
  3676. secondBonus.type = Bonus::LUCK;
  3677. }
  3678. else
  3679. {
  3680. gbonus.bonus.type = (cb->getDate(1)%2) ? Bonus::LUCK : Bonus::MORALE;
  3681. }
  3682. break;
  3683. case 52: //Mermaid
  3684. messageID = 83;
  3685. sound = soundBase::LUCK;
  3686. gbonus.bonus.type = Bonus::LUCK;
  3687. gbonus.bonus.val = 1;
  3688. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  3689. break;
  3690. case 64: //Rally Flag
  3691. sound = soundBase::MORALE;
  3692. messageID = 111;
  3693. gbonus.bonus.type = Bonus::MORALE;
  3694. gbonus.bonus.val = 1;
  3695. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  3696. second = true;
  3697. secondBonus = gbonus.bonus;
  3698. secondBonus.type = Bonus::LUCK;
  3699. bonusMove = 400;
  3700. break;
  3701. case 56: //oasis
  3702. messageID = 95;
  3703. gbonus.bonus.type = Bonus::MORALE;
  3704. gbonus.bonus.val = 1;
  3705. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  3706. bonusMove = 800;
  3707. break;
  3708. case 96: //temple
  3709. messageID = 140;
  3710. iw.soundID = soundBase::temple;
  3711. gbonus.bonus.type = Bonus::MORALE;
  3712. if(cb->getDate(1)==7) //sunday
  3713. {
  3714. gbonus.bonus.val = 2;
  3715. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  3716. }
  3717. else
  3718. {
  3719. gbonus.bonus.val = 1;
  3720. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  3721. }
  3722. break;
  3723. case 110://Watering Hole
  3724. sound = soundBase::MORALE;
  3725. messageID = 166;
  3726. gbonus.bonus.type = Bonus::MORALE;
  3727. gbonus.bonus.val = 1;
  3728. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  3729. bonusMove = 400;
  3730. break;
  3731. case 31: //Fountain of Youth
  3732. sound = soundBase::MORALE;
  3733. messageID = 57;
  3734. gbonus.bonus.type = Bonus::MORALE;
  3735. gbonus.bonus.val = 1;
  3736. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  3737. bonusMove = 400;
  3738. break;
  3739. case 94: //Stables TODO: upgrade Cavaliers
  3740. sound = soundBase::horse20;
  3741. std::set<ui32> slots;
  3742. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); ++i)
  3743. {
  3744. if(i->second.type->idNumber == 10)
  3745. slots.insert(i->first);
  3746. }
  3747. if (!slots.empty())
  3748. {
  3749. for (std::set<ui32>::const_iterator i = slots.begin(); i != slots.end(); i++)
  3750. {
  3751. UpgradeCreature uc(*i, id, 11);
  3752. //uc.applyGh (&gh);
  3753. }
  3754. }
  3755. messageID = 137;
  3756. gbonus.bonus.type = Bonus::LAND_MOVEMENT;
  3757. gbonus.bonus.val = 600;
  3758. bonusMove = 600;
  3759. gbonus.bonus.duration = Bonus::ONE_WEEK;
  3760. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  3761. break;
  3762. }
  3763. if(visited)
  3764. {
  3765. if(ID==64 || ID==96 || ID==56 || ID==52 || ID==94)
  3766. messageID--;
  3767. else
  3768. messageID++;
  3769. }
  3770. else
  3771. {
  3772. //TODO: fix if second bonus val != main bonus val
  3773. if(gbonus.bonus.type == Bonus::MORALE || secondBonus.type == Bonus::MORALE)
  3774. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  3775. if(gbonus.bonus.type == Bonus::LUCK || secondBonus.type == Bonus::LUCK)
  3776. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  3777. cb->giveHeroBonus(&gbonus);
  3778. if(second)
  3779. {
  3780. gbonus.bonus = secondBonus;
  3781. cb->giveHeroBonus(&gbonus);
  3782. }
  3783. if(bonusMove) //swan pond - take all move points, stables - give move point this day
  3784. {
  3785. SetMovePoints smp;
  3786. smp.hid = h->id;
  3787. smp.val = h->movement + bonusMove;
  3788. cb->setMovePoints(&smp);
  3789. }
  3790. }
  3791. iw.soundID = sound;
  3792. iw.text << std::pair<ui8,ui32>(11,messageID);
  3793. cb->showInfoDialog(&iw);
  3794. }
  3795. const std::string & CGBonusingObject::getHoverText() const
  3796. {
  3797. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  3798. hoverName = VLC->generaltexth->names[ID];
  3799. if(h)
  3800. {
  3801. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  3802. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3803. else
  3804. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3805. }
  3806. return hoverName;
  3807. }
  3808. void CGBonusingObject::initObj()
  3809. {
  3810. if(ID == 11 || ID == 52) //Buoy / Mermaid
  3811. {
  3812. blockVisit = true;
  3813. }
  3814. }
  3815. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  3816. {
  3817. int messageID;
  3818. InfoWindow iw;
  3819. iw.player = h->tempOwner;
  3820. iw.soundID = soundBase::GENIE;
  3821. if (!visited)
  3822. {
  3823. if (h->mana > h->manaLimit())
  3824. messageID = 76;
  3825. else
  3826. {
  3827. messageID = 74;
  3828. cb->setManaPoints (h->id, 2 * h->manaLimit());
  3829. cb->setObjProperty (id, ObjProperty::VISITED, true);
  3830. }
  3831. }
  3832. else
  3833. messageID = 75;
  3834. iw.text << std::pair<ui8,ui32>(11,messageID);
  3835. cb->showInfoDialog(&iw);
  3836. }
  3837. const std::string & CGMagicSpring::getHoverText() const
  3838. {
  3839. hoverName = VLC->generaltexth->names[ID];
  3840. if(!visited)
  3841. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  3842. else
  3843. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  3844. return hoverName;
  3845. }
  3846. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  3847. {
  3848. int message;
  3849. InfoWindow iw;
  3850. iw.soundID = soundBase::faerie;
  3851. iw.player = h->tempOwner;
  3852. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  3853. {
  3854. message = 78;
  3855. }
  3856. else if(h->mana < h->manaLimit())
  3857. {
  3858. giveDummyBonus(h->id);
  3859. cb->setManaPoints(h->id,h->manaLimit());
  3860. message = 77;
  3861. }
  3862. else
  3863. {
  3864. message = 79;
  3865. }
  3866. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  3867. cb->showInfoDialog(&iw);
  3868. }
  3869. const std::string & CGMagicWell::getHoverText() const
  3870. {
  3871. getNameVis(hoverName);
  3872. return hoverName;
  3873. }
  3874. void CGPandoraBox::initObj()
  3875. {
  3876. blockVisit = (ID==6); //block only if it's really pandora's box (events also derive from that class)
  3877. }
  3878. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  3879. {
  3880. BlockingDialog bd (true, false);
  3881. bd.player = h->getOwner();
  3882. bd.soundID = soundBase::QUEST;
  3883. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  3884. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  3885. }
  3886. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  3887. {
  3888. if (accept)
  3889. {
  3890. if (stacksCount() > 0) //if pandora's box is protected by army
  3891. {
  3892. InfoWindow iw;
  3893. iw.player = h->tempOwner;
  3894. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  3895. cb->showInfoDialog(&iw);
  3896. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  3897. }
  3898. else if (message.size() == 0 && resources.size() == 0
  3899. && primskills.size() == 0 && abilities.size() == 0
  3900. && abilityLevels.size() == 0 && artifacts.size() == 0
  3901. && spells.size() == 0 && creatures.Slots().size() > 0
  3902. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  3903. {
  3904. InfoWindow iw;
  3905. iw.player = h->tempOwner;
  3906. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  3907. cb->showInfoDialog(&iw);
  3908. }
  3909. else //if it gives something without battle
  3910. {
  3911. giveContents (h, false);
  3912. }
  3913. }
  3914. }
  3915. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  3916. {
  3917. if(result->winner)
  3918. return;
  3919. giveContents(h,true);
  3920. }
  3921. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  3922. {
  3923. InfoWindow iw;
  3924. iw.player = h->getOwner();
  3925. bool changesPrimSkill = false;
  3926. for (int i = 0; i < primskills.size(); i++)
  3927. {
  3928. if(primskills[i])
  3929. {
  3930. changesPrimSkill = true;
  3931. break;
  3932. }
  3933. }
  3934. if(gainedExp || changesPrimSkill || abilities.size())
  3935. {
  3936. getText(iw,afterBattle,175,h);
  3937. if(gainedExp)
  3938. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  3939. for(int i=0; i<primskills.size(); i++)
  3940. if(primskills[i])
  3941. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  3942. for(int i=0; i<abilities.size(); i++)
  3943. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  3944. cb->showInfoDialog(&iw);
  3945. //give exp
  3946. if(gainedExp)
  3947. cb->changePrimSkill(h->id,4,gainedExp,false);
  3948. //give prim skills
  3949. for(int i=0; i<primskills.size(); i++)
  3950. if(primskills[i])
  3951. cb->changePrimSkill(h->id,i,primskills[i],false);
  3952. //give sec skills
  3953. for(int i=0; i<abilities.size(); i++)
  3954. {
  3955. int curLev = h->getSecSkillLevel(abilities[i]);
  3956. if(curLev && curLev < abilityLevels[i]
  3957. || h->secSkills.size() < SKILL_PER_HERO )
  3958. {
  3959. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  3960. }
  3961. }
  3962. }
  3963. if(spells.size())
  3964. {
  3965. std::set<ui32> spellsToGive;
  3966. iw.components.clear();
  3967. for(int i=0; i<spells.size(); i++)
  3968. {
  3969. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  3970. spellsToGive.insert(spells[i]);
  3971. }
  3972. if(spellsToGive.size())
  3973. {
  3974. cb->changeSpells(h->id,true,spellsToGive);
  3975. cb->showInfoDialog(&iw);
  3976. }
  3977. }
  3978. if(manaDiff)
  3979. {
  3980. getText(iw,afterBattle,manaDiff,176,177,h);
  3981. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  3982. cb->showInfoDialog(&iw);
  3983. cb->setManaPoints(h->id, h->mana + manaDiff);
  3984. }
  3985. if(moraleDiff)
  3986. {
  3987. getText(iw,afterBattle,moraleDiff,178,179,h);
  3988. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  3989. cb->showInfoDialog(&iw);
  3990. GiveBonus gb;
  3991. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id,"");
  3992. gb.id = h->id;
  3993. cb->giveHeroBonus(&gb);
  3994. }
  3995. if(luckDiff)
  3996. {
  3997. getText(iw,afterBattle,luckDiff,180,181,h);
  3998. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  3999. cb->showInfoDialog(&iw);
  4000. GiveBonus gb;
  4001. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id,"");
  4002. gb.id = h->id;
  4003. cb->giveHeroBonus(&gb);
  4004. }
  4005. iw.components.clear();
  4006. iw.text.clear();
  4007. for(int i=0; i<resources.size(); i++)
  4008. {
  4009. if(resources[i] < 0)
  4010. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4011. }
  4012. if(iw.components.size())
  4013. {
  4014. getText(iw,afterBattle,182,h);
  4015. cb->showInfoDialog(&iw);
  4016. }
  4017. iw.components.clear();
  4018. iw.text.clear();
  4019. for(int i=0; i<resources.size(); i++)
  4020. {
  4021. if(resources[i] > 0)
  4022. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4023. }
  4024. if(iw.components.size())
  4025. {
  4026. getText(iw,afterBattle,183,h);
  4027. cb->showInfoDialog(&iw);
  4028. }
  4029. iw.components.clear();
  4030. // getText(iw,afterBattle,183,h);
  4031. for(int i=0; i<artifacts.size(); i++)
  4032. {
  4033. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  4034. if(iw.components.size() >= 14)
  4035. {
  4036. cb->showInfoDialog(&iw);
  4037. iw.components.clear();
  4038. }
  4039. }
  4040. if(iw.components.size())
  4041. {
  4042. cb->showInfoDialog(&iw);
  4043. }
  4044. for(int i=0; i<resources.size(); i++)
  4045. if(resources[i])
  4046. cb->giveResource(h->getOwner(),i,resources[i]);
  4047. for(int i=0; i<artifacts.size(); i++)
  4048. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  4049. iw.components.clear();
  4050. iw.text.clear();
  4051. if (creatures.Slots().size())
  4052. { //this part is taken straight from creature bank
  4053. MetaString loot;
  4054. CCreatureSet ourArmy = creatures;
  4055. for(TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  4056. { //build list of joined creatures
  4057. iw.components.push_back(Component(i->second));
  4058. loot << "%s";
  4059. loot.addReplacement(i->second);
  4060. }
  4061. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second.count == 1)
  4062. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4063. else
  4064. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4065. iw.text.addReplacement (loot.buildList());
  4066. iw.text.addReplacement (h->name);
  4067. cb->showInfoDialog(&iw);
  4068. cb->giveCreatures (id, h, ourArmy);
  4069. }
  4070. if(!afterBattle && message.size())
  4071. {
  4072. iw.text << message;
  4073. cb->showInfoDialog(&iw);
  4074. }
  4075. cb->removeObject(id);
  4076. }
  4077. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  4078. {
  4079. if(afterBattle || !message.size())
  4080. {
  4081. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  4082. iw.text.addReplacement(h->name);
  4083. }
  4084. else
  4085. {
  4086. iw.text << message;
  4087. afterBattle = true;
  4088. }
  4089. }
  4090. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  4091. {
  4092. iw.components.clear();
  4093. iw.text.clear();
  4094. if(afterBattle || !message.size())
  4095. {
  4096. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  4097. iw.text.addReplacement(h->name);
  4098. }
  4099. else
  4100. {
  4101. iw.text << message;
  4102. afterBattle = true;
  4103. }
  4104. }
  4105. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  4106. {
  4107. if(!(availableFor & (1 << h->tempOwner)))
  4108. return;
  4109. if(cb->getPlayerSettings(h->tempOwner)->human)
  4110. {
  4111. if(humanActivate)
  4112. activated(h);
  4113. }
  4114. else if(computerActivate)
  4115. activated(h);
  4116. }
  4117. void CGEvent::activated( const CGHeroInstance * h ) const
  4118. {
  4119. if(stacksCount() > 0)
  4120. {
  4121. InfoWindow iw;
  4122. iw.player = h->tempOwner;
  4123. if(message.size())
  4124. iw.text << message;
  4125. else
  4126. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4127. cb->showInfoDialog(&iw);
  4128. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  4129. }
  4130. else
  4131. {
  4132. giveContents(h,false);
  4133. }
  4134. }
  4135. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  4136. {
  4137. InfoWindow iw;
  4138. iw.player = h->tempOwner;
  4139. switch (ID)
  4140. {
  4141. case 58://redwood observatory
  4142. case 60://pillar of fire
  4143. {
  4144. iw.soundID = soundBase::LIGHTHOUSE;
  4145. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  4146. FoWChange fw;
  4147. fw.player = h->tempOwner;
  4148. fw.mode = 1;
  4149. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  4150. cb->sendAndApply (&fw);
  4151. break;
  4152. }
  4153. case 15://cover of darkness
  4154. {
  4155. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  4156. for (std::map<ui8, PlayerState>::iterator i = cb->gameState()->players.begin(); i != cb->gameState()->players.end(); i++)
  4157. {
  4158. if (h->tempOwner != i->first && i->second.status == PlayerState::INGAME) //TODO: team support
  4159. {
  4160. FoWChange fw;
  4161. fw.mode = 0;
  4162. fw.player = i->first;
  4163. cb->getTilesInRange (fw.tiles, pos, 20, i->first, -1);
  4164. cb->sendAndApply (&fw);
  4165. }
  4166. }
  4167. break;
  4168. }
  4169. }
  4170. cb->showInfoDialog(&iw);
  4171. }
  4172. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  4173. {
  4174. if(spell == 255)
  4175. {
  4176. tlog1 << "Not initialized shrine visited!\n";
  4177. return;
  4178. }
  4179. if(!hasVisited(h->tempOwner))
  4180. cb->setObjProperty(id,10,h->tempOwner);
  4181. InfoWindow iw;
  4182. iw.soundID = soundBase::temple;
  4183. iw.player = h->getOwner();
  4184. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  4185. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  4186. iw.text << ".";
  4187. if(!h->getArt(17)) //no spellbook
  4188. {
  4189. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  4190. }
  4191. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  4192. {
  4193. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  4194. }
  4195. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  4196. {
  4197. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  4198. }
  4199. else //give spell
  4200. {
  4201. std::set<ui32> spells;
  4202. spells.insert(spell);
  4203. cb->changeSpells(h->id,true,spells);
  4204. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  4205. }
  4206. cb->showInfoDialog(&iw);
  4207. }
  4208. void CGShrine::initObj()
  4209. {
  4210. if(spell == 255) //spell not set
  4211. {
  4212. int level = ID-87;
  4213. std::vector<ui16> possibilities;
  4214. cb->getAllowedSpells (possibilities, level);
  4215. if(!possibilities.size())
  4216. {
  4217. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  4218. return;
  4219. }
  4220. spell = possibilities[ran() % possibilities.size()];
  4221. }
  4222. }
  4223. const std::string & CGShrine::getHoverText() const
  4224. {
  4225. hoverName = VLC->generaltexth->names[ID];
  4226. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4227. {
  4228. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  4229. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  4230. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4231. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  4232. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  4233. }
  4234. return hoverName;
  4235. }
  4236. void CGSignBottle::initObj()
  4237. {
  4238. //if no text is set than we pick random from the predefined ones
  4239. if(!message.size())
  4240. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  4241. if(ID == 59)
  4242. {
  4243. blockVisit = true;
  4244. }
  4245. }
  4246. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  4247. {
  4248. InfoWindow iw;
  4249. iw.soundID = soundBase::STORE;
  4250. iw.player = h->getOwner();
  4251. iw.text << message;
  4252. cb->showInfoDialog(&iw);
  4253. if(ID == 59)
  4254. cb->removeObject(id);
  4255. }
  4256. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  4257. {
  4258. }
  4259. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  4260. {
  4261. int type = bonusType;
  4262. int bid = bonusID;
  4263. //check if the bonus if applicable, if not - give primary skill (always possible)
  4264. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  4265. if((type == 1
  4266. && ((ssl == 3) || (!ssl && h->secSkills.size() == SKILL_PER_HERO))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  4267. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)))) //hero doesn't have a spellbook or already knows the spell
  4268. {
  4269. type = 0;
  4270. bid = ran() % PRIMARY_SKILLS;
  4271. }
  4272. InfoWindow iw;
  4273. iw.soundID = soundBase::gazebo;
  4274. iw.player = h->getOwner();
  4275. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  4276. switch (type)
  4277. {
  4278. case 0:
  4279. cb->changePrimSkill(h->id,bid,+1);
  4280. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  4281. break;
  4282. case 1:
  4283. cb->changeSecSkill(h->id,bid,+1);
  4284. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  4285. break;
  4286. case 2:
  4287. {
  4288. std::set<ui32> hlp;
  4289. hlp.insert(bid);
  4290. cb->changeSpells(h->id,true,hlp);
  4291. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  4292. }
  4293. break;
  4294. default:
  4295. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  4296. return;
  4297. }
  4298. cb->showInfoDialog(&iw);
  4299. cb->removeObject(id);
  4300. }
  4301. void CGScholar::initObj()
  4302. {
  4303. blockVisit = true;
  4304. if(bonusType == 255)
  4305. {
  4306. bonusType = ran()%3;
  4307. switch(bonusType)
  4308. {
  4309. case 0:
  4310. bonusID = ran() % PRIMARY_SKILLS;
  4311. break;
  4312. case 1:
  4313. bonusID = ran() % SKILL_QUANTITY;
  4314. break;
  4315. case 2:
  4316. std::vector<ui16> possibilities;
  4317. for (int i = 1; i < 6; ++i)
  4318. cb->getAllowedSpells (possibilities, i);
  4319. bonusID = possibilities[ran() % possibilities.size()];
  4320. break;
  4321. }
  4322. }
  4323. }
  4324. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  4325. {
  4326. if (h->tempOwner != tempOwner && stacksCount() > 0) {
  4327. //TODO: Find a way to apply magic garrison effects in battle.
  4328. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  4329. return;
  4330. }
  4331. //New owner.
  4332. if (h->tempOwner != tempOwner)
  4333. cb->setOwner(id, h->tempOwner);
  4334. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  4335. }
  4336. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  4337. {
  4338. if (result->winner == 0)
  4339. onHeroVisit(h);
  4340. }
  4341. ui8 CGGarrison::getPassableness() const
  4342. {
  4343. return 1<<tempOwner;
  4344. }
  4345. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  4346. {
  4347. int sound = 0;
  4348. int txtid = -1;
  4349. switch(ID)
  4350. {
  4351. case 22: //Corpse
  4352. txtid = 37;
  4353. sound = soundBase::MYSTERY;
  4354. break;
  4355. case 39: //Lean To
  4356. sound = soundBase::GENIE;
  4357. txtid = 64;
  4358. break;
  4359. case 105://Wagon
  4360. sound = soundBase::GENIE;
  4361. txtid = 154;
  4362. break;
  4363. case 108:
  4364. break;
  4365. default:
  4366. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  4367. return;
  4368. }
  4369. if(ID == 108)//Warrior's Tomb
  4370. {
  4371. //ask if player wants to search the Tomb
  4372. BlockingDialog bd(true, false);
  4373. sound = soundBase::GRAVEYARD;
  4374. bd.player = h->getOwner();
  4375. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  4376. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  4377. return;
  4378. }
  4379. InfoWindow iw;
  4380. iw.soundID = sound;
  4381. iw.player = h->getOwner();
  4382. if(players.size()) //we have been already visited...
  4383. {
  4384. txtid++;
  4385. if(ID == 105) //wagon has extra text (for finding art) we need to omit
  4386. txtid++;
  4387. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4388. }
  4389. else //first visit - give bonus!
  4390. {
  4391. switch(artOrRes)
  4392. {
  4393. case 0: // first visit but empty
  4394. if (ID == 22) //Corpse
  4395. ++txtid;
  4396. else
  4397. txtid+=2;
  4398. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4399. break;
  4400. case 1: //art
  4401. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  4402. cb->giveHeroArtifact(bonusType,h->id,-2);
  4403. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4404. if (ID == 22) //Corpse
  4405. {
  4406. iw.text << "%s";
  4407. iw.text.addReplacement (MetaString::ART_NAMES, bonusType);
  4408. }
  4409. break;
  4410. case 2: //res
  4411. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  4412. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  4413. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  4414. break;
  4415. }
  4416. if(ID == 105 && artOrRes == 1)
  4417. {
  4418. iw.text.localStrings.back().second++;
  4419. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  4420. }
  4421. }
  4422. cb->showInfoDialog(&iw);
  4423. cb->setObjProperty(id,10,h->getOwner());
  4424. }
  4425. const std::string & CGOnceVisitable::getHoverText() const
  4426. {
  4427. hoverName = VLC->generaltexth->names[ID] + " ";
  4428. hoverName += (hasVisited(cb->getCurrentPlayer())
  4429. ? (VLC->generaltexth->allTexts[352]) //visited
  4430. : ( VLC->generaltexth->allTexts[353])); //not visited
  4431. return hoverName;
  4432. }
  4433. void CGOnceVisitable::initObj()
  4434. {
  4435. switch(ID)
  4436. {
  4437. case 22: //Corpse
  4438. {
  4439. blockVisit = true;
  4440. int hlp = ran()%100;
  4441. if(hlp < 20)
  4442. {
  4443. artOrRes = 1;
  4444. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  4445. }
  4446. else
  4447. {
  4448. artOrRes = 0;
  4449. }
  4450. }
  4451. break;
  4452. case 39: //Lean To
  4453. {
  4454. artOrRes = 2;
  4455. bonusType = ran()%6; //any basic resource without gold
  4456. bonusVal = ran()%4 + 1;
  4457. }
  4458. break;
  4459. case 108://Warrior's Tomb
  4460. {
  4461. artOrRes = 1;
  4462. int hlp = ran()%100;
  4463. if(hlp < 30)
  4464. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  4465. else if(hlp < 80)
  4466. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  4467. else if(hlp < 95)
  4468. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  4469. else
  4470. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  4471. }
  4472. break;
  4473. case 105://Wagon
  4474. {
  4475. int hlp = ran()%100;
  4476. if(hlp < 10)
  4477. {
  4478. artOrRes = 0; // nothing... :(
  4479. }
  4480. else if(hlp < 50) //minor or treasure art
  4481. {
  4482. artOrRes = 1;
  4483. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  4484. }
  4485. else //2 - 5 of non-gold resource
  4486. {
  4487. artOrRes = 2;
  4488. bonusType = ran()%6;
  4489. bonusVal = ran()%4 + 2;
  4490. }
  4491. }
  4492. break;
  4493. }
  4494. }
  4495. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  4496. {
  4497. if(accept)
  4498. {
  4499. InfoWindow iw;
  4500. iw.player = h->getOwner();
  4501. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  4502. if(players.size()) //we've been already visited, player found nothing
  4503. {
  4504. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  4505. }
  4506. else //first visit - give artifact
  4507. {
  4508. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  4509. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  4510. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  4511. cb->giveHeroArtifact(bonusType,h->id,-2);
  4512. }
  4513. if(!h->hasBonusFrom(Bonus::OBJECT,ID)) //we don't have modifier from this object yet
  4514. {
  4515. //ruin morale
  4516. GiveBonus gb;
  4517. gb.id = h->id;
  4518. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,-3,id,"");
  4519. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  4520. cb->giveHeroBonus(&gb);
  4521. }
  4522. cb->showInfoDialog(&iw);
  4523. cb->setObjProperty(id,10,h->getOwner());
  4524. }
  4525. }
  4526. void CBank::initObj()
  4527. {
  4528. switch (ID) //find apriopriate key
  4529. {
  4530. case 16: //bank
  4531. index = subID; break;
  4532. case 24: //derelict ship
  4533. index = 8; break;
  4534. case 25: //utopia
  4535. index = 10; break;
  4536. case 84: //crypt
  4537. index = 9; break;
  4538. case 85: //shipwreck
  4539. index = 7; break;
  4540. }
  4541. bc = NULL;
  4542. daycounter = 0;
  4543. multiplier = 1;
  4544. }
  4545. const std::string & CBank::getHoverText() const
  4546. {
  4547. hoverName = VLC->objh->creBanksNames[index];
  4548. if (bc == NULL)
  4549. hoverName += " " + VLC->generaltexth->allTexts[352];
  4550. else
  4551. hoverName += " " + VLC->generaltexth->allTexts[353];
  4552. return hoverName;
  4553. }
  4554. void CBank::reset(ui16 var1) //prevents desync
  4555. {
  4556. ui8 chance = 0;
  4557. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  4558. {
  4559. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  4560. {
  4561. bc = VLC->objh->banksInfo[index][i];
  4562. break;
  4563. }
  4564. }
  4565. artifacts.clear();
  4566. }
  4567. void CBank::initialize() const
  4568. {
  4569. cb->setObjProperty (id, 14, ran()); //synchronous reset
  4570. for (ui8 i = 0; i <= 3; i++)
  4571. {
  4572. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  4573. {
  4574. switch (i)
  4575. {
  4576. case 0:
  4577. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_TREASURE));
  4578. break;
  4579. case 1:
  4580. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MINOR));
  4581. break;
  4582. case 2:
  4583. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_MAJOR));
  4584. break;
  4585. case 3:
  4586. cb->setObjProperty(id, 18, cb->getRandomArt (CArtifact::ART_RELIC));
  4587. break;
  4588. }
  4589. }
  4590. }
  4591. }
  4592. void CBank::setPropertyDer (ui8 what, ui32 val)
  4593. /// random values are passed as arguments and processed identically on all clients
  4594. {
  4595. switch (what)
  4596. {
  4597. case 11: //daycounter
  4598. if (val == 0)
  4599. daycounter = 0;
  4600. else
  4601. daycounter++;
  4602. break;
  4603. case 12: //multiplier, in percent
  4604. multiplier = ((float)val)/100;
  4605. break;
  4606. case 13: //bank preset
  4607. bc = VLC->objh->banksInfo[index][val];
  4608. break;
  4609. case 14:
  4610. reset (val%100);
  4611. break;
  4612. case 15:
  4613. bc = NULL;
  4614. break;
  4615. case 16: //remove rewards from Derelict Ship
  4616. artifacts.clear();
  4617. break;
  4618. case 17: //set ArmedInstance army
  4619. {
  4620. int upgraded = 0;
  4621. if (val%100 < bc->upgradeChance) //once again anti-desync
  4622. upgraded = 1;
  4623. switch (bc->guards.size())
  4624. {
  4625. case 1:
  4626. for (int i = 0; i < 4; ++i)
  4627. setCreature (i, bc->guards[0].first, bc->guards[0].second / 5 );
  4628. setCreature (4, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  4629. break;
  4630. case 4:
  4631. {
  4632. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  4633. if (bc->guards.back().second) //all stacks are present
  4634. {
  4635. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  4636. {
  4637. setCreature (stacksCount(), it->first, it->second);
  4638. }
  4639. }
  4640. else //split first stack, as in Crypt
  4641. {
  4642. setCreature (0, bc->guards[0].first, bc->guards[0].second / 2 );
  4643. setCreature (1, bc->guards[0].first, bc->guards[0].second - (bc->guards[0].second / 2) );
  4644. setCreature (2, bc->guards[1].first, bc->guards[1].second);
  4645. setCreature (3, bc->guards[2].first + upgraded, bc->guards[2].second);
  4646. }
  4647. }
  4648. break;
  4649. default:
  4650. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  4651. return;
  4652. }
  4653. }
  4654. break;
  4655. case 18: //add Artifact
  4656. artifacts.push_back (val);
  4657. break;
  4658. }
  4659. }
  4660. void CBank::newTurn() const
  4661. {
  4662. if (bc == NULL)
  4663. {
  4664. if (cb->getDate(0) == 1)
  4665. initialize(); //initialize on first day
  4666. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  4667. {
  4668. initialize();
  4669. cb->setObjProperty (id, 11, 0); //daycounter 0
  4670. if (ID == 24 && cb->getDate(0) > 1)
  4671. {
  4672. cb->setObjProperty (id, 12, 0);//ugly hack to make derelict ships usable only once
  4673. cb->setObjProperty (id, 16, 0);
  4674. }
  4675. }
  4676. else
  4677. cb->setObjProperty (id, 11, 1); //daycounter++
  4678. }
  4679. }
  4680. void CBank::onHeroVisit (const CGHeroInstance * h) const
  4681. {
  4682. if (bc)
  4683. {
  4684. int banktext = 0;
  4685. switch (ID)
  4686. {
  4687. case 16: //generic bank
  4688. banktext = 32;
  4689. break;
  4690. case 24:
  4691. banktext = 41;
  4692. break;
  4693. case 25: //utopia
  4694. banktext = 47;
  4695. break;
  4696. case 84: //crypt
  4697. banktext = 119;
  4698. break;
  4699. case 85: //shipwreck
  4700. banktext = 122;
  4701. break;
  4702. }
  4703. BlockingDialog bd (true, false);
  4704. bd.player = h->getOwner();
  4705. bd.soundID = soundBase::DANGER;
  4706. bd.text << VLC->generaltexth->advobtxt[banktext];
  4707. if (ID == 16)
  4708. bd.text.addReplacement (VLC->objh->creBanksNames[index]);
  4709. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4710. }
  4711. else
  4712. {
  4713. InfoWindow iw;
  4714. iw.soundID = soundBase::GRAVEYARD;
  4715. iw.player = h->getOwner();
  4716. if (ID == 84) //morale penalty for empty Crypt
  4717. {
  4718. GiveBonus gbonus;
  4719. gbonus.id = h->id;
  4720. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4721. gbonus.bonus.source = Bonus::OBJECT;
  4722. gbonus.bonus.id = ID;
  4723. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[98];
  4724. gbonus.bonus.type = Bonus::MORALE;
  4725. gbonus.bonus.val = -1;
  4726. cb->giveHeroBonus(&gbonus);
  4727. iw.text << VLC->generaltexth->advobtxt[120];
  4728. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4729. }
  4730. else
  4731. {
  4732. iw.text << VLC->generaltexth->advobtxt[33];
  4733. iw.text.addReplacement (VLC->objh->creBanksNames[index]);
  4734. }
  4735. cb->showInfoDialog(&iw);
  4736. }
  4737. }
  4738. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  4739. {
  4740. if (accept)
  4741. {
  4742. cb->setObjProperty (id, 17, ran()); //get army
  4743. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  4744. }
  4745. }
  4746. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4747. {
  4748. if (result->winner == 0)
  4749. {
  4750. int textID = -1;
  4751. InfoWindow iw;
  4752. iw.player = h->getOwner();
  4753. MetaString loot;
  4754. switch (ID)
  4755. {
  4756. case 16: case 25:
  4757. textID = 34;
  4758. break;
  4759. case 24: //derelict ship
  4760. if (multiplier)
  4761. textID = 43;
  4762. else
  4763. {
  4764. GiveBonus gbonus;
  4765. gbonus.id = h->id;
  4766. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4767. gbonus.bonus.source = Bonus::OBJECT;
  4768. gbonus.bonus.id = ID;
  4769. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[101];
  4770. gbonus.bonus.type = Bonus::MORALE;
  4771. gbonus.bonus.val = -1;
  4772. cb->giveHeroBonus(&gbonus);
  4773. textID = 42;
  4774. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4775. }
  4776. break;
  4777. case 84: //Crypt
  4778. if (bc->resources.size() != 0)
  4779. textID = 121;
  4780. else
  4781. {
  4782. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4783. GiveBonus gbonus;
  4784. gbonus.id = h->id;
  4785. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4786. gbonus.bonus.source = Bonus::OBJECT;
  4787. gbonus.bonus.id = ID;
  4788. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  4789. gbonus.bonus.type = Bonus::MORALE;
  4790. gbonus.bonus.val = -1;
  4791. cb->giveHeroBonus(&gbonus);
  4792. textID = 120;
  4793. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  4794. }
  4795. break;
  4796. case 85: //shipwreck
  4797. if (bc->resources.size())
  4798. textID = 124;
  4799. else
  4800. textID = 123;
  4801. break;
  4802. }
  4803. //grant resources
  4804. if (textID != 42) //empty derelict ship gives no cash
  4805. {
  4806. for (int it = 0; it < bc->resources.size(); it++)
  4807. {
  4808. if (bc->resources[it] != 0)
  4809. {
  4810. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  4811. loot << "%d %s";
  4812. loot.addReplacement (iw.components.back().val);
  4813. loot.addReplacement (MetaString::RES_NAMES, iw.components.back().subtype);
  4814. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  4815. }
  4816. }
  4817. }
  4818. //grant artifacts
  4819. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  4820. {
  4821. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  4822. loot << "%s";
  4823. loot.addReplacement (MetaString::ART_NAMES, *it);
  4824. cb->giveHeroArtifact (*it, h->id ,-2);
  4825. }
  4826. //display loot
  4827. if (!iw.components.empty())
  4828. {
  4829. if (textID == 34)
  4830. {
  4831. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  4832. iw.text.addReplacement (MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  4833. iw.text.addReplacement (loot.buildList());
  4834. }
  4835. else
  4836. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  4837. cb->showInfoDialog(&iw);
  4838. }
  4839. loot.clear();
  4840. iw.components.clear();
  4841. iw.text.clear();
  4842. //grant creatures
  4843. CCreatureSet ourArmy;
  4844. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  4845. {
  4846. int slot = ourArmy.getSlotFor(it->first);
  4847. ourArmy.addToSlot(slot, it->first, it->second);
  4848. }
  4849. for (TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  4850. {
  4851. iw.components.push_back(Component(i->second));
  4852. loot << "%s";
  4853. loot.addReplacement (i->second);
  4854. }
  4855. if (ourArmy.Slots().size())
  4856. {
  4857. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second.count == 1)
  4858. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  4859. else
  4860. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  4861. iw.text.addReplacement (loot.buildList());
  4862. iw.text.addReplacement (h->name);
  4863. cb->showInfoDialog(&iw);
  4864. cb->giveCreatures (id, h, ourArmy);
  4865. }
  4866. cb->setObjProperty (id, 15, 0); //bc = NULL
  4867. }
  4868. else //in case of defeat
  4869. initialize();
  4870. }
  4871. void CGPyramid::initObj()
  4872. {
  4873. //would be nice to do that only once
  4874. if (!pyramidConfig.guards.size())
  4875. {
  4876. pyramidConfig.level = 1;
  4877. pyramidConfig.chance = 100;
  4878. pyramidConfig.upgradeChance = 0;
  4879. for (int i = 0; i < 2; ++i)
  4880. {
  4881. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(116, 20));
  4882. pyramidConfig.guards.push_back (std::pair <ui16, ui32>(117, 10));
  4883. }
  4884. pyramidConfig.combatValue; //how hard are guards of this level
  4885. pyramidConfig.value; //overall value of given things
  4886. pyramidConfig.rewardDifficulty; //proportion of reward value to difficulty of guards; how profitable is this creature Bank config
  4887. pyramidConfig.easiest; //?!?
  4888. }
  4889. bc = &pyramidConfig;
  4890. std::vector<ui16> available;
  4891. cb->getAllowedSpells (available, 5);
  4892. spell = (available[rand()%available.size()]);
  4893. }
  4894. const std::string & CGPyramid::getHoverText() const
  4895. {
  4896. hoverName = VLC->generaltexth->names[ID];
  4897. if (bc == NULL)
  4898. hoverName += " " + VLC->generaltexth->allTexts[352];
  4899. else
  4900. hoverName += " " + VLC->generaltexth->allTexts[353];
  4901. return hoverName;
  4902. }
  4903. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  4904. {
  4905. if (bc)
  4906. {
  4907. BlockingDialog bd (true, false);
  4908. bd.player = h->getOwner();
  4909. bd.soundID = soundBase::DANGER;
  4910. bd.text << VLC->generaltexth->advobtxt[105];
  4911. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  4912. }
  4913. else
  4914. {
  4915. InfoWindow iw;
  4916. iw.player = h->getOwner();
  4917. iw.text << VLC->generaltexth->advobtxt[107];
  4918. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  4919. GiveBonus gb;
  4920. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  4921. gb.id = h->id;
  4922. cb->giveHeroBonus(&gb);
  4923. cb->showInfoDialog(&iw);
  4924. }
  4925. }
  4926. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  4927. {
  4928. if (result->winner == 0)
  4929. {
  4930. InfoWindow iw;
  4931. iw.player = h->getOwner();
  4932. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  4933. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  4934. if (!h->getArt(17))
  4935. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  4936. else if (h->getSecSkillLevel(7) < 3)
  4937. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  4938. else
  4939. {
  4940. std::set<ui32> spells;
  4941. spells.insert (spell);
  4942. cb->changeSpells (h->id, true, spells);
  4943. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  4944. }
  4945. cb->showInfoDialog(&iw);
  4946. cb->setObjProperty (id, 15, 0);
  4947. }
  4948. }
  4949. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  4950. {
  4951. if (what >= 101 && what <= (100 + PLAYER_LIMIT))
  4952. playerKeyMap.find(what-101)->second.insert(val);
  4953. }
  4954. bool CGKeys::wasMyColorVisited (int player) const
  4955. {
  4956. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  4957. return true;
  4958. else
  4959. return false;
  4960. }
  4961. const std::string & CGKeymasterTent::getHoverText() const
  4962. {
  4963. hoverName = VLC->generaltexth->names[ID];
  4964. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4965. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4966. else
  4967. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4968. return hoverName;
  4969. }
  4970. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  4971. {
  4972. InfoWindow iw;
  4973. iw.soundID = soundBase::CAVEHEAD;
  4974. iw.player = h->getOwner();
  4975. if (!wasMyColorVisited (h->getOwner()) )
  4976. {
  4977. cb->setObjProperty(id, h->tempOwner+101, subID);
  4978. iw.text << std::pair<ui8,ui32>(11,19);
  4979. }
  4980. else
  4981. iw.text << std::pair<ui8,ui32>(11,20);
  4982. cb->showInfoDialog(&iw);
  4983. }
  4984. void CGBorderGuard::initObj()
  4985. {
  4986. blockVisit = true;
  4987. }
  4988. const std::string & CGBorderGuard::getHoverText() const
  4989. {
  4990. hoverName = VLC->generaltexth->names[ID];
  4991. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  4992. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  4993. else
  4994. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  4995. return hoverName;
  4996. }
  4997. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  4998. {
  4999. if (wasMyColorVisited (h->getOwner()) )
  5000. {
  5001. BlockingDialog bd (true, false);
  5002. bd.player = h->getOwner();
  5003. bd.soundID = soundBase::QUEST;
  5004. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  5005. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  5006. }
  5007. else
  5008. {
  5009. InfoWindow iw;
  5010. iw.player = h->getOwner();
  5011. iw.soundID = soundBase::CAVEHEAD;
  5012. iw.text << std::pair<ui8,ui32>(11,18);
  5013. cb->showInfoDialog (&iw);
  5014. }
  5015. }
  5016. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  5017. {
  5018. if (accept)
  5019. cb->removeObject(id);
  5020. }
  5021. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  5022. {
  5023. if (!wasMyColorVisited (h->getOwner()) )
  5024. {
  5025. InfoWindow iw;
  5026. iw.player = h->getOwner();
  5027. iw.text << std::pair<ui8,ui32>(11,18);
  5028. cb->showInfoDialog(&iw);
  5029. }
  5030. }
  5031. ui8 CGBorderGate::getPassableness() const
  5032. {
  5033. ui8 ret = 0;
  5034. for (int i = 0; i < PLAYER_LIMIT; i++)
  5035. ret |= wasMyColorVisited(i)<<i;
  5036. return ret;
  5037. }
  5038. void CGMagi::initObj()
  5039. {
  5040. if (ID == 27)
  5041. {
  5042. blockVisit = true;
  5043. eyelist[subID].push_back(id);
  5044. }
  5045. }
  5046. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  5047. {
  5048. if (ID == 37)
  5049. {
  5050. InfoWindow iw;
  5051. CenterView cv;
  5052. FoWChange fw;
  5053. cv.player = iw.player = fw.player = h->tempOwner;
  5054. iw.soundID = soundBase::LIGHTHOUSE;
  5055. iw.player = h->tempOwner;
  5056. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  5057. cb->showInfoDialog(&iw);
  5058. fw.mode = 1;
  5059. std::vector<si32>::iterator it;
  5060. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  5061. {
  5062. const CGObjectInstance *eye = cb->getObj(*it);
  5063. cb->getTilesInRange (fw.tiles, eye->pos, 5, h->tempOwner, 1);
  5064. cb->sendAndApply(&fw);
  5065. cv.pos = eye->pos;
  5066. cv.focusTime = 2000;
  5067. cb->sendAndApply(&cv);
  5068. }
  5069. cv.pos = h->getPosition(false);
  5070. cb->sendAndApply(&cv);
  5071. }
  5072. else if (ID == 27)
  5073. {
  5074. InfoWindow iw;
  5075. iw.player = h->tempOwner;
  5076. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  5077. cb->showInfoDialog(&iw);
  5078. }
  5079. }
  5080. void CGBoat::initObj()
  5081. {
  5082. hero = NULL;
  5083. }
  5084. void CGSirens::initObj()
  5085. {
  5086. blockVisit = true;
  5087. }
  5088. const std::string & CGSirens::getHoverText() const
  5089. {
  5090. getNameVis(hoverName);
  5091. return hoverName;
  5092. }
  5093. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  5094. {
  5095. int message;
  5096. InfoWindow iw;
  5097. iw.soundID = soundBase::DANGER;
  5098. iw.player = h->tempOwner;
  5099. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  5100. {
  5101. iw.text.addTxt(11,133);
  5102. }
  5103. else
  5104. {
  5105. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  5106. int xp = 0;
  5107. SetGarrisons sg;
  5108. sg.garrs[h->id] = h->getArmy();
  5109. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  5110. {
  5111. int drown = (int)(i->second.count * 0.3);
  5112. if(drown)
  5113. {
  5114. sg.garrs[h->id].setStackCount(i->first, i->second.count - drown);
  5115. xp += drown * i->second.type->valOfBonuses(Bonus::STACK_HEALTH);
  5116. }
  5117. }
  5118. if(xp)
  5119. {
  5120. iw.text.addTxt(11,132);
  5121. iw.text.addReplacement(xp);
  5122. cb->sendAndApply(&sg);
  5123. }
  5124. else
  5125. {
  5126. iw.text.addTxt(11,134);
  5127. }
  5128. }
  5129. cb->showInfoDialog(&iw);
  5130. }
  5131. //bool IShipyard::validLocation() const
  5132. //{
  5133. // std::vector<int3> offsets;
  5134. // getOutOffsets(offsets);
  5135. //
  5136. // TerrainTile *tile;
  5137. // for(int i = 0; i < offsets.size(); i++)
  5138. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  5139. // return true;
  5140. // return false;
  5141. //}
  5142. int3 IBoatGenerator::bestLocation() const
  5143. {
  5144. std::vector<int3> offsets;
  5145. getOutOffsets(offsets);
  5146. TerrainTile *tile;
  5147. for(int i = 0; i < offsets.size(); i++)
  5148. if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->tertype == TerrainTile::water) //tile is in the map and is water
  5149. return o->pos + offsets[i];
  5150. return int3(-1,-1,-1);
  5151. }
  5152. int IBoatGenerator::state() const
  5153. {
  5154. int3 tile = bestLocation();
  5155. TerrainTile *t = IObjectInterface::cb->getTile(tile);
  5156. if(!t)
  5157. return 3; //no water
  5158. else if(!t->blockingObjects.size())
  5159. return 0; //OK
  5160. else if(t->blockingObjects.front()->ID == 8)
  5161. return 1; //blocked with boat
  5162. else
  5163. return 2; //blocked
  5164. }
  5165. int IBoatGenerator::getBoatType() const
  5166. {
  5167. //We make good ships by default
  5168. return 1;
  5169. }
  5170. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  5171. : o(O)
  5172. {
  5173. }
  5174. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  5175. {
  5176. cost.resize(RESOURCE_QUANTITY);
  5177. cost[0] = 10;
  5178. cost[6] = 1000;
  5179. }
  5180. IShipyard::IShipyard(const CGObjectInstance *O)
  5181. : IBoatGenerator(O)
  5182. {
  5183. }
  5184. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  5185. {
  5186. if(obj->ID == TOWNI_TYPE)
  5187. {
  5188. return static_cast<CGTownInstance*>(obj);
  5189. }
  5190. else if(obj->ID == 87)
  5191. {
  5192. return static_cast<CGShipyard*>(obj);
  5193. }
  5194. else
  5195. {
  5196. tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  5197. return NULL;
  5198. }
  5199. }
  5200. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  5201. {
  5202. return castFrom(const_cast<CGObjectInstance*>(obj));
  5203. }
  5204. CGShipyard::CGShipyard()
  5205. :IShipyard(this)
  5206. {
  5207. }
  5208. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  5209. {
  5210. offsets += int3(1,0,0), int3(-3,0,0), int3(1,1,0), int3(-3,1,0), int3(1,-1,0), int3(-3,-1,0),
  5211. int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0);
  5212. }
  5213. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  5214. {
  5215. if(tempOwner != h->tempOwner)
  5216. cb->setOwner(id, h->tempOwner);
  5217. int s = state();
  5218. if(s)
  5219. {
  5220. InfoWindow iw;
  5221. iw.player = tempOwner;
  5222. switch(s)
  5223. {
  5224. case 1:
  5225. iw.text.addTxt(MetaString::GENERAL_TXT, 51);
  5226. break;
  5227. case 2:
  5228. iw.text.addTxt(MetaString::ADVOB_TXT, 189);
  5229. break;
  5230. case 3:
  5231. tlog1 << "Shipyard without water!!! " << pos << "\t" << id << std::endl;
  5232. return;
  5233. }
  5234. cb->showInfoDialog(&iw);
  5235. }
  5236. else
  5237. {
  5238. OpenWindow ow;
  5239. ow.id1 = id;
  5240. ow.id2 = h->id;
  5241. ow.window = OpenWindow::SHIPYARD_WINDOW;
  5242. cb->sendAndApply(&ow);
  5243. }
  5244. }
  5245. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  5246. {
  5247. if (!hasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  5248. {
  5249. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  5250. {
  5251. //ask if he wants to buy one
  5252. int text;
  5253. switch (subID)
  5254. {
  5255. case 0:
  5256. text = 25;
  5257. break;
  5258. case 1:
  5259. text = 26;
  5260. break;
  5261. case 2:
  5262. text = 27;
  5263. break;
  5264. default:
  5265. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  5266. }
  5267. BlockingDialog bd (true, false);
  5268. bd.player = h->getOwner();
  5269. bd.soundID = soundBase::LIGHTHOUSE;
  5270. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  5271. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  5272. }
  5273. else //if he cannot afford
  5274. {
  5275. InfoWindow iw;
  5276. iw.player = h->getOwner();
  5277. iw.soundID = soundBase::CAVEHEAD;
  5278. iw.text << std::pair<ui8,ui32>(11,28);
  5279. cb->showInfoDialog (&iw);
  5280. }
  5281. }
  5282. else //if he already visited carographer
  5283. {
  5284. InfoWindow iw;
  5285. iw.player = h->getOwner();
  5286. iw.soundID = soundBase::CAVEHEAD;
  5287. iw.text << std::pair<ui8,ui32>(11,24);
  5288. cb->showInfoDialog (&iw);
  5289. }
  5290. }
  5291. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  5292. {
  5293. if (accept) //if hero wants to buy map
  5294. {
  5295. cb->giveResource (h->tempOwner, 6, -1000);
  5296. FoWChange fw;
  5297. fw.player = h->tempOwner;
  5298. //subIDs of different types of cartographers:
  5299. //water = 0; land = 1; underground = 2;
  5300. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  5301. cb->sendAndApply (&fw);
  5302. cb->setObjProperty (id, 10, h->tempOwner);
  5303. }
  5304. }
  5305. void CShop::newTurn() const
  5306. {
  5307. switch (ID)
  5308. {
  5309. case 7: //ArtMerchant aka. Black Market
  5310. if (cb->getDate(0)%28 == 1)
  5311. {
  5312. cb->setObjProperty (id, 13, 0);
  5313. cb->setObjProperty (id, 14, rand());
  5314. }
  5315. break;
  5316. case 78: //Refugee Camp
  5317. case 95: //Tavern -- global hero pool?
  5318. if (cb->getDate(0)%7 == 1)
  5319. cb->setObjProperty (id, 14, rand());
  5320. break;
  5321. }
  5322. }
  5323. void CShop::setPropertyDer (ui8 what, ui32 val)
  5324. {
  5325. switch (what)
  5326. {
  5327. case 14: //reset - get new items
  5328. reset(val);
  5329. break;
  5330. }
  5331. }
  5332. void CGArtMerchant::reset(ui32 val)
  5333. {//TODO: it should have 2 global pools instead of unique for each merchant:
  5334. // 1) for town merchants - resets every month,
  5335. // 2) for adv. map - resets only on game start
  5336. std::vector<CArtifact*>::iterator index;
  5337. for (ui8 i = 0; i < 3; ++i) //each tier
  5338. {
  5339. int count = 0;
  5340. std::vector<CArtifact*> arts; //to avoid addition of different tiers
  5341. switch (i)
  5342. {
  5343. case 0:
  5344. cb->getAllowed (arts, CArtifact::ART_TREASURE);
  5345. count = 3; // first row - three treasures,
  5346. break;
  5347. case 1:
  5348. cb->getAllowed (arts, CArtifact::ART_MINOR);
  5349. count = 3; // second row three minors
  5350. break;
  5351. case 2:
  5352. cb->getAllowed (arts, CArtifact::ART_MAJOR);
  5353. count = 1; // and a third row - one major
  5354. break;
  5355. }
  5356. for (ui8 n = 0; n < count; n++)
  5357. {
  5358. index = arts.begin() + val % arts.size();
  5359. advMapArts [advMapArts.size()] = new Component (Component::ARTIFACT, (*index)->id, 0, 0);
  5360. arts.erase(index);
  5361. val *= (id + n * i); //randomize
  5362. }
  5363. }
  5364. }
  5365. void CGRefugeeCamp::reset(ui32 val)
  5366. {
  5367. creatureID = VLC->creh->creatures[val%VLC->creh->creatures.size()]->idNumber;
  5368. }
  5369. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  5370. {
  5371. cb->showThievesGuildWindow(id);
  5372. }
  5373. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  5374. {
  5375. InfoWindow iw;
  5376. iw.player = h->tempOwner;
  5377. if(!hasVisited(h->tempOwner))
  5378. {
  5379. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  5380. cb->sendAndApply(&iw);
  5381. cb->setObjProperty(id,20,h->tempOwner); //increment general visited obelisks counter
  5382. OpenWindow ow;
  5383. ow.id1 = h->tempOwner;
  5384. ow.window = OpenWindow::PUZZLE_MAP;
  5385. cb->sendAndApply(&ow);
  5386. cb->setObjProperty(id,10,h->tempOwner); //mark that particular obelisk as visited
  5387. }
  5388. else
  5389. {
  5390. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  5391. cb->sendAndApply(&iw);
  5392. }
  5393. }
  5394. void CGObelisk::initObj()
  5395. {
  5396. obeliskCount++;
  5397. }
  5398. const std::string & CGObelisk::getHoverText() const
  5399. {
  5400. hoverName = VLC->generaltexth->names[ID];
  5401. if(hasVisited(cb->getCurrentPlayer()))
  5402. hoverName += " " + VLC->generaltexth->allTexts[352]; //not visited
  5403. else
  5404. hoverName += " " + VLC->generaltexth->allTexts[353]; //visited
  5405. return hoverName;
  5406. }
  5407. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  5408. {
  5409. CPlayersVisited::setPropertyDer(what, val);
  5410. switch(what)
  5411. {
  5412. case 20:
  5413. assert(val < PLAYER_LIMIT);
  5414. visited[val]++;
  5415. assert(visited[val] <= obeliskCount);
  5416. break;
  5417. }
  5418. }
  5419. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  5420. {
  5421. if(h->tempOwner != tempOwner)
  5422. {
  5423. ui8 oldOwner = tempOwner;
  5424. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  5425. InfoWindow iw;
  5426. iw.player = h->tempOwner;
  5427. iw.text.addTxt(MetaString::ADVOB_TXT, 69);
  5428. iw.soundID = soundBase::LIGHTHOUSE;
  5429. cb->sendAndApply(&iw);
  5430. giveBonusTo(h->tempOwner);
  5431. if(oldOwner < PLAYER_LIMIT) //remove bonus from old owner
  5432. {
  5433. RemoveBonus rb(RemoveBonus::PLAYER);
  5434. rb.whoID = oldOwner;
  5435. rb.source = Bonus::OBJECT;
  5436. rb.id = id;
  5437. cb->sendAndApply(&rb);
  5438. }
  5439. }
  5440. }
  5441. void CGLighthouse::initObj()
  5442. {
  5443. if(tempOwner < PLAYER_LIMIT)
  5444. {
  5445. giveBonusTo(tempOwner);
  5446. }
  5447. }
  5448. const std::string & CGLighthouse::getHoverText() const
  5449. {
  5450. hoverName = VLC->generaltexth->names[ID];
  5451. //TODO: owned by %s player
  5452. return hoverName;
  5453. }
  5454. void CGLighthouse::giveBonusTo( ui8 player ) const
  5455. {
  5456. GiveBonus gb(GiveBonus::PLAYER);
  5457. gb.bonus.type = Bonus::SEA_MOVEMENT;
  5458. gb.bonus.val = 500;
  5459. gb.id = player;
  5460. gb.bonus.duration = Bonus::PERMANENT;
  5461. gb.bonus.source = Bonus::OBJECT;
  5462. gb.bonus.id = id;
  5463. cb->sendAndApply(&gb);
  5464. }
  5465. void CArmedInstance::setArmy(const CCreatureSet &src)
  5466. {
  5467. slots.clear();
  5468. for(TSlots::const_iterator i = src.Slots().begin(); i != src.Slots().end(); i++)
  5469. {
  5470. CStackInstance &inserted = slots[i->first];
  5471. inserted = i->second;
  5472. inserted.armyObj = this;
  5473. }
  5474. }
  5475. CCreatureSet CArmedInstance::getArmy() const
  5476. {
  5477. return *this;
  5478. }
  5479. void CArmedInstance::randomizeArmy(int type)
  5480. {
  5481. int max = VLC->creh->creatures.size();
  5482. for (TSlots::iterator j = slots.begin(); j != slots.end();j++)
  5483. {
  5484. if(j->second.idRand > max)
  5485. {
  5486. if(j->second.idRand % 2)
  5487. j->second.setType(VLC->townh->towns[type].basicCreatures[(j->second.idRand-197) / 2 -1]);
  5488. else
  5489. j->second.setType(VLC->townh->towns[type].upgradedCreatures[(j->second.idRand-197) / 2 -1]);
  5490. j->second.idRand = -1;
  5491. }
  5492. assert(j->second.armyObj == this);
  5493. }
  5494. return;
  5495. }
  5496. void CArmedInstance::getParents(TCNodes &out, const CBonusSystemNode *root /*= NULL*/) const
  5497. {
  5498. const PlayerState *p = cb->getPlayerState(tempOwner);
  5499. if(!p) //occurs when initializing starting hero and heroes from the pool, also for neutral armed objects
  5500. out.insert(&cb->gameState()->globalEffects);
  5501. else
  5502. out.insert(p); //hero always inherits bonuses from player
  5503. if(battle)
  5504. out.insert(battle);
  5505. }
  5506. CArmedInstance::CArmedInstance()
  5507. {
  5508. battle = NULL;
  5509. }
  5510. void CArmedInstance::getBonuses(BonusList &out, const CSelector &selector, const CBonusSystemNode *root /*= NULL*/) const
  5511. {
  5512. CBonusSystemNode::getBonuses(out, selector, root);
  5513. if(!battle)
  5514. {
  5515. //TODO do it clean, unify with BattleInfo version
  5516. if(Selector::matchesType(selector, Bonus::MORALE) || Selector::matchesType(selector, Bonus::LUCK))
  5517. {
  5518. for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  5519. i->second.getBonuses(out, selector, Selector::effectRange(Bonus::ONLY_ALLIED_ARMY), this);
  5520. }
  5521. }
  5522. if(Selector::matchesType(selector, Bonus::MORALE))
  5523. {
  5524. //number of alignments and presence of undead
  5525. if(contains(dynamic_cast<const CStackInstance*>(root)))
  5526. {
  5527. bool archangelInArmy = false;
  5528. bool canMix = hasBonusOfType(Bonus::NONEVIL_ALIGNMENT_MIX);
  5529. std::set<si8> factions;
  5530. for(TSlots::const_iterator i=Slots().begin(); i!=Slots().end(); i++)
  5531. {
  5532. // Take Angelic Alliance troop-mixing freedom of non-evil, non-Conflux units into account.
  5533. const si8 faction = i->second.type->faction;
  5534. if (canMix
  5535. && ((faction >= 0 && faction <= 2) || faction == 6 || faction == 7))
  5536. {
  5537. factions.insert(0); // Insert a single faction of the affected group, Castle will do.
  5538. }
  5539. else
  5540. {
  5541. factions.insert(faction);
  5542. }
  5543. if(i->second.type->idNumber == 13)
  5544. archangelInArmy = true;
  5545. }
  5546. if(factions.size() == 1)
  5547. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, +1, id, VLC->generaltexth->arraytxt[115]));//All troops of one alignment +1
  5548. else
  5549. {
  5550. int fcountModifier = 2-factions.size();
  5551. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, fcountModifier, id, boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factions.size() % fcountModifier)));//Troops of %d alignments %d
  5552. }
  5553. if(vstd::contains(factions,4))
  5554. out.push_back(Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, id, VLC->generaltexth->arraytxt[116]));//Undead in group -1
  5555. }
  5556. }
  5557. }
  5558. bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode mode) const
  5559. {
  5560. switch(mode)
  5561. {
  5562. case RESOURCE_RESOURCE:
  5563. {
  5564. float effectiveness = std::min(((float)getMarketEfficiency()+1.0f) / 20.0f, 0.5f);
  5565. float r = VLC->objh->resVals[id1], //value of given resource
  5566. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  5567. if(r>g) //if given resource is more expensive than wanted
  5568. {
  5569. val2 = ceil(r / g);
  5570. val1 = 1;
  5571. }
  5572. else //if wanted resource is more expensive
  5573. {
  5574. val1 = (g / r) + 0.5f;
  5575. val2 = 1;
  5576. }
  5577. }
  5578. break;
  5579. case CREATURE_RESOURCE:
  5580. {
  5581. const float effectivenessArray[] = {0, 0.3, 0.45, 0.50, 0.65, 0.7, 0.85, 0.9, 1};
  5582. float effectiveness = effectivenessArray[std::min(getMarketEfficiency(), 8)];
  5583. float r = VLC->creh->creatures[id1]->cost[6], //value of given creature in gold
  5584. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  5585. if(r>g) //if given resource is more expensive than wanted
  5586. {
  5587. val2 = ceil(r / g);
  5588. val1 = 1;
  5589. }
  5590. else //if wanted resource is more expensive
  5591. {
  5592. val1 = (g / r) + 0.5f;
  5593. val2 = 1;
  5594. }
  5595. }
  5596. break;
  5597. case RESOURCE_PLAYER:
  5598. val1 = 1;
  5599. val2 = 1;
  5600. break;
  5601. default:
  5602. assert(0);
  5603. return false;
  5604. }
  5605. return true;
  5606. }
  5607. bool IMarket::allowsTrade(EMarketMode mode) const
  5608. {
  5609. return false;
  5610. }
  5611. int IMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  5612. {
  5613. switch(mode)
  5614. {
  5615. case RESOURCE_RESOURCE:
  5616. case ARTIFACT_RESOURCE:
  5617. case CREATURE_RESOURCE:
  5618. return -1;
  5619. default:
  5620. return 1;
  5621. }
  5622. }
  5623. std::vector<int> IMarket::availableItemsIds(EMarketMode mode) const
  5624. {
  5625. std::vector<int> ret;
  5626. switch(mode)
  5627. {
  5628. case RESOURCE_RESOURCE:
  5629. case ARTIFACT_RESOURCE:
  5630. case CREATURE_RESOURCE:
  5631. for (int i = 0; i < 7; i++)
  5632. ret.push_back(i);
  5633. break;
  5634. }
  5635. return ret;
  5636. }
  5637. const IMarket * IMarket::castFrom(const CGObjectInstance *obj)
  5638. {
  5639. switch(obj->ID)
  5640. {
  5641. case TOWNI_TYPE:
  5642. return static_cast<const CGTownInstance*>(obj);
  5643. case 7: //Black Market
  5644. case 99: //Trading Post
  5645. case 221: //Trading Post (snow)
  5646. case 213: //Freelancer's Guild
  5647. return static_cast<const CGMarket*>(obj);
  5648. default:
  5649. tlog1 << "Cannot cast to IMarket object with ID " << obj->ID << std::endl;
  5650. return NULL;
  5651. }
  5652. }
  5653. IMarket::IMarket(const CGObjectInstance *O)
  5654. :o(O)
  5655. {
  5656. }
  5657. std::vector<EMarketMode> IMarket::availableModes() const
  5658. {
  5659. std::vector<EMarketMode> ret;
  5660. for (int i = 0; i < MARTKET_AFTER_LAST_PLACEHOLDER; i++)
  5661. if(allowsTrade((EMarketMode)i))
  5662. ret.push_back((EMarketMode)i);
  5663. return ret;
  5664. }
  5665. void CGMarket::onHeroVisit(const CGHeroInstance * h) const
  5666. {
  5667. OpenWindow ow;
  5668. ow.id1 = id;
  5669. ow.id2 = h->id;
  5670. ow.window = OpenWindow::MARKET_WINDOW;
  5671. cb->sendAndApply(&ow);
  5672. }
  5673. void CGMarket::initObj()
  5674. {
  5675. }
  5676. void CGMarket::newTurn() const
  5677. {
  5678. }
  5679. int CGMarket::getMarketEfficiency() const
  5680. {
  5681. return 5;
  5682. }
  5683. bool CGMarket::allowsTrade(EMarketMode mode) const
  5684. {
  5685. switch(mode)
  5686. {
  5687. case RESOURCE_RESOURCE:
  5688. case RESOURCE_PLAYER:
  5689. switch(ID)
  5690. {
  5691. case 99: //Trading Post
  5692. case 221: //Trading Post (snow)
  5693. return true;
  5694. default:
  5695. return false;
  5696. }
  5697. case CREATURE_RESOURCE:
  5698. return ID == 213; //Freelancer's Guild
  5699. }
  5700. return false;
  5701. }
  5702. int CGMarket::availableUnits(EMarketMode mode, int marketItemSerial) const
  5703. {
  5704. return -1;
  5705. }
  5706. std::vector<int> CGMarket::availableItemsIds(EMarketMode mode) const
  5707. {
  5708. switch(mode)
  5709. {
  5710. case RESOURCE_RESOURCE:
  5711. case RESOURCE_PLAYER:
  5712. return IMarket::availableItemsIds(mode);
  5713. default:
  5714. return std::vector<int>();
  5715. }
  5716. }
  5717. CGMarket::CGMarket()
  5718. :IMarket(this)
  5719. {
  5720. }