CObjectHandler.cpp 175 KB

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