CObjectHandler.cpp 192 KB

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