CObjectHandler.cpp 182 KB

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