CObjectHandler.cpp 191 KB

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