CObjectHandler.cpp 192 KB

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