CObjectHandler.cpp 185 KB

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