CObjectHandler.cpp 185 KB

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