CObjectHandler.cpp 179 KB

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