CObjectHandler.cpp 187 KB

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