CObjectHandler.cpp 178 KB

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