CObjectHandler.cpp 173 KB

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