CObjectHandler.cpp 188 KB

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