CObjectHandler.cpp 191 KB

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