CObjectHandler.cpp 183 KB

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