CObjectHandler.cpp 180 KB

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