CObjectHandler.cpp 180 KB

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